Skip to main content

selectNLUResult

When several types of activation rules are used together in one script, they are triggered in the following order: patterns first, then intents.

If some alternative behavior is required, you can redefine the mechanism of selecting the activation rule when several rule types can be triggered at the same time.

Handler definition

For configuring the required behavior, you need to write a selectNLUResult handler in the script and pass it to the bind function:

bind("selectNLUResult", function($context) {
// ...
});

The handler function receives the context object as an argument, which in turn contains a nluResults object.

nluResults object

{
"patterns": [
// An array with all activation rules triggered by the pattern classifier.
],
"intents": [
// An array with all rules triggered by the intent classifier.
],
"selected": {
// An object with the selected activation rule.
}
}
tip
In order to redefine the activation rule, change the value of nluResults.selected.

The arrays contained in nluResults have objects with the following fields:

FieldTypeDescription
clazzStringThe path to the state that matched the request.
scoreNumberThe probability score of the triggered activation rule.
ptObjectAlias for parseTree.
debugInfoObjectTechnical information about the triggered rule.
answerStringThe answer to the intent.
Patterns and answers from the knowledge base do not have this field.
ruleTypeStringThe type of the triggered rule: patternintent, or intentGroup.
priorityNumberIntent priority. It matters if the intents have the same probability score and contextual distance to the state.
Patterns do not have this field.
contentObjectThe field has an array replies which contains information about the answers from the knowledge base:
type is the answer type.
text, image, audio, file are the fields with the content of the answer.
distanceNumberContextual distance to the state that matched the request.
locatorObjectThe object contains the following fields:
filename is the name of the file in which the state is specified.
line is the line number in the code editor where the state starts.
col is the number of the first character of the pattern, intent, or event, which is specified after the trigger tag by which the request reached the state. Reverse numbering is used, that is, counting goes from right to left.
storageType is the storage type of the file that contains the state. Possible values: "SYSTEM", "GIT", "LOCAL", "RUNTIME".
parseTreeObjectFull request parse tree.
targetStateStringThe state where the transition was made.

How to use

Reorder activation rule triggers

We can change the order of triggering activation rules so that, for example, intents have priority over patterns.

tip
You can define the handler function either in the init section or in a separate file with the .js extension, which should then be imported into the script via the require tag.
bind("selectNLUResult", function($context) {
// Print the results to the log for debugging.
log($context.nluResults);

if ($context.nluResults.intents.length > 0) {
// If at least one rule was triggered by the intent classifier, use the first such rule.
$context.nluResults.selected = $context.nluResults.intents[0];
return;
}

if ($context.nluResults.patterns.length > 0) {
// If no intents were triggered, use the first rule from the pattern classifier.
$context.nluResults.selected = $context.nluResults.patterns[0];
}
});

Trigger the rule with maximum score

We can configure the activation mechanism so that the rule with the highest score always has priority, regardless of whether it was triggered by a pattern or an intent.

bind("selectNLUResult", function($context) {
// Convert all triggered rules from all classifiers into an array.
var allResults = _.chain($context.nluResults)
.omit("selected")
.values()
.flatten()
.value();

// Count the maximum value of `score` among all rules.
var maxScore = _.chain(allResults)
.pluck("score")
.max()
.value();

// Assign the rule with the maximum score to `nluResults.selected`.
$context.nluResults.selected = _.findWhere(allResults, {
score: maxScore
});
});
tip
We use the features of the Underscore.js library in this example.