VoiceXML 2.0 Grammars, Part II
One-of (or lists)
So far, our name grammar is not very useful because it only matches my first and last name. What we want to be able to do is to expand the list of possible names to include last names for people whose first name is Jonathan.
We can do this by creating a name rule that includes a rule reference to a list of first and last names.
$name = $firstName $lastName; $firstName = Jonathan | Jeff; $lastName = Eisenzopf | Franklin | Smith;
XML <rule id="Name"> <ruleref uri="#firstName"/> <ruleref uri="#lastName"/> </rule> <rule id="firstName"> <one-of> <item>Jonathan</item> <item>Jeff</item> </one-of> </rule> <rule id="lastName"> <one-of> <item>Eisenzopf</item> <item>Franklin</item> <item>Smith</item> </one-of> </rule>
For XML grammars, the <one-of> element may contain one or more <item> elements which contain a string (or token sequebce) for each alternate utterance.
The name grammar combines the firstName and then the lastName grammar via rule references to create a full name grammar, which is capable of recognizing a combination of first and last names.
The list of possible utterances that the namegrammar could match are as follows:
- Jonathan Eisenzopf
- Jonathan Franklin
- Jonathan Smith
- Jeff Eisenzopf
- Jeff Franklin
- Jeff Smith
The Speech Recognition Grammar Specification (SRGS) also includes some very useful operators that allow us to create complex word patterns that reflect natural language by defining grammar tokens and rule references as optional and/or repeatable.
Making Tokens Optional
Since callers may response to a prompt using different words, grammars must be able to define optional words. For example, when asked to say their name a caller might say any one of the following:
- "Um, my name is Jonathan"
- "My name is Jonathan Eisenzopf"
- "Um, yeah, well, I'm Jonathan Eisenzopf"
We need to be able to identify and capture all of the words that were uttered in addition to the name. Also, notice that the caller might only give us their first name, so the last name might also be optional.
ABNF $name = [um [yeah well]] ([my name is] | [I'm]) $firstName [$lastName];
XML <rule id="name"> <item repeat="0-1">um <item repeat="0-1">yeah well</item> </item> <one-of> <item repeat="0-1">my name is</item> <item repeat="0-1">I'm</item> </one-of> <ruleref uri="#firstName"/> <item repeat="0-1"> <ruleref uri="#firstName"/> </item> </rule>
For ABNF grammars, optional tokens are defined by surrounding them with a set of square brackets. Optional tokens can also be grouped. In the grammar above, the first set of outside brackets will optionally match "um" or "um yeah well". Following the first set of optional tokens, we have used parenthesis to group an optional list of alternative utterances. The grouping operator (parentheses) are only used in ABNF grammara because XML grammar are explicitly defined. This grammar phrase will optionally match "my name is" or "I'm" or nothing (because they're optional). Additionally, this grammar will match a first or first and last name because the $lastName grammar is surrounded by square brackets.
Unlike ABNF grammars XML grammars use the repeat attribute within the <item> element to define optional tokens and rule expansions. This is done by setting the value of the repeat attribute to 0-1 which means "zero or one." If you remember earlier, the <item> element can be used to encapsulate token sequences. In the example above, we enclose the item containing the token sequence "yeah well" within the item containing the token "um". The repeat attribute in both is set to 0-1 which means that the optional utterance "um" may be followed by the optional utterance "yeah well". Next, we match the optional list of utterances, "my name is" or "I'm" by enclosing them in a <one-of> element. Lastly, we include the firstName grammar with <ruleref> and make the lastName grammar optional by enclosing the associated <ruleref> in a <item> element whose repeat attribute is set to 0-1.
Zero or More
We can match zero or more instances of a token by appending the repeat operator after an ABNF sequence or by setting the repeat attribute of the <item> element to 0-.
ABNF $mood = I am very <0-> happy;
XML <rule id="mood">I am <item repeat="0-">very</item> happy </rule>
The repeat operator for ABNF grammars are enclosed with <>. The syntax for XML grammars is very similar to defining optional tokens.
The example grammar above will match any of the following utterances:
- "I am happy"
- "I am very happy"
- "I am very very very very very happy"
One or More
This is very similar to zero or more except that the token must occur at least one time in the utterance.
ABNF $mood = I am very <1-> happy;
XML <rule id="mood">I am <item repeat="1-">very</item> happy </rule>
So, the example above would match any of the following grammars:
- "I am very happy"
- I am very very very happy"
But it would not match:
- "I am happy"
Token Ranges and Exact Matches
We can also specify a range of instances or an exact number of instances that a token can occur in an utterance.
ABNF $eat = Please <1-5> eat your food;
$eat = Please <5> eat your food;
$eat = Please <5-> eat your food;
XML <rule id="eat"> <item repeat="1-5">Please</item> eat your food </rule>
<rule id="eat"> <item repeat="5">Please</item> eat your food </rule>
<rule id="eat"> <item repeat="5-">Please</item> eat your food </rule>
The first example would match one to five instances of the word please. The second example will match exactly five instances and the last example will match at least five instances of the word please.
If you have absorbed the content of this tutorial, then you will be able to create almost any VoiceXML 2.0 grammar that's required. In the next tutorial, we will learn some of the finer details of SRGS.
About Jonathan Eisenzopf
Jonathan is a Senior Partner of The Ferrum Group, LLC which provides speech IVR consulting, training, and professional services. Feel free to send an email to email@example.com regarding questions or comments about this or any article.
Page 2 of 2