Use the SrgsGrammar Building Blocks

You can use the classes in the System.Speech.Recognition.SrgsGrammar namespace to create a grammar dynamically, while the application is running. The classes in this namespace map closely to the elements specified in the World Wide Web Consortium (W3C) Speech Recognition Grammar Specification (SRGS) Version 1.0, as shown in the following table. For more information, see SRGS Grammar XML Reference.

SrgsGrammar Namespace Member

SRGS element

SrgsItem

item

SrgsOneOf

one-of

SrgsRule

rule

SrgsRuleRef

ruleref

SrgsToken

token

SrgsNameValueTag

tag

SrgsSemanticInterpretationTag

tag

Using SrgsItem Objects

An SrgsItem instance can represent a word or phrase, and can also contain SrgsRuleRef, SrgsToken, SrgsSemanticInterpretationTag, SrgsOneOf instances. An SrgsItem instance can appear within an SrgsRule instance, or as one of several alternative choices in an SrgsOneOf instance.

When you create an SrgsItem instance, you can specify minimum and maximum repetition values in the constructor. For example, if you use minimum and maximum repetition counts of 0 and 1, then the item can be spoken zero or one time (recognition can be successful whether or not recognized speech contains the contents of the item). If minimum and maximum repetition counts of 1 and 1 are specified, then the item must be spoken exactly once for recognition to be successful.

In the following example, optionalItem is a grammar element that can be spoken once or not at all. The mandatoryItem object must be spoken exactly once.

SrgsItem optionalItem = new SrgsItem(0, 1, "please");
SrgsItem mandatoryItem = new SrgsItem(1, 1, "yes");

Using SrgsOneOf Objects

Use SrgsOneOf objects to represent a list of alternative utterances that a user may speak, any of which will constitute successful recognition. SrgsOneOf objects are analogous to Choices objects that are used in GrammarBuilder grammars.

The following example shows an SrgsOneOf instance that can match a user utterance containing any one of the five fruits listed.

SrgsOneOf fruits = new SrgsOneOf(new string[] 
{"apple", "banana", "pear", "peach", "melon"});

The following example shows an SRGS one-of element with the same structure as the fruits object in the previous example.

<one-of>
  <item> apple </item>
  <item> banana </item>
  <item> pear </item>
  <item> peach </item>
  <item> melon </item>
</one-of>

Using SrgsRule Objects

An SrgsRule object represents an element of a grammar that defines the set of phrases that a user can speak to produce a successful recognition. A SrgsDocument object must contain at least one SrgsRule object.

An SrgsRule instance can contain objects of the following types. All of these types inherit from the abstract SrgsElement base class.

You can add an instance of one of the preceding types to a rule by calling the Add(SrgsElement) method.

The following example shows the creation of an SrgsRule instance named subjRule. The example sets the Id property on subjRule to "id_Subject" when it creates the instance. The default value of the Scope property of an SrgsRule instance is Private (one of two values on the SrgsRuleScope enumeration).

After creating the subjRule instance, the example initiates an SrgsOneOf object containing six alternative words, and then adds the SrgsOneOf instance to subjRule.

// Create a rule and give it the identifier "id_Subject".
SrgsRule subjRule = new SrgsRule("id_Subject");
SrgsOneOf subjList = new SrgsOneOf(new string[] {"I", "you", "he", "she", "Tom", "Mary"});
subjRule.Add(oneOf);

The SrgsRule instance in the preceding example is functionally equivalent to the SRGS XML rule element in the following example.

<rule id="id_Subject" scope="private">
  <one-of>
    <item> I </item>
    <item> you </item>
    <item> he </item>
    <item> she </item>
    <item> Tom </item>
    <item> Mary </item>
  </one-of>
</rule>

Using SrgsRuleRef Objects

An SrgsRuleRef object specifies a reference to an SrgsRule object in the same grammar, or to a rule element in an XML-format SRGS file external to the grammar.

The following SrgsRule instance contains three SrgsRuleRef instances. When constructing each rule reference, the example passes the referenced SrgsRule object and a string (representing the semantic key) in the constructor call. The contents of the referenced rules are not shown.

// Create a rule that contains references to three other rules.
SrgsRule mainRule = new SrgsRule("Subj_Verb_Obj");
mainRule.Scope = SrgsRuleScope.Public;

// Create the first rule reference and add it to the rule.
SrgsRuleRef subjRef = new SrgsRuleRef(subjRule, "theSubject");
mainRule.Add(subjRef);

// Create the second rule reference and add it to the rule.
SrgsRuleRef verbRef = new SrgsRuleRef(verbRule, "theVerb");
mainRule.Add(verbRef);

// Add logic to handle articles: "the", "a", "and"
// occurring zero or one time.
SrgsOneOf articles = new SrgsOneOf(new SrgsItem[] { 
  (new SrgsItem(0, 1, "the")), new SrgsItem(0, 1, "a"), 
  new SrgsItem(0, 1, "an") });
mainRule.Add(articles);

// Create the third rule reference and add it to the rule.
SrgsRuleRef objRef = new SrgsRuleRef(objRule, "theObject");
mainRule.Add(objRef);

Note

A SrgsRuleRef instance references an SrgsRule object by specifying the object. However, to reference a rule element in an XML-format grammar, the SrgsRuleRef object specifies the identifier of the rule element (its id attribute).

The rule shown in the preceding example is functionally equivalent to the SRGS XML rule in the following example. This comparison assumes that:

  • The identifier for subjRule is "id_Subject".

  • The identifier for verbRule is "id_verb".

  • The identifier for objRule is "id_Object".

<rule id="Subj_Verb_Obj" scope="public">
  <item>
    <ruleref uri="#id_Subject"/>
    <tag> out.id_Subject = rules.latest()</tag>

    <ruleref uri="#id_Verb"/>
    <tag> out.id_Verb = rules.latest() </tag>

    <one-of>
      <item repeat="0-1"> the </item>
      <item repeat="0-1"> a </item>
      <item repeat="0-1"> an </item>
    </one-of>

    <ruleref uri="#id_Object"/>
    <tag> out.id_Object = rules.latest() </tag>
  </item>
</rule>

See Also

Concepts

Create Grammars Using SrgsGrammar