$nlp.match
The method classifies the text in the context of the specified state.
Syntax
The method accepts the phrase to match and the path where to perform the match as input parameters.
$nlp.match("test 1", "/")
The method returns the NLPResult
object, which contains information about the triggered pattern, state, and the parseTree
object.
{
"parseTree": {
"tag": "root",
"pattern": "root",
"text": "bye",
"startPos": 0,
"endPos": 1,
"words": "bye",
"_Root": "bye"
},
"targetState": "/Bye",
"pattern": "bye",
"effectivePattern": "bye",
"score": "1.0"
}
How to use
The $nlp.match
method can be used to manage the order in which phrases are classified in specific contexts.
For example, there is a script where the bot tells clients about current promotions. Information about promotions is stored in a third-party service.
state: Promos || modal = true
# Display the promotion names.
a: {{$session.promos.join(", ")}}
a: Which of our promotions would you like to know more about?
state: Get
event: noMatch
script:
$temp.match = $nlp.matchExamples($parseTree.text, $session.promos);
# If the promotion name was found in the request, save it and switch to the /PromoDetails state.
if: $temp.match && $temp.match.weight >= 0.2
script:
$session.promoSelected = $temp.match.example;
go!: /PromoDetails
# If not, search for the matches in other states.
script:
$temp.match = $nlp.match($parseTree.text, "/");
# If a match with another state was found, move there.
if: $temp.match && $temp.match.score >= 0.2
script:
$parseTree = $temp.match.parseTree;
go!: {{$temp.match.targetState}}
# If the request still wasn’t recognized, display the following message:
a: I don’t understand you. What promotion would you like to know about?
Let’s create a Promos
state where the list of promotions will be displayed to the client. To stay in the context of this state until the client’s request is processed, set the modal
flag to true
.
As mentioned before, information about promotions is stored in a third-party service, not in the bot script. Therefore, using patterns to obtain this information can be inefficient. Instead, we will use the Get
state with the noMatch
event that will be triggered on unrecognized client requests.
Since we still want to recognize the client’s phrase to tell them about promotions, we will use the $nlp.matchExamples
method to determine the requested promotion and redirect the dialog to the right state. If the received request could not find the corresponding promotion name, then use the $nlp.match
method to recognize the whole phrase. If the corresponding state still wasn’t found, display the following message:
I don’t understand you. What promotion would you like to know about?
modal = true
flag is specified in the state in which to search, then only its nested states will be matched by the $nlp.match
method.