Partilhar via


Speech Grammar Editor Tips

  Microsoft Speech Technologies Homepage

This topic contains tips to consider when using the Speech Grammar Editor. For more tips, see issues of the Microsoft Speech Technologies Newsletter on the Microsoft Speech web site.

Building and Using Dynamic Grammars

When building ASP.NET speech applications, the author may want to incorporate dynamic content into their grammars. In most cases, this is done to make the application more flexible when the content is not known ahead of time or is expected to change. For instance, in a retail application in which the user chooses an item from the “specials deals of the day,” the author can build a dynamic grammar that will pull the specials for that particular day.

Dynamic grammars can be implemented as normal .aspx pages. The author specifies the static content of the grammar declaratively, and can generate the dynamic parts in a code-behind file. For example, the file MyGrammar.aspx might look like the following sample.

<grammar version="1.0" xmlns="http://www.w3.org/2001/06/grammar"
 lang="en-US" tag-format="semantics-ms/1.0">

<!-- Yes No Cancel -->

<!-- $YesNo._value: string -->
  <rule id="YesNo" scope="public">
    <example> yes </example>
    <example> sure </example>
    <example> no </example>
    <example> I don't think so </example>
    <one-of>
      <item> <%# ItemContent %> </item>       
      <item> <ruleref uri="#No"/> </item>
    </one-of>
    <tag> $ = $$ </tag>
  </rule>
</grammar>

In the code-behind file, the author can use the Page.DataBind method in the Page_Load event handler, and create a ListContents property as in the following sample.

protected string ItemContent 
  {
    get 
    {
      string s = “”;
      // build 's' into the dynamic bit
      // e.g., s += “      <item> <ruleref uri="#Yes"/> </item>\n”
      return(s);
    }
  }

This grammar and rule are referenced like any others, as shown in the following sample <ruleref> tag.

<ruleref name=“MyRule” url=“MyGrammar.aspx” />

However, it is important to note that the Speech Grammar Editor will not load .aspx files, and will therefore be unable to validate any grammars that include this grammar as a child.

Using Carrier Grammar Rules

The application controls contain built-in grammars to recognize specific data. For example, the Date application control recognizes dates and the NaturalNumber control recognizes numbers. However, depending on the question, users' answers may include more than just dates or numbers. For example, imagine that the question asked is: “When do you want to fly?” Users may reply with “I'd like to fly on March the second please.” The phrase in which the date is embedded, in this case “I'd like to fly on” and the word “please.” is called a carrier phrase. The carrier phrase does not contain important semantic data. Authors can specify which carrier phrases the application control should expect. First, write the grammar for the carrier phrase, as in the following example.

<grammar version="1.0" xmlns="http://www.w3.org/2001/06/grammar"
 lang="en-US" tag-format="semantics-ms/1.0" mode="voice">
  <rule id=“Rule1” scope=“public”>
    <item>I'd like to</item>
    <one-of>
      <item>fly</item>
      <item>depart</item>
    </one-of>
    <item repeat=“0-1”>on</item>
  </rule>
  <rule id=“Rule2” scope=“public”>
    <item>please</item>
  </rule>
</grammar>

Then, point the Date control to those rules via the CarrierGrammar, PreAnswerCarrierRule, PostAnswerCarrierRule, PreConfirmCarrierRule and PostConfirmCarrierRule properties, as in the following example.

<speech1:Date id=“Date1” runat=“server” QuestionPrompt=“When do you want to fly?” 
    YearSemanticItem=“YearSemanticItem” MonthSemanticItem=“MonthSemanticItem” 
    DaySemanticItem=“DaySemanticItem” CarrierGrammarUrl=“carrier.grxml” 
    PostAnswerCarrierRule=“Rule2” PostConfirmCarrierRule=“Rule2” 
    PreAnswerCarrierRule=“Rule1” PreConfirmCarrierRule=“Rule1”></speech1:Date>

This is an easy way to extend the contexts in which the application controls can be used.

See Also

Enabling Speech Recognition | Creating Grammars | Grammar Design