Introduction to Voice XML Part 3: Voice XML Grammars
- Voice XML Grammar Options
- XML Grammars
- Inline versus External Grammars
- Going External
- Summary
- Resources
In grade school, learning the rules of grammar is often painful but pays off when one needs to write and speak coherent sentences. When building voice applications, grammars play a similar role as the basis for user communication. Understanding the rules of voice grammars opens the door to constructing robust voice applications that model your users’ speech patterns and improves the quality of the voice interface.
In this article, we’ll look at how to take your Voice XML applications to the next level of sophistication through the use of grammars. A grammar defines the words and word phrases that a user can say at any point in a conversational dialogue. Working in conjunction with your voice server, the words and phrases defined in a voice grammar are processed by the server’s speech recognition engine to identify permissible sequences of words and to attach meaning to them.
The art of grammar authoring is critical to the development of robust, usable telephony speech applications. When an application grammar accurately models the speech patterns of callers, everyone wins. The usability of the application is enhanced, and caller satisfaction increases. With growth in the market for speech technology, grammar authoring is emerging as an important area of specialization.
Grammars are built around tokens and rules. Tokens are the basic units of a grammar, either spoken words or touch-tone digits (dtmf). Rules are what we use to string together tokens (or other rules) to build phrases that a Voice XML engine can recognize and respond to.
As an example, imagine a voice application linked to your computerized home control system. You’ve been working late and are heading home. You dial your Voice XML app and say this:
Hey, turn on the Jacuzzi. I’ll be home at 9 P. Set the temp to 102.
A well-architected grammar should also be able to understand something like this:
Yo, Jacuzzi on, heat it to 102, be home in 2 hours.
If the grammar is set up correctly, the voice server should parse either of the above phrases and perhaps generate some XML that looks like this:
<command> <device>Jacuzzi</device> <temp>102</temp> <time>2100</temp> </command>
It can notify your home server using an HTTP get request:
http://myhomeserver.com?action=on&device=jacuzzi&time=2100&temp=102
Then, when you arrive home, just step into the heated Jacuzzi that’s ready for you after a hard day at work.
Voice XML Grammar Options
There is no one standard for writing grammars; different providers typically support several options. Currently, there are four major grammar formats in use today, described in the following sections.
GSL (Nuance)
GSL is the format used by the popular Nuance advanced speech recognition system (ASR). GSL has been incorporated by Voice XML development platforms such as BeVocal Café, Nuance, Tellme Studio, and others. GSL allows rule composition using constructs such as optional, sequences, alternatives, and probabilities. Since GSL has been around for a while, there are numerous web sites where you can find examples and tutorials on GSL.
Since GSL is quite compact and easier to write than XML grammars, many developers prefer GSL. The first examples in this article use GSL.
Java Speech Grammar Format (JSGF)
JSGF is Sun Microsystems’ platform and vendor-independent format for use in speech recognition. JSGF is part of the Java Speech API initiative and is now available as a W3C Note. JSGF leverages the Java programming language to provide naming conventions for grammar and package names.
Similar to the Java programming language, JSGF supports the convenient concept of documentation comments. With Sun’s move into the Java phone cellular space, expect to see JSGF in wide use.
Speech Recognition Grammar Specification (SRGS)
The Speech Recognition Specification (SRGS) is part of the W3C’s effort to develop standards to allow speech-based interaction with Web-based applications. In setting up SRGS, the W3C actually specified two grammar formats: an XML-based syntax (with an associated DTD) and a non-XML syntax called ABNF.
The ABNF format is based on Backus-Naur Form (BNF), which has been used for decades to specify context-free grammars for computer languages and protocols. SRGS supports rule definitions and, as part of the current working draft, an XSLT stylesheet that can convert the XML grammars to their corresponding ABNF form.
The choice between the non-XML and XML formats is up to the developer, and depends on what is supported by your voice server. While non-XML grammars may be easier to write, there are two strong arguments for using XML. The first is the W3C Voice XML 2.0 Recommendation which requires compliant implementations to support the XML format and only optionally support ABNF.
The second and most compelling reason to become familiar at least with the XML grammar is the existence of an associated DTD and XML Schema that, when used with an XML editor, makes the task of grammar writing less error-prone. Even if you write an XML grammar with just a text editor, you can always run a quick validity check and reduce potential lost time trying to eyeball a grammar error.
So let’s jump in and take a look at grammar construction by working through a sample application that asks users for a name and returns a phone extension. It should be noted that these examples are a bit contrived to illustrate grammar construction. All the lookup takes place within the Voice XML document. While this works fine for a very small company, in practice you would want to pass the user request to a server and return some Voice XML that reported the extension.
With that in mind, let’s start our grammar explorations by looking at the popular and widely supported Nuance GSL grammar. After we get our feet wet with GSL, we’ll take a look at how to do similar things with the SGRS XML grammar.
Example 1: Working with Grammars
Let’s begin by looking at some sample dialogues that can be used to retrieve the telephone extension of employees.
Example 1 Dialogue:
S: Whose extension would you like me to look up?U: Bob Kirin.
S: Their extension is 8 3 0 6.
S: Whose extension would you like me to look up?
U: Bob.
S: Their extension is 8 3 0 6.
S: Whose extension would you like me to look up?
U: Kirin.
S: Their extension is 8 3 0 6.
Here the user can use first name, last name, or both to get the extension.
Listing 1 Simple Voice XML form with inline grammar
1 <vxml version="2.0" xmlns=’http://www.w3.org/2001/vxml’ xmlns:xsi=’http://www.w3.org/2001/XMLSchema-instance’ xsi:schemaLocation=’http://www.w3.org/2001/vxml http://www.w3.org/TR/voicexml20/vxml.xsd’> 2 <form id="F1"> 3 <field name="worker"> 4 <prompt> 5 Whose extension would you like me to look up? 6 </prompt> 7 <grammar type="application/x-nuance-gsl" mode="voice"> 8 <![CDATA[ 9 [ 10 [bob kirin] {<worker "8 3 0 6">} 11 [roger whitney] {<worker "9 2 3 4">} 12 [wilma rudolph] {<worker "8 1 2 1">} 13 [customer service] {<worker "6 2 3 4">} 14 ] 15 ]]> 16 </grammar> 17 <filled > 18 <prompt> 19 Their extension is <value expr="worker"/> 20 </prompt> 21 </filled> 22 </field> 23 </form> 24 </vxml>
Listing 1 shows the complete Voice XML document that supports the dialogues for this example. It contains a form with a field. Within that field are a prompt (what the user first hears), a grammar that determines acceptable input, and a filled element that is executed when there is a match between spoken input and the grammar element.
If there is no match, the default no-match handler reports a do-not-understand message and returns the user to the form.
Let’s deconstruct the Voice XML in Listing 1:
- Line 3: The field is called "worker". Our goal is to determine which worker the user is interested in.
- Line 7: The grammar begins. Note that the grammar element is a subelement of field. The type attribute specifies the kind of grammar (Nuance GSL), and the mode attribute specifies voice (dtmf is also an option).
- Line 8: The grammar is enclosed in a CDATA section. Most non-XML grammars are wrapped inside CDATA since they might include characters that cause trouble for an XML parser.
- Line 10: The [] defines what the speech recognizer is looking for (i.e. Bob Kirin). The {} specifies the meaning when Bob Kirin is spoken. In this case, the value for the field worker is set to the string "8 3 0 6". We include spaces here so that the speech generator will pronounce each number individually.
- Line 17: When a match is made against one of the workers, the filled element is executed. In this case, we use a prompt to report the worker’s extension, which is made available by retrieving the value of the field variable worker.
Example 2: Alternate Options
In Example 1, users have the option of speaking the complete name or just the first or last name. But if two individuals have the same first or last name, this creates a problem.
To force a sequence of word tokens to be spoken, grammar writers can enclose the required words in parentheses; for example: (roger rabbit). Listing 2 illustrates a grammar that will support the following dialogue.
Example 2 Dialogue:
S: Whose extension would you like me to look up?U: Roger.
S: I did not understand what you said.
U: Roger Rabbit.
S: Their extension is 9 2 8 8.
S: Whose extension would you like me to look up?
U: The big fat boss.
S: Their extension is 8 3 0 6.
S: Whose extension would you like me to look up?
U: Kirin.
S: Their extension is 8 3 0 6.
Listing 2 A GSL grammar with options
1 <grammar type="application/x-nuance-gsl" mode="voice"> 2 <![CDATA[ 3 [ 4 [bob kirin (the big fat boss)] {<worker "8 3 0 6">} 5 [(roger whitney) whitney] {<worker "9 2 3 4">} 6 [wilma rudolph] {<worker "8 1 2 1">} 7 [(roger rabbit) rabbit] {<worker "9 2 8 8">} 8 [customer service] {<worker "6 2 3 7">} 9 ] 10 ]]> 11 </grammar>
Some details about Listing 2:
- Line 4: We can ask for bob, kirin, bob kirin, or the big fat boss.
- Lines 5 and 7: To distinguish workers with the same first names, we surround the full names with ().
Example 3: Wildcards
The wildcard operators provide additional control over the options for spoken input. Similar to regular expression and DTD syntax, GSL supports the following wildcard style operators:
- Zero or more occurrences: *
- One or more occurrences: +
- Optional: ?
For example, consider the following alternative ways of getting the numbers.
Example 3 Dialogue:
S: Whose extension would you like me to look up?U: Bob.
S: Their extension is 8 3 0 6.
...
U: The big fat boss.
S: Their extension is 8 3 0 6.
...
U: big boss.
S: Their extension is 8 3 0 6.
...
U: boss.
S: Their extension is 8 3 0 6.
S: Whose extension would you like me to look up?
U: Customer Service.
S: Their extension is 6 2 3 7.
...
U: Help.
S: Their extension is 6 2 3 7.
...
U: I need help.
S: Their extension is 6 2 3 7.
...
U: I really really really really need help.
S: Their extension is 6 2 3 7.
Listing 3 shows the grammar for the Example 3 dialogue.
Listing 3 A GSL grammar with wildcards
1 <grammar type="application/x-nuance-gsl" mode="voice"> 2 <![CDATA[ 3 [ 4 [bob kirin (?the ?big ?fat boss)] {<worker "8 3 0 6">} 5 [(roger whitney) whitney] {<worker "9 2 3 4">} 6 [wilma rudolph] {<worker "8 1 2 1">} 7 [(roger rabbit) rabbit] {<worker "9 2 3 8 8">} 8 [customer service (?i *really ?need help) ] {<worker "6 2 3 7">} 9 ] 10 ]]> 11 </grammar>
Note the following for Listing 3:
- Line 4: Using ?the ?big ?fat makes each word optional.
- Line 8: Using *really lets users say really in sequence as many times as they like, including none.
As you can see, the Nuance GSL grammar is straightforward and relatively easy to understand. Let’s now move on to the XML grammar defined as part of the W3C’s SGRS.