VoiceXML Adventure Game - Mother of Perl | 2 | WebReference

VoiceXML Adventure Game - Mother of Perl | 2

To page 1current pageTo page 3To page 4To page 5To page 6To page 7
[previous] [next]

VoiceXML Adventure Game

Creating a Grammar for DTMF Input

Grammars

Before we proceed any further, we need to talk a bit more about grammars. They are a very important part of any VoiceXML document. A grammar is a set of expressions that define what DTMF tones and spoken input will be recognized by the system. These grammars are limited to what you program them to recognize. Depending on where you place the grammar, the scope can be the whole document, a form, or a field within a form. The Tellme grammar syntax uses the Nuance Grammar Specification Language (GSL).

It's not too difficult to define a simple grammar that allows a user to make a selection via DTMF tones. Let's define a simple grammar that provides our users with the option to open one of three doors in our adventure.

  <grammar><![CDATA[
          [
            [dtmf-1] {<option "door1">}
            [dtmf-2] {<option "door2">}
            [dtmf-3] {<option "door3">}
          ]
        ]]>
  </grammar>

First, grammars are placed within the grammar element. You should also surround the grammar with a CDATA section, which tells the XML parser not to process the text inside. In the grammar above, we have defined three options. Each option is defined by a grammar statement. The statement consists of an expression to be matched, and the corresponding value to return when a match occurs.

The expression used to match a digit is: dtmf-$ where $ is the number we want to match. For example, dtmf-1 would be matched if a user presses 1 on their telephone keypad.

The second part of the expression defines the value that will be returned when we get a match. For example, {<option "foo">} will return the text foo when the user input matches the first part of the expression.

Next we're going to apply this simple grammar to a real VoiceXML document.

Getting DTMF Input

The first version of our adventure game below will only accept DTMF input. The user can select 1, 2, or 3, which corresponds with one of the three doors.

An example user session follows:

Tellme: You are in a small room with three doors.

Tellme: To open the first door, press 1.

Tellme: To open the second door, press 2.

Tellme: To open the third door, press 3.

User: (pressed 1)

Tellme: You see a large hungry monkey.

Now, let's take a look at the VoiceXML document.

Example 1.

<?xml version="1.0"?>
<vxml version="1.0"> 
  <form> 
    <field name="answer"> 
      <grammar><![CDATA[
          [
            [dtmf-1] {<option "door1">}
            [dtmf-2] {<option "door2">}
            [dtmf-3] {<option "door3">}
          ]
        ]]>
      </grammar> 
      <prompt><audio>You are in a small room with three doors.</audio>	
      <pause>300</pause> <audio>To open the first door, press 1.</audio>
      <pause>300</pause> <audio>To open the second door, press 2.</audio>
      <pause>300</pause> <audio>To open the third door, press 3.</audio> </prompt> 
    </field> 
    <filled>
      <result name="door1"> 
        <audio>You see a large hungry monkey.</audio>
	       <reprompt/>
      </result>
      <result name="door2">
        <audio>You see another room with three doors, a man, and his monkey.</audio>
        <reprompt/>
      </result>
      <result name="door3">
        <audio>You see a man scratching his monkey.</audio>
        <reprompt/> 
      </result> 
    </filled> 
  </form>
</vxml>

For more information on using the filled and result elements, please see Page 3 of the first part of this tutorial. You might have also noticed the pause element in the example above. Using the element forces Tellme.com to pause between prompts. In this case, we want to stop for 300 milliseconds between options to duplicate the pause that would occur in natural speech.


To page 1current pageTo page 3To page 4To page 5To page 6To page 7
[previous] [next]

http://www.internet.com

Produced by Jonathan Eisenzopf
All Rights Reserved. Legal Notices.
Created: March 8, 2001

URL: http://www.webreference.com/perl/tutorial/21/2.html