Sample grammar file revisited

Let’s review the sample file we looked at earlier (see Sample grammar file).

First, consider the header:

<?xml version="1.0" ?>
<grammar xmlns="http://www.w3.org/2001/06/grammar"
xmlns:nuance="http://voicexml.nuance.com/grammar"
version="1.0" xml:lang="en-US" root="YesNo"
tag-format="swi-semantics/1.0">

From the attributes of the <grammar> element, we know the language for the grammar is United States English (xml:lang="en-US"), and Nuance tag format will be used in any script <tag> elements (tag-format="swi-semantics/1.0").

Similarly, the header identifies "YesNo" as the root rule (root="YesNo"):

<rule id="YesNo" scope="public">
    <one-of>
        <item>
            <ruleref uri="#Yes"/>
            <tag>YesNo='yes'</tag>
        </item>
        <item>
            <ruleref uri="#No"/>
            <tag>YesNo='no'</tag>
        </item>
    </one-of>
</rule>

This rule is public in scope, so it could be referenced directly from another grammar file or a VoiceXML file. Since YesNo is the root rule for this grammar, it isn’t actually necessary to make it public: any reference to the grammar will go to YesNo automatically. However, in another context it might be necessary: for example, if YesNo were part of a generic “universals” grammar defining several unrelated grammar rules.

The rule itself consists of a simple <one-of> list with two options. Each of these options refers to a sub-rule which appears further on in the grammar file.

The first option refers to the Yes rule (<ruleref uri="#Yes"/>):

<!-- Subgrammar identifying Yes responses-->
<rule id="Yes">
    <one-of>
        <item>yes</item>
        <item>yeah</item>
        <item>right</item>
        <item>correct</item>
    </one-of>
</rule>

At runtime, Recognizer interprets these words in text into the corresponding pronunciation in United States English (which was specified in the header), and compares that pronunciation with the user’s spoken response. If the user’s utterance matches any of the four options, the word is recognized and the corresponding branch in the YesNo rule is activated:

        <item>
            <ruleref uri="#Yes"/>
            <tag>YesNo='yes'</tag>
        </item>

The code in the <tag> element assigns a value of "yes" to the YesNo variable.

Similarly, Recognizer converts words listed in the No rule to their US English pronunciations, and compares them with the user’s spoken response:

<!-- Subgrammar identifying No responses-->
<rule id="No">
    <one-of>
        <item>no</item>
        <item>nope</item>
        <item>wrong</item>
        <item>incorrect</item>
    </one-of>
</rule>

If the user utterance matches one of these four words, this activates the other branch of the YesNo rule:

        <item>
            <ruleref uri="#No"/>
            <tag>YesNo='no'</tag>
        </item>

This branch assigns a value of "no" to the YesNo variable.

If the user utterance doesn’t match an option from either rule with reasonable accuracy (for example, if the user says “maybe”), a “no match” result is returned to the application. The application may then ask the user to repeat the answer, or transfer to a live operator, or take some other action as specified in the voice application’s VoiceXML file.

Note that Recognizer assigns a confidence score to each recognition, indicating how accurate the match appears to be. The confidence score required for a recognition to be accepted can be set in the application VoiceXML file.

</grammar>

Like any other XML document, the grammar file must end with a closing delimiter.