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.
}
}
nluResults.selected
.The arrays contained in nluResults
have objects with the following fields:
Field | Type | Description |
---|---|---|
clazz | String | The path to the state that matched the request. |
score | Number | The probability score of the triggered activation rule. |
pt | Object | Alias for parseTree . |
debugInfo | Object | Technical information about the triggered rule. |
answer | String | The answer to the intent. Patterns and answers from the knowledge base do not have this field. |
ruleType | String | The type of the triggered rule: pattern , intent , or intentGroup . |
priority | Number | Intent priority. It matters if the intents have the same probability score and contextual distance to the state. Patterns do not have this field. |
content | Object | The 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. |
distance | Number | Contextual distance to the state that matched the request. |
locator | Object | The 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" . |
parseTree | Object | Full request parse tree. |
targetState | String | The 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.
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
});
});