article by Frank Nimphius, November 2019
Oracle Digital Assistant analyzes user messages to identify intents and to extract information. This article explains 4 options you can use to find and extract information from user messages sent to skills in Digital Assistant.
Option 1: System.Intent component
The system intent component usually is one of the first components in a dialog flow. It passes the incoming user message to the NLP model for it to identify the user intent and to extract values for configured entities. The image below shows an example for a fictitious booking bot. The intent engine recognized the user intention to confirm a booking and also extracts the booking number to confirm.
The approach is based on a custom entity, which in case of this sample is an entity of type Regular Expression. The expression checks the user message for a pattern of 'CX-' followed by 7 digits.
For the entity to be resolved with the intent, you need to associate it with the entity. In the image below, the "ConfirmBooking" intent is associated with the BookingCode entity. You can test this by clicking the Try it Out! link and then typing a sentence like "I like to confirm CX-4545631". This then will show the detected intent but also the BookingCode entity value.
The image below shows the dialog flow code used in this sample. A variable bookingCode of type BookingCode is defined to dave the extracted user value. A variable "iResult" of type nlpresult is associated with the System.Intent component. The resolved intent and the extracted entities (BookingCode in the sample) will be saved within this variable.
Note: Option 1 and option 2 are related, so make sure you read option 2 even if your requirement is met already.
Option 2: Input Prompt
Option 2 also uses the BookingCode entity. This option works for dialog flow states that prompt users for a value. In the image below, the prompt is "To confirm a booking, please provide a valid booking code". Bot users then may type a valid or invalid booking code, or, as shown in the image below, type a sentence that contains a valid or invalid booking code. This option 2 handles bot use cases.
The dialog flow sample below references the bookingCode variable of type BookingCode from the System.Text variable property. This settings ensures that user input is validated against the BookingCode entity, and, if a valid value is found, the value is saved in the variable. As you can see in the image below, the nlpResultVariable of the System.Text component points to the iResult variable. This is where Option 1 and Option 2 relate. Because if the initial user message already contains the order number, then this is saved in the iResult variable.
If iResult contains the entity value then the System.Text component in the askBookingCode state is not rendered but skipped.
If a user is prompted to provide a valid booking code and provides an invalid code, then the prompt is repeated with an additional information (see image below)
Option 3: System.MatchEntity
The third option again uses an entity to validate the user message. However, the message itself is saved in a string variable, which could e.g. be the case if the string is saved through a custom component.
The image below shows two variables that are defined. the bookingCodeStr variable holds the user message as a string. The bookingCode variable is of type BookingCode and should later hold the valid booking code number provided by the user.
The System.Text component used in Option 2 now references the bookingCodeStr variable (this is how in the sample we get the user message). Note that the nlpResultVariable property has no effect in this case because the variable is of type string and not entity.
Lets assume the user provides the following message when prompted for the booking code: "Hmm. I have CX-1234561, can you confirm" . The message clearly contains more information than the booking code.
To resolve the booking code in the image above, the dialog flow navigates to the System.MatchEntity component state. The System.MatchEntity component references the variable of type BookingCode in its "variable" property and the variable bookingCodeStr (of type string) in its sourceVariable property. If the user message contains a valid booking code, then 1. the booing code gets saved in the bookingCode variable and 2. the dialog flow navigation follows the match action transition. If no booking code is found, then the nomatch transition is followed.
The image below shows the bot response for the "Hmm. I have CX-1234561, can you confirm" user message.
Option 4: Apache FreeMarker Expressions
Using entities for validating user input and to extract values from messages certainly is the most easiest to use. However, a 4th option is to use Apache FreeMarker to find a matching pattern (like a booking code) in a user message. Regular Expression support in Apache FreeMarker is not great and thus requires some good skills in this area to find a working expression. The image below shows a runtime example in which the bot successfully detects and isolates the booking code.
The dialog flow code shown below uses two regular expressions. The checkValidBookingCode state uses a System.Switch component along with Apache FreeMarker expressions to find a pattern that matches 'CX-' followed by 7 digits. If found, the expression returns a value 'match'. If not found then the expression returns 'nomatch'- The 'match' and 'nomatch' values are mapped to dialog flow states to handle the success and failure case.
In the case where the booking code pattern was found, another RegularExpression is used in the System.SetVariable component to save the booking code in the bookingCode variable. The result then can be printed from the variable.
Summary
This article shows 4 options you can use to extract information from user messages. In this article, the extracted information – a booking code – is printed in a message. In a real bot use case the information usually would be used as an input parameter in a remote service request.
Related Content
TechExchange – Using Apache Freemarker Expression in Oracle Intelligent Bots
TechExchange Quick-Tip: How-to Validate User Input For Non-Entity Variables
TechExchange: Entities Best Practices – Refining Entity Validation Against A Dynamic Sublist Of Values
TechExchange: All 2-Minutes Oracle Digital Assistant Tech Tip Videos on YouTube