Share via


Some Examples

This topic includes the following sections:

  • Simple example without <tag> elements
  • Simple example with <tag> elements
  • Two different ways to return values
    • Example returning a composite string
    • Example returning a multi-property object

Simple example without <tag> elements

A simple inline grammar permitting a match to "yes" or "no" might look like this:

<grammar mode="voice" xml:lang="en-US" version="1.0"
         root="yesOrNo">
   <rule id="yesOrNo">
     <one-of>
       <item>yes</item> 
       <item>no</item>
     </one-of>
   </rule>
</grammar>

Note

In this simple case, a tag is not required. Either "yes" or "no" is returned without a tag. Since a tag is not being used, the tag-format attribute is not required in the <grammar> tag.

In a VoiceXML application, this might look like:

<field name="response">
   <prompt>do you want to proceed</prompt>
   <grammar mode="voice" xml:lang="en-US"
            tag-format="semantics/1.0" version="1.0"
            root="yesOrNo">
      <rule id="yesOrNo">
         <one-of>
            <item>yes</item>  
            <item>no</item>
          </one-of>
       </rule>
   </grammar>
   <filled>....</filled>
   <noinput> </noinput>
   <nomatch> </nomatch>
</field>

If the speech recognition engine finds a match between the speaker’s utterance and one of the two items, that item is assigned to the VoiceXML <field> variable, response. If no match occurs, there is no assignment and a nomatch or noresponse event is thrown.

Simple example with <tag> elements

In the real world, a speaker might say "yes," "yeah," "yep," yup", "un huh," "yay yus" (Texan), "no," "nope," "nah", or "uh uh." When a number of different speaker utterances might mean the same thing, and yet you want to return only one value ("yes" or "no"), semantic interpretation <tag> elements must be used.

When tag-format = "semantics/1.0":

<field name="response">
   <prompt>do you want to proceed</prompt>
   <grammar   mode="voice" xml:lang="en-US" 
              tag-format="semantics/1.0"
              version="1.0" root="yesOrNo">
      <rule id="yesOrNo">
         <one-of>
            <item>
              <one-of>
                 <item>yes</item>
                 <item>yeah</item>
                 <item>yep</item>
                 <item>yup</item>
                 <item>un huh</item>
                 <item>yay yus</item>
              </one-of>
              <tag>out="yes";</tag>
            </item>
            <item>
              <one-of>
                 <item>no</item>
                 <item>nope</item>
                 <item>nah</item>
                 <item>uh uh</item>
               </one-of>
               <tag>out="no";</tag>
            </item>
         </one-of>
      </rule>
   </grammar>
   <noinput> </noinput>
   <nomatch> </nomatch>
</field>

This grammar allows a match with either a positive or a negative response (but not both). Further it allows only one of the positive responses to match or only one of the negative responses to match. If a positive response matches, the Rule Variable is assigned the value "yes" irrespective of which positive utterance was matched. If a negative response matches, the Rule Variable is assigned the value "no," irrespective of which negative utterance was matched.

Two different ways to return values

Many grammars need to return multiple values (sometimes referred to as multi-slots). There are two alternative ways to return these multiple values:

  • Compose a string that includes all the values (first example below). This is the way Tellme builds most of its own grammars.
  • Return an ECMAScript object with multiple properties (second example below).

Example returning a composite string

<field name="cityAndStateVar">
   <prompt>What is your city and state</prompt>
   <grammar mode="voice" xml:lang="en-US" 
            tag-format="semantics/1.0"
            version="1.0" root="cityAndState">
      <rule id="cityAndState">
         <ruleref uri="http://www.ourgrammars.com/usCities.grxml"/> 
         <tag>out=rules.latest( )</tag>
         <ruleref uri="http://www.ourgrammars.com/usStates.grxml"/>
         <tag>out += ", " + rules.latest( )</tag>
      </rule>
   </grammar>
</field>

After the usCities grammar is invoked, rules.latest() contains the matched city. The first tag places this in the Rule Variable, out. Next, the usStates grammar is invoked and the name of the matched state replaces the contents of rules.latest(). The second tag adds a comma and space to the out variable and then adds the new contents of rules.latest(), which is the matched state. The final out variable that is placed in the cityAndStateVar variable by the VoiceXML application is then the string "city, state".

Example returning a multi-property object

<field name="cityAndStateVar">
   <prompt>What is your city and state</prompt>
   <grammar mode="voice" xml:lang="en-US" 
            tag-format="semantics/1.0"
            version="1.0" root="cityAndState">
      <rule id="cityAndState">
         <ruleref uri="http://www.ourgrammars.com/usCities.grxml"/> 
         <tag>out.city=rules.latest( )</tag>
         <ruleref uri="http://www.ourgrammars.com/usStates.grxml"/>
         <tag>out.state=rules.latest( )</tag>
      </rule>
   </grammar>
</field>

In this case, the VoiceXML application places out in the cityAndStateVar variable. out is an ECMAScript object with two properties, city and state, so that cityAndStateVar is also an object—its properties, cityAndStateVar.city and cityAndStateVar.state are available to the VoiceXML application.

Note

The "comprehensive example" in the next chapter returns a multi-property ECMAScript object also.