Count on it
You’ve seen how to recognize a common first name (common to USA) in user’s input and even built a small talk utility to chat with the user and respond to his name. But that is hardly the most common thing the user might tell you. What is? Well, a number or a count or a series of numbers, of course.
Before we proceed, as in the previous tutorials, ensure that you have node and npm installed, create a new directory and initialize npm project there (accepting default values):
Note: if you don’t want to type in all the source code, you don’t have to. This tutorial is available as an npm module. Simply load the module, then copy all the files to your current directory:
If you decide not to install the tutorial module then don’t forget to install vui-ad-hoc-alexa-recognizer:
You get the numeric values from the user primarily using the built in NUMBER slot type (i.e. AMAZON.NUMBER or TRANSCEND.NUMBER). Before we dive right into using this slot type, it’s important to understand what is considered a number by vui-ad-hoc-alexa-recognizer. A number in vui-ad-hoc-alexa-recognizer is NOT what you would strictly consider a number in Mathematics. A number, instead, is any continuous numeric or count input. For instance “one two three” is considered a single number, rather than three separate consecutive numbers. There is a reason to this madness. And that reason is that in chat bots and especially voice services the users will often either spell out a whole number digit by digit or by a group of digits. For example, if your zip code is “12345” do you say “my zip code is twelve thousand three hundred forty five”? No, you don’t. You say “my zip code is one two three four five”. Or for a phone number, if your phone number is “(123) 456-7890” you might say “my phone number is one two three four fifty six seventy eight ninety”. So, vui-ad-hoc-alexa-recognizer will do its best to string these together to form a single number. It will also properly parse “proper” numbers, e.g. if the user says “one million five hundred thirty thousand eleven” vui-ad-hoc-alexa-recognizer will return “1530011”. Furthermore, since the text may be produced by all sorts of sources, both words and numbers will be accepted. Thus “my zip code is zero 1 2 thirty five” will produce 01235”. Also, both ordinal and cardinal numbers will be accepted. Thus “second” and “two” will both evaluate to 2.
Now, let’s add a few intents that can understand numbers. A zip code intent might be just a perfect first example. So, let’s update the intents.json:
and the updated utterances.txt file:
This is NOT the whole story - you now need to ensure that the number you get makes sense. If the user says “my zip code is 1” you have to reject this, as well as rejecting “my zip code is 123456789”. And you need to give the user feedback about the error. So, let’s update the talk.js to validate, store, and use zip codes:
Now, when you run the talk.js you’ll see this: