If you have gone into your conversation service since yesterday, you will find you have a new feature called System Entities. For the moment you only have number, percentage and currency recognition.
For this introduction I am just going to use @sys-number entity to make Watson do simple math.
First let’s train Watson about the math terms we are going to use. For this we are going to use intents.
Why intents and not entities? Hands down Intents will mean very little training for it to understand similar terms. Also as system entities, are also entities they can interfere with any logic you put in. I use the number “42” so as to not bias the classification to a particular number.
Next we go to entities and switch on the @sys-number entity.
Now for dialog, first we want to make sure what the person has said is a valid math question, if not we say we don’t understand. We do it with the following conditional statement.
intents.size() >0 AND intents[0].confidence < 0.30
This will ensure that the system only responds if it is confident to do so. Next we put another node in which checks to see if the system is unsure.
intents.size() >0 AND intents[0].confidence < 0.60 AND entities.size() == 2
Now you will notice we are using entities.size(). This is because the numbers are entities, and @sys-number doesn’t have the size() method. We want to make sure that the end user typed in two numbers before continuing.
Now what we have done that, the procedure is more or less the same for each action, so here is just the addition.
The answer is <? @sys-number[0].numeric_value + @sys-number[1].numeric_value ?>
This takes the first and second numeric value and adds them. While conversation will recognise numbers from text and take action on them, it won’t always do this. So we have to use the numeric_value attribute.
While mostly fine, there are issues you won’t be able to easily cater for. For example the importance of the numbers location.
Take for the example the two questions which are the same, but will give very different answers.
- What is ten divided by two?
- Using the number two divide up the number ten.
One way to solve this is just to create a second division intent which knows the numbers are reversed, but more likely you can solve this with message shaping.
You will find a similar issue though when you start to use the other system entities. For example if you have @sys-percentage active, then “20%” is not only a percent, but it is also a number. This makes it tricker when trying to read the entity or @sys-number stack.
For what comes back from conversation, you will see that the entity structure has changed.
"entities":[{ "entity":"sys-number", "location":[17,32], "value":"200", "metadata":{ "numeric_value":200 } }
Now you have a metadata field which you can get the numeric value from.
As always, here is a sample conversation script.
Great post. Is really helpful from learning prospective. As a beginner I only used Text and Context in my JSON watson response. This shows the other ways and the syntax. Also is it possible to write a function in my JSON Watson Response? Thanks again.
Hello Sodoherty, how can we do formula/equation recognition in Watson conversation?