Grammar Rules (Microsoft.Speech)

Microsoft Speech Platform SDK 11

Collapse image Expand Image Copy image CopyHover image

The basic unit of a speech recognition grammar is the rule. A grammar rule defines a pattern or sequence of words or phrases. If the user's statement matches that pattern, the rule is matched by the application. A grammar must contain at least one rule.

A grammar rule has an identifier that distinguishes it from other rules and that provides a handle by which other rules can reference it. A grammar rule's identifier must be unique within a containing grammar.

Rules can contain references to other rules within the same grammar or to rules contained in a separate grammar. A grammar can expose rules for use externally by other grammars. This concept is very similar to the software concept of public or private scoping. Rules that are publicly scoped can be used by other grammars. Rules that are designated as private can only be used by the grammar in which they exist.

The definition of a grammar rule can also include semantic content, which is information about the words and phrases recognized by the speech recognition engine. For more information, see Create and Access Semantic Content (Microsoft.Speech) and Semantic Interpretation Markup (Microsoft.Speech).

Although the GrammarBuilder and Choices classes do not explicitly define a rule construct, their contents serve the same function as rules in SRGS-compliant grammars: to define the language constraints and the semantic content for speech recognition.

Examples

Each of the following examples creates the same rule in a grammar using one of the three authoring processes: XML-format grammar, Microsoft.Speech.Recognition.SrgsGrammar namespace, and GrammarBuilder class. The rule matches input phrases such as the following:

  • "Find albums in the jazz category"

  • "Find artists in the rock category"

The category selection is made by a rule reference to a second rule in the grammar, named genres.

XML Format

This first example builds a grammar using the XML format defined by the Speech Recognition Grammar Specification (SRGS) Version 1.0.

XML  Copy imageCopy Code
<grammar version="1.0" xml:lang="en-US" mode="voice" root="mediaMenu"
xmlns="http://www.w3.org/2001/06/grammar">

  <rule id="mediaMenu" scope="public">

  Find

    <one-of>
      <item> albums </item>
      <item> artists </item>
    </one-of>

  in the

    <ruleref uri="#genres"/>

  category.

  </rule>

  <rule id="genres" scope="public">
    <one-of>
      <item> Blues </item>
      <item> Classical </item>
      <item> Gospel </item>
      <item> Jazz </item>
      <item> Rock </item>
    </one-of>
  </rule>
</grammar> 

Given the file name "MediaMenuGrammar.grxml", the XML grammar above can be built into a Grammar object using the following code snippet:

  Copy imageCopy Code
// Build a Grammar object from the XML grammar.
Grammar mediaMenuGrammar = new Grammar("C:\\MediaMenuGrammar.grxml");

SRGSGrammar Namespace

The example that follows uses classes of the Microsoft.Speech.Recognition.SrgsGrammar namespace to build the same grammar as in the previous example. The classes of the Microsoft.Speech.Recognition.SrgsGrammar namespace match closely to the elements and attributes defined by the SRGS specification.

C#  Copy imageCopy Code
SrgsDocument mediaMenu = new SrgsDocument();

// Create rules and one-of elements.
SrgsRule displayList = new SrgsRule("selectList");
displayList.Scope = SrgsRuleScope.Public;
SrgsRule genres = new SrgsRule("musicGenres");
genres.Scope = SrgsRuleScope.Public;
SrgsOneOf listTypes = new SrgsOneOf(new string[] {"albums", "artists"});
SrgsOneOf genreTypes = new SrgsOneOf(new string[] {"blues", "classical", "gospel", "jazz", "rock"});
genres.Add(genreTypes);

// Add the rules to the grammar and set the root rule.
mediaMenu.Rules.Add(displayList);
mediaMenu.Rules.Add(genres);
mediaMenu.Root = displayList;

// Assemble the grammar components.
displayList.Elements.Add(new SrgsItem ("Find"));
displayList.Elements.Add(listTypes);
displayList.Elements.Add(new SrgsItem("in the"));
displayList.Elements.Add(new SrgsRuleRef(genres));
displayList.Elements.Add(new SrgsItem("category."));

// Build a Grammar object from the SrgsDocument.
Grammar mediaMenuGrammar = new Grammar(mediaMenu);

GrammarBuilder Class

The following example uses the GrammarBuilder and Choices classes in the Microsoft.Speech.Recognition namespace to build a grammar that is functionally identical to the previous two examples. The Choices class serves the same function as the one-of element in SRGS-compliant grammars.

C#  Copy imageCopy Code
//  Create lists of alternative choices.
Choices listTypes = new Choices(new string[] {"albums", "artists"});
Choices genres = new Choices(new string[] {"blues", "classical", "gospel", "jazz", "rock"});

// Create a GrammarBuilder object and assemble the grammar components.
GrammarBuilder mediaMenu = new GrammarBuilder("Find");
mediaMenu.Append(listTypes);
mediaMenu.Append("in the");
mediaMenu.Append(genres);
mediaMenu.Append("category.");

// Build a Grammar object from the GrammarBuilder.
Grammar mediaMenuGrammar = new Grammar(mediaMenu);

Remarks

Each of the completed grammars in the examples above must be built into a Grammar object before it can be loaded by a speech recognition engine. You can build an XML-format grammar into a Grammar object using the path and file name of the grammar as a string argument to the constructor.

See Also