VoiceXML 2.0 Grammars, Part II
In Part II of our introduction to VoiceXML 2.0 grammars, we will learn how to use tokens, rules and operators to create grammars that match natural utterances.
Grammars match spoken words or touch-tone digits. These words are referred to as tokens. The simplest grammars are token strings composed of one or more words. For example, we might create an inline grammar that matches my first and last name.
ABNF <grammar>Jonathan Eisenzopf</grammar>
XML <grammar> <token>Jonathan Eisenzopf</token> </grammar>
<grammar> <item>Jonathan Eisenzopf</item> </grammar>
Inline grammars are embedded within VoiceXML code instead of external files. By default, words that aren't marked with a special grammar symbol (ABNF) or are included in a grammar element (XML) are treated as tokens. Therefore, we could have excluded the <token> element in the XML grammar above and it would have performed exactly the same.
In the example above, my full name is a sequence of tokens (Jonathan followed by Eisenzopf). The Automatic Speech Recognition (ASR) engine will only recognize my full name in the specified order.
I've also included an optional method for encapsulating a sequence of tokens using the <item> element. The end result is identical to using the <token> element, however, I wanted to show it to you now because we will be using the <item> element later in the tutorial.
Grammars often consist of sub-grammars. This allows us to define re-usable grammar components, such as a phone number. These sub-grammars are included into other grammars via a rule reference. A rule reference can point to a local grammar, or an external grammar rule contained in another file, or even on another server on the Internet. For example, we may want to create a sub-grammar that contains all possible first names and include it in a top-level grammar:
ABNF <grammar>$firstName Eisenzopf</grammar> XML <grammar> <ruleref uri="#firstName"/> Eisenzopf </grammar>
The local sub-grammar named firstName is being referenced in the grammar above. The sub-grammar is local because it's contained in the same grammar file, however, we could also have referenced the sub-grammar if it were in a different file by specifying the full URI of the grammar file:
ABNF <grammar>$(http://grammars.com/name.gram#firstName) Eisenzopf</grammar> XML <grammar> <ruleref uri="http://grammars.com/name.grxml#firstName"/> Eisenzopf </grammar>
Grammar files consist of one or more grammar rules. Each rule is defined by a unique name. Rule names cannot contain a period, colon, or hyphen character and cannot be named NULL, VOID, or GARBAGE. Rule names are also case sensitive. To continue expanding on the name example above, let's create the rule referenced above as firstName.
ABNF $firstName = Jonathan;
XML <rule id="firstName">Jonathan</rule>
The unique rule name in ABNF grammars is defined by the character string to the right of the $ character. This particular rule is very simple in that it will only match my first name, Jonathan.
XML grammars define rules using the <rule> element. The unique rule name is contained in the id attribute.
Grammar rule scope
By default, VoiceXML 2.0 grammar rules are private. This means that rules can only be referenced within the same grammar file. If we wanted a VoiceXML dialog or another grammar to reference a grammar rule, we need to specifically scope it as public.
ABNF public $firstName = Jonathan;
XML <rule id="firstName" scope="private">Jonathan</rule>
To scope a grammar rule as public or private in ABNF grammars, pre-pend (public or private) to the rule definition.
For XML grammars, include the scope attribute to the <rule> element where the value is either public or private.