VoiceXML Developer Series: A Tour Through VoiceXML, Part VI
In the last edition of the VoiceXML Developer, we created a pizza pie ordering system for Joe's Pizza Palace, which utilized the GSL grammar format. In this edition, we're going to continue our focus on grammars by examining the other widely used VoiceXML 1.0 grammar format, JSFG.
JSGF stands for Java Speech Grammar Format and was developed by Sun Microsystems. While the top three voice portal providers, Tellme, BeVocal, and Voxeo, all use the Nuance GSL format, the IBM Voice Server uses JSGF for grammars. As with GSL, VoiceXML can refer to a grammar in an external file or specify the grammar in the VoiceXML document inside the <grammar> element. In fact, both GSL and JSGF are rule based grammars. The basic syntax of a JSGF grammar rule is:
<rule> = token_string;
where the rule name is surrounded by < and > characters and the tokens representing the input to match is contained on the right hand side of an equal sign followed by a semi-colon.
You can directly embed JSGF grammars within the <grammar> element. Below is an example of an inline grammar within a VoiceXML document that will match the utterance, "I like pie":
<grammar type="text/jsgf">I like pie</grammar>
As with GSL, when including a grammar within a form <field> element, the grammar will return the match, and set the value for the field.
<?xml version="1.0"?> <vxml version="1.0"> <form id="clown"> <block name="welcome">Hi, I'm commie the clown.</block> <field modal="false" name="greeting"> <grammar type="text/jsgf">hi | hello | howdy | yo</grammar> </field> </form> </vxml>
A JSGF file starts with a JSGF declaration followed by the name of the grammar in the file. The file can contain multiple rules.
#JSGF V1.0 grammar greeting; <greeting> = hi | hello | howdy | yo;
The example above contains the same grammar as the previous inline grammar example except for the fact that it is contained in an external file. The modified VoiceXML file, which refers to the external grammar, is below:
<?xml version="1.0"?> <vxml version="1.0"> <form id="clown"> <block name="welcome">Hi, I'm commie the clown.</block> <field modal="false" name="greeting"> <grammar type="application/x-jsgf" src="greeting.jsgf" /> </field> </form> </vxml>
Using JSGF lists
We can define a list of selections by separating them with the | character.
<grammar type="text/jsgf">small | medium | large</grammar>
In the example above, the grammar will match small, medium, or large. We can also make a word optional by surrounding it with a pair of square brackets:
<grammar type="text/jsgf">small | medium | [real] large</grammar>
The example above will match, small, medium, large, or real large. If we were to reuse the Joe's Pizza Palace example from the last article, and the grammar above was to match the pizza size, we may want to allow users to provider alternate utterances for the choices. We can do this by grouping a token with a set of parenthesis.
<grammar type="text/jsgf"> (small|little)|(medium|regular)|[real](large|big) </grammar>
Now a customer can say little or small, medium or regular, and large or big or real large or real big.