Detecting information in expressions
In the previous tutorial, you learned about intents and expressions. In this section, we'll cover another important part of bot building: entities.
You will learn how to detect valuable information, mentioned by a user in an intent, using contextual entities. Entities are important pieces of information that are extracted from an expression.
There are four types of entities. You can find more information about them here: Entities/understanding-users/natural-language-processing-nlp/synonym-entities
In this tutorial we will focus on one entity type: contextual entities. Contextual entities use machine learning to identify entities in sentences by learning what type of word your entity is, where it's placed in the sentence, and what the specific context is.
You want to store these contextual entities in a separate variable so you can re-use them later on. Read more about the difference between entities and variables here. In the next tutorial, you will learn how you can ask explicitly for missing information.
Let's say we have an intent that tells us the user wants to book a train ticket. A few different expressions could be:
I want a train ticket
I need a ticket
Can I book a train ticket here?
This assures the bot dialog, linked to the corresponding intent, to be triggered when the user says one of these expressions.
But what would happen if the user says:
I want a train ticket to Amsterdam
I need to go to Antwerp tomorrow
Can I book a train ticket to Brussels please?
These expressions contain valuable information. We want to make sure we capture that information, in this case the destination and time, and save it as entities. We then have expressions with an entity in them.
We are now going to create a new intent with some expressions for booking a train ticket. Some of these expressions will contain a contextual entity, but some will not.
Step 9: Creating contextual entities
Not all users will immediately mention their destination, so let's make sure we train our intent without those specific entities as well:
Go to
NLP
>Intents
Click on
Add Intent
Add a new intent called
book train ticket
Add some simple expressions, like:
I want a train ticket
I need a ticket
Can I book a train ticket, please?
If you have trouble doing this, please read the previous tutorial.
Next, it's time to add a contextual entity.
Go to
Intents
and select yourbook train ticket
intentClick on
+ Add expression
to create a new expressionEnter an expression that contains an entity, for example:
I want to book a ticket from Brussels to Paris
Select
Brussels
in this sentence
![](https://docs-en.sinch.com/~gitbook/image?url=https%3A%2F%2Fgblobscdn.gitbook.com%2Fassets%252F-LLTwFwbOqJj4dDhg8Ju%252F-MkGxjU95gXOWRIT-00A%252F-MkGxxv8SOeFMIFvsE7c%252Fimage.png%3Falt%3Dmedia%26token%3D004b7ae3-6de6-400a-b0d8-39ff6e062252&width=768&dpr=4&quality=100&sign=730d983c&sv=1)
Click on the '+ entity' icon in the bottom right of the expression box to create a new contextual entity for 'Brussels'
![](https://docs-en.sinch.com/~gitbook/image?url=https%3A%2F%2Fgblobscdn.gitbook.com%2Fassets%252F-LLTwFwbOqJj4dDhg8Ju%252F-MkGxjU95gXOWRIT-00A%252F-MkGyCH7wsvcaDFBmzaV%252Fimage.png%3Falt%3Dmedia%26token%3D24d67668-28c8-4bac-8527-8cbde0db440a&width=768&dpr=4&quality=100&sign=50281821&sv=1)
Brussels is the location the user wants to depart from, so we will name this entity
origin
Type
origin
in theCreate new entity
field and click on 'Create new entity' to confirm
![](https://docs-en.sinch.com/~gitbook/image?url=https%3A%2F%2Fgblobscdn.gitbook.com%2Fassets%252F-LLTwFwbOqJj4dDhg8Ju%252F-MkGxjU95gXOWRIT-00A%252F-MkGyJxX0IFrM96GU68X%252Fimage.png%3Falt%3Dmedia%26token%3D77862567-721f-4704-917a-95eefc1eafcc&width=768&dpr=4&quality=100&sign=561f2f10&sv=1)
Brussels will be added to the list of possible values for the @origin variable
Do the same thing for
Paris
as a 'destination' entity
You will then have the following set-up for this expression:
![](https://docs-en.sinch.com/~gitbook/image?url=https%3A%2F%2Fgblobscdn.gitbook.com%2Fassets%252F-LLTwFwbOqJj4dDhg8Ju%252F-MkGxjU95gXOWRIT-00A%252F-MkGyV2A2i5z-N9Bjko2%252Fimage.png%3Falt%3Dmedia%26token%3D4ad66920-83d0-4a9b-935e-0a823ab6fa23&width=768&dpr=4&quality=100&sign=dd723671&sv=1)
We now save added the expression 'I want to book a ticket from @origin to @destination', where 'Brussels' is a value for @origin and 'Paris' is a value for the entity @destination.
Add some other values to the 'origin' and 'destination' entities in the expression field. These will be saved for all future expressions. You can add these in the 'Create new value' box and pressing enter.
Add more expressions that contain the entities origin and destination
Once you have added more Entity values, these will also show up in the menu Entities
> Contextual Entities
![](https://docs-en.sinch.com/~gitbook/image?url=https%3A%2F%2Fgblobscdn.gitbook.com%2Fassets%252F-LLTwFwbOqJj4dDhg8Ju%252F-MTtSJzh0IZmA5tu3CIt%252F-MTtUS1oKy_IOQmi5o0r%252Fimage.png%3Falt%3Dmedia%26token%3D8db10598-73a6-4d7f-9c52-359dfe861b2d&width=768&dpr=4&quality=100&sign=ca41beca&sv=1)
Now, let's add some more expressions to our
Book train ticket
intent. Some ideas for expressions:Can I book a train from Cologne to Brussels?
I need to be in Rotterdam
I need a train to London
I want to travel to Lyon
I want to buy a ticket from Moscow to Vladivostok
I need a ticket from New York to Baltimore
​
When typing a new expression, you can add entities and entity values in two ways:
Typing @ and then the name of the entity, for example @origin. You can add a new value in the box below with 'Create new value'
Selecting an entity value and clicking the +entity value button. For example, select 'Cologne', click the +button. This will result in 'Cologne' being changed into @origin and 'Cologne' will be a value of @origin
Make sure you retrain the NLP model by clicking the
Update NLP
button in the right upper corner.
This will now result some expressions for the Book train ticket
intent, and entity values, like so:
![](https://docs-en.sinch.com/~gitbook/image?url=https%3A%2F%2Fgblobscdn.gitbook.com%2Fassets%252F-LLTwFwbOqJj4dDhg8Ju%252F-MkGzbGNWNnq3K523Ulr%252F-MkGzyb8SE__tYj3dfE0%252Fimage.png%3Falt%3Dmedia%26token%3D39186f8a-7c8f-4d85-9303-cb1a0653322c&width=768&dpr=4&quality=100&sign=8ed81e35&sv=1)
To make it easier for you to add new expressions fast, we have built the expressions generator. Head over to this page for more information.
Step 10: Testing contextual entities
After we have retrained our model, let's see if its good enough to recognise the destination entity.
Go to
NLP
>Test
to open the testing consoleWrite 'I would like to go to Brussels from Amsterdam' as the expression to be tested
Click on
Test
You'll see that the entity gets recognized with a 99.93% confidence. The results will be different based on your training set. If the entity is not recognized correctly, you can add it here as a training expression immediately by clicking +Add expression
.
Make sure you retrain the NLP model before testing newly added expressions
![](https://docs-en.sinch.com/~gitbook/image?url=https%3A%2F%2Fgblobscdn.gitbook.com%2Fassets%252F-LLTwFwbOqJj4dDhg8Ju%252F-MkH0Xc2j9da2v7Beyly%252F-MkH15--aKH17tP9HGHH%252Fimage.png%3Falt%3Dmedia%26token%3Dad4d3c89-61ee-49c2-bdbf-cd49bc93d818&width=768&dpr=4&quality=100&sign=ea58664e&sv=1)
Now we know how to add intents, create expressions and entities, however we still need to create a conversation so the user can talk to Choo Choo and our bot replies accordingly. Let's add some bot messages in the next step.
Step 11: Using variables in messages
When a user says something containing an entity, and the entity is successfully detected, our tool will automatically store the entity as a variable for that specific user.
At the moment, when you test your bot, the user is stuck after giving the information about the ticket:
However, we do see some positive items, namely that the 'origin' and 'destination' are stored correctly as variables. You can see this by opening the debugger by clicking 'Debugger' (with the magnifying glass icon) in the emulator. In the 'Debugger' tab, you can scroll down and you see this:
![](https://docs-en.sinch.com/~gitbook/image?url=https%3A%2F%2Fgblobscdn.gitbook.com%2Fassets%252F-LLTwFwbOqJj4dDhg8Ju%252F-MU2TVa1AuMzEh_EOnYY%252F-MU2VeSiba5oLsepO84p%252Fimage.png%3Falt%3Dmedia%26token%3D44d3181f-b11c-4dd4-bc4d-55fc1fa86aeb&width=768&dpr=4&quality=100&sign=575b7d4a&sv=1)
So even though the sentence did give an error message, these entities are correctly recognized in the user input. This means the variable 'origin' is now saved with a variable value 'Brussels' and the variable 'destination' with the value 'Paris'. Also, in the ''NLP Result' tab we see that the intent was recognised correctly, that's great! Let's now work on removing that error message first.
The error message is caused by the fact that the intent Book train ticket
does not have a bot dialog linked to it. So even though it is correctly recognised, we are not telling the bot what to do when that intent is recognised.
We can change that by adding a new Bot message:
In the menu Bot dialogs, open the 'General' flow, create a bot dialog of the type 'Bot message'
book train ticket
. Open the 'NLP' tab, and choose theBook train ticket
intent in the 'Intent' dropdown.In the 'Settings' tab, name the bot message
Book train ticket
Add a new text message with the text "So you want to go to
{destination}
, I can help you with that!"Click
Create
to save this bot message.
To reuse the variable later on in the conversation, you can put it in between curly brackets like this: {variable_name}
When writing this message to the users, Chatlayer will automatically substitute {variable_name}
with the value of the variable. If the variable is empty, an empty space will be shown.
![](https://docs-en.sinch.com/~gitbook/image?url=https%3A%2F%2Fgblobscdn.gitbook.com%2Fassets%252F-LLTwFwbOqJj4dDhg8Ju%252F-MkH0Xc2j9da2v7Beyly%252F-MkH1PrdzbeTf9tu68sF%252Fimage.png%3Falt%3Dmedia%26token%3D7c8f42e4-4d7b-4558-b519-2fd051b6e447&width=768&dpr=4&quality=100&sign=ca14c9ad&sv=1)
We have now linked the Book train ticket
to this bot message, great job! This means that, when a user says something that triggers the Book train ticket
intent, this bot message will show.
Step 12: Testing entities in the emulator
Now that we have linked everything, we are ready to test if everything is configured correctly by using the emulator.
Open the emulator (the
Test your bot
tool on the bottom right)If needed, clear the last conversation by clicking 'Clear conversation' on the top right. This starts a new conversation
Enter "I want to go to Amsterdam" and click on submit
Open the debugger
In the tab 'NLP Result' you can now see if the entity was recognized correctly:
When creating new bot dialogs, you don't need to re-train the NLP
If you do not get the result as stated above, please check the following items in your bot:
If your entity is recognised by the NLP but doesn't show up in with
{destination}
it did not pass the threshold of 80%. Try adding that value to your entity and re-train your model, or choose another destinationIf you get 'Sorry I didn't understand that', double check if your intent is linked to the Bot message and this is saved correctly.
If your intent or expression is not recognised, try re-training your NLP again.
Now we already have a great start with linking the intent and giving a response to the expression the user says. However, we want more information from the user. Let's add more expressions and entities.
Step 13: Multiple entities in one expression
You can add as many entities as you want to one expression. For Choo Choo, we want to more information from the user than just the destination and origin, to give a complete train-booking experience. Let's add more contextual entities!
Go to the
Expressions
menuClick 'Add Expression'
Select the
Book train ticket
intent
Create the following expression:
I want to go from Antwerp to Brussels tomorrow at 9am in first class
And create the following entities:
origin: Antwerp
destination: Brussels
departure-date: tomorrow
departure-time: 9am
class: first class
If you are having trouble adding these, scroll back to step 9 in this tutorial to read all about it.
![](https://docs-en.sinch.com/~gitbook/image?url=https%3A%2F%2Fgblobscdn.gitbook.com%2Fassets%252F-LLTwFwbOqJj4dDhg8Ju%252F-MkH3SXFOwX2uPCsNuP2%252F-MkH525pu7JX-S69BFna%252Fimage.png%3Falt%3Dmedia%26token%3Dbf9e6fe8-55f3-472b-a331-de6789211512&width=768&dpr=4&quality=100&sign=77504e6&sv=1)
Additional suggestions for expressions
I need to be in Paris next Thursday
I need to be in New York on Friday
I want to go to Brussels on Monday
Friday I want to go from Antwerp to Amsterdam
I want to travel in second class from Ghent to Brussel on Friday
I want to travel in first class from Antwerp to Aalst on Thursday
I like to book a first class ticket from Aalst to Brussels at nine o'clock
Tomorrow I want to go from Antwerp to Brussels on the train from 9:00 in first class
Keep in mind that NLP techniques are probabilistic in nature. When you try to capture five expressions in one sentence, it might not be able to recognise all of them correctly. As a general rule of thumb, you can start to expect reasonable results for one entity when the NLP was given at least 30 expression to learn from.
Add more expressions with the new contextual entities to the intent. Ensure you have around 20 expressions for Book train ticket
in total.
Step 14: Missing entities
Let's test out your newly created expressions:
Update the wording in the book train ticket
dialog to correctly display the entities:
I need a ticket from Antwerp to Brussels tomorrow at 9am in first class
If you try to update your NLP and you get an error message about 5 example entities, it means you need to add more entity values to some of your newly created entities. To do so, go to NLP > Entities > Contextual entities and make sure that that entity has at least 5 values.
Uh oh, this isn't really what we expected. As you can see, not all variables were recognized correctly. So, what's the issue? Lets have a look at the NLP results in the debugger:
origin
, destination
, time_departure
and class
were found correctly, but only time_departure
and class
have a confidence score higher than 80%. Origin
and destination
score much lower, so they weren't processed as variables.
In the NLP treshold settings of our bot, we put a threshold of 80%, so anything underneath that won't be recognized correctly.
How can you fix this issue? By adding more expressions! Try adding more expressions and retrain your NLP model to see if the variables now show up correctly in the bot. You can also choose to lower the NLP score, but be careful as this can impact the overal accuracy of your bot in the long run.
Lesson recap
Your bot now has the following configuration:
3 intents with around 35 expressions in total
5 contextual entities
A bot message, linked to the
Book train ticket
intent, confirming the user input in the message
You now know how to:
Create contextual entities and entity values
Use variables in a bot message
Use multiple contextual entities in an expression
Test your input in the debugger
Not every user will give all the entities you need. In the next tutorial, you will learn how to check if a user has already provided certain information, and ask for what's missing.
Last updated