Pattern weight modification
If you use patterns to implement NLU in your script, it is possible to forcibly change the weight of matches by patterns or their individual elements.
This can be a useful feature in a number of cases:
- The request is matched by several different patterns. You need to specify explicitly which of these patterns should have priority and be assigned a higher confidence value.
- Conversely, you may need to assign a negative priority to one of the pattern elements, so that matches against it lower the confidence score below the threshold value and don’t trigger any states.
$weight
Use the $weight
element to modify pattern weight.
This element can be used either after the q
and q!
tags or when declaring named patterns using the patterns
tag.
$weight
can take one of the following shapes:
$weight<a>
$weight<a+b>
$weight<a-b>
$weight<+b>
$weight<-b>
The pattern weight changes according to the equation:
where is the final pattern weight,
and and are the number values set in $weight
.
and are arbitrary real numbers. The difference between and is that:
- changes the weight linearly, multiplying it by .
- changes the weight by a constant value, adding to it.
How to use
In the script below, there are similar-looking states differing only in $weight
presence and values.
A postProcess
handler is configured so that the bot response contains the confidence score of pattern matches.
This score will be different for each test [1..5]
request.
theme: /
init:
bind("postProcess", function(ctx) {
$reactions.answer("Weight: " + ctx.nBest[0].score);
});
state: Start
q!: $regex</start>
a: Let’s begin.
state: Test1
q!: test 1
# Weight: 0.79
state: Test2
q!: test 2 $weight<1.001>
# Weight: 0.7908
state: Test3
q!: test 3 $weight<0.9+0.05>
# Weight: 0.75
state: Test4
q!: test 4 $weight<+0.3>
# Weight: 1.03
state: Test5
q!: test 5 $weight<-0.2>
# Weight: 0.63
$localWeight
If you use the $weight
element in a pattern, it modifies the confidence of matching requests against this pattern as a whole.
However, in some cases you may need a more flexible behavior
to change the weight only for specific pattern components matched by named patterns.
Use the $localWeight
element to do so.
The $localWeight
element takes the same shapes as $weight
.
It has to be followed by a named pattern reference.
The number specified in $localWeight
is only used for calculating the weight of request substrings matched by this named pattern.
How to use
The difference between $weight
and $localWeight
can be observed in the following script example.
If the Test1
state is triggered, the confidence score is lower than that for Test2
:
- In
Test1
, the request is matched against the pattern, and then the whole score is decreased by 20%. - In
Test2
, only the score of thetest
substring is decreased, while the score for2
is calculated as before.
patterns:
$test = test
theme: /
init:
bind("postProcess", function(ctx) {
$reactions.answer("Weight: " + ctx.nBest[0].score);
});
state: Start
q!: $regex</start>
a: Let’s begin.
state: Test1
q!: $weight<0.8> $test 1
# Weight: 0.8
state: Test2
q!: $localWeight<0.8> $test 2
# Weight: 0.84