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 with the following fields:
{
"patterns": [
// All activation rules triggered by the pattern classifier
],
"intents": [
// Rules triggered by the intent classifier
],
"selected": {
// The selected activation rule
}
}
In order to redefine the activation rule, change the value of nluResults.selected
.
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.
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
});
});
We use the features of the Underscore.js library in this example.