Create SrgsDocument Objects (Microsoft.Speech)

You can use an SrgsDocument instance to build strongly-typed grammars that conform to the Speech Recognition Grammar Specification (SRGS) Version 1.0. An SrgsDocument instance is a container for the components that comprise a speech recognition grammar:

  • The root rule (Root), which is active when the grammar is loaded.

  • The collection of rules that are defined for the grammar (Rules).

  • The recognition mode, either voice or dual-tone multi-frequency (DTMF) (Mode).

  • The phonetic alphabet (PhoneticAlphabet) to use when creating inline pronunciations for SrgsToken objects.

  • The language and locale that the grammar supports (Culture).

  • The grammar’s base Uniform Resource Identifier (URI) (XmlBase).

You can use any of five overloaded constructors to create an SrgsDocument instance. The constructor with no arguments creates an SrgsDocument instance with an empty SrgsRulesCollection, and with most of its properties set to their default values. One SrgsDocument constructor takes a GrammarBuilder instance as its argument, and another takes an SrgsRule instance as its argument.

With two of the constructors you can create an SrgsDocument instance using an existing XML grammar. One of these constructors takes a string argument that represents the file location of the XML grammar. The other constructor takes an XmlReader instance as its argument. For more information, see the example in SrgsDocument(XmlReader).

The following example shows the creation of a simple rule that can recognize the names of five different fruits. After initializing the SrgsRule object, the example uses an SrgsOneOf object to create a list of alternatives from an array of String objects. Adding the SrgsOneOf object to the rule, the example then creates the SrgsDocument instance. The rule is then added to the SrgsRulesCollection on the SrgsDocument instance, and designated as the root rule of the grammar.

// Create a rule and give it the identifier "fruitList".
SrgsRule fruitRule = new SrgsRule("fruitList");

// Create a list of alternatives containing names of fruit 
// and add the list to the rule.
SrgsOneOf fruit = new SrgsOneOf(new string[] { "apple", "banana", "pear", "peach", "melon" });
fruitRule.Scope = SrgsRuleScope.Public;

// Create an SrgsDocument instance, add the rule, and set it as the root rule.
SrgsDocument document = new SrgsDocument();
document.Root = fruitRule;