Expression entities are entities that consist of a regular expression, as opposed to one or multiple (compound) words. Regular expressions (Regex) are a sequence of characters that match a pattern in a piece of text. They make it possible to react to user input (e.g. a question) containing a specific sequence such as a postal code or tracking number; basically, any input that is variable and cannot be captured in a single word (without adding all the possible variations).
Regex has precedence over regular entities and will override the normalization process. This means that any input matching that regex will not be normalized, so make sure that your regex is as specific as possible, so they don't interfere with regular recognition in your project.
A great example of an expression type entity would be an entity that aims to capture a 5-digit postal code when provided. Because the amount of postal codes is extensive, it’s impossible to capture all possible variations in a regular entity (as this would involve adding countless words in the form of postal codes in an entity). What all postal codes do have in common, is their format. This makes them a perfect fit for being captured through an expression entity. Let’s create an expression entity to capture postal codes.
Create & Edit Regular Expression
Before you start creating regular expressions in Conversational AI Cloud, it’s good to know that they use case-insensitive matching and that they are validated and run using the ECMAScript standard.
We’re going to add a regular expression that will recognize a Dutch postal code from the end user’s input.
Follow these steps to create an expression type entity:
- Go to the Entities editor and click the add icon “+ “.
- Select Create expression from the drop-down list
- Add a name and description for your regular expression
- Insert the expression, for postal code: “[1-9]\d{3}\s?[a-zA-Z]{2} ”
- Click Save
If you have any questions that match with the regular expression that you just created, they'll show underneath.
Disclaimer:
After creating or editing your expression entity, the system will run it through your project's content. Should it find a match, it’ll link the entity to the question similar to how standard entities work. As expression entities allow for very broad recognition when defined poorly, this may have a negative effect on your recognition. If you’re unsure about the inner workings of regular expressions, make sure to take a look at some of the following external resources:
- developer.mozilla.org Full guide on writing and using Regex
- regexr.com Full Regex reference with help & examples
Test Regular Expressions
When you've created a new regular expression entity, be sure to test it and make sure it is being recognized properly. Regular expressions override Conversational AI Cloud's normalization process, enabling you to fine-tune your recognition. However, if your project is using multiple regular expressions, it is possible for them to conflict with one another.
Regular expressions in Conversational AI Cloud use case-insensitive matching and are compliant with the ECMAScript standard, which JavaScript is based on.
You can test your regular expressions at regex101.com. Be sure to select ECMAScript as your flavor before testing the code.
If you have tested the code and it still doesn't work in Conversational AI Cloud, please send a ticket to Support.