Share via


Creating the Grammars

  Microsoft Speech Technologies Homepage

The ColorChooser control must recognize a user's color selection. It must also recognize a user's response to a prompt that asks whether the color name that the application recognized is the correct color name. These tasks require three grammars:

  • A grammar to recognize the user's full response to the initial prompt requesting the user to select a color
  • A grammar to recognize color names
  • A grammar to recognize the user's response to a prompt for confirmation

The control generates these grammars using three methods:

  • A main method named QuestionGrammar
  • A helper method named ColorRule
  • A another main method named ConfirmGrammar

For the sake of simplicity for this example, the grammars recognize only blue, yellow, green, and red.

Generating the ColorRule and QuestionGrammar Grammars

The main method, QuestionGrammar, is dependent upon the helper method ColorRule. Because of this dependency, this section presents the code for ColorRule first. The code declares a string constant within the ColorChooser class for each of the four color names.

private const string _blue              = "blue";
private const string _green             = "green";
private const string _yellow            = "yellow";
private const string _red               = "red";

The ColorRule method generates the grammar rule that recognizes the color names.

private string ColorRule()
{
    string rule = "<rule id=\"color\">" +
                  "<one-of>" +
                  "<item>"     + _blue     + "</item>" +
                  "<item>"     + _yellow   + "</item>" +
                  "<item>"     + _green    + "</item>" +
                  "<item>"     + _red      + "</item>" +
                  "</one-of>" +
                  "</rule>";
    return rule;
}

The QuestionGrammar method generates the grammar that recognizes the answer to the main question. QuestionGrammar calls the ColorRule method to generate the list of color names that can be a part of the answer.

private string QuestionGrammar()
{
    string cfg = "<grammar root=\"top\" xml:lang=\"en-US\" version=\"1.0\" xmlns=\"http://www.w3.org/2001/06/grammar\" tag-format=\"semantics-ms/1.0\">" +
                
                 "<rule id=\"top\" scope=\"public\">" +
                 OptionalPreAnswerRuleRef +
                 "<ruleref uri=\"#color\"/>" +
                 "<tag> $.color = $$ </tag>" +
                 OptionalPostAnswerRuleRef +
                 "</rule>" +

                ColorRule() +

                "</grammar>";
    return cfg;
}

Generating the ConfirmGrammar Grammar

The ColorChooser control uses the ConfirmGrammar method to generate a grammar that recognizes the answer to the confirmation question. However, generating a grammar that confirms a selection is not as simple as generating a grammar that recognizes only "yes" and "no." Affirmative and negative responses can be expressed using a number of words including: "yeah," "yep," "ok," "sure," "right," "correct," "nope," "negative," "nah," and others.

In order to simplify speech application development, the Speech Application SDK includes a voice mode grammar library. The library contains rulesets for recognizing commonly used data such as dates, times, and numbers. Rules for common responses that are synonymous with "yes" and "no" are already coded in the YesNoCancel rules.

In order to use the library, the ColorChooser code first declares a private string to contain the name of the .cfg file containing the grammar library.

#region Private Members

    private string _library                 = "1033/cmnrules.cfg";

Next, to encode the path to the library, the ConfirmGrammar method calls the GetAbsolutePath method to obtain the path to the library.

private string GetAbsolutePath(string relativePath)
{
    if(Context == null)
    {
        return relativePath;
    }
    return new Uri(Context.Request.Url, relativePath).AbsoluteUri;
}

The ConfirmGrammar method first creates a string variable containing the path to the YesNoCancel ruleset in the voice mode grammar library, and then generates the grammar rule that recognizes the user's affirmative or negative response. Like QuestionGrammar, this method also calls the ColorRule.

private string ConfirmGrammar()
{
    string cfgLibrary = GetAbsolutePath(LibraryRelativeLocation + _library);

    string cfg= "<grammar root=\"top\" xml:lang=\"en-US\" version=\"1.0\" xmlns=\"http://www.w3.org/2001/06/grammar\" tag-format=\"semantics-ms/1.0\">" +
                
                "<rule id=\"top\" scope=\"public\">" +
                "<one-of>" +
                "<item>" +
                "<ruleref uri=\"" + cfgLibrary + "#Yes\"/>" +
                "<tag> $.accept = true; </tag>" +
                "</item>" +
                "<item>" +
                "<ruleref uri=\"" + cfgLibrary + "#No\"/>" +
                "<tag> $.deny = true; </tag>" +
                "</item>" +
                
                "<item>" +
                "<item repeat=\"0-1\">" +
                "<ruleref uri=\"" + cfgLibrary + "#No\"/>" +
                "</item>" +        
                OptionalPreConfirmRuleRef +
                "<ruleref uri=\"#color\"/>" +
                "<tag> $.color = $$ </tag>" +
                OptionalPostConfirmRuleRef +
                "</item>" +
                "</one-of>" +
                "</rule>" +
    
                ColorRule() +

                "</grammar>";
    return cfg;
}

See Also

Creating ColorChooser: A Custom Application Speech Control | Creating the Class and SemanticItem Property | Implementing the Dialogue Flow | Generating Prompts | Creating a Client-Side Object and SALT