Export (0) Print
Expand All

GrammarBuilder Class

Provides a mechanism for programmatically building the constraints for a speech recognition grammar.

System.Object
  System.Speech.Recognition.GrammarBuilder

Namespace:  System.Speech.Recognition
Assembly:  System.Speech (in System.Speech.dll)

[DebuggerDisplayAttribute("{DebugSummary}")]
public class GrammarBuilder

The GrammarBuilder type exposes the following members.

  NameDescription
Public methodGrammarBuilder()Initializes a new, empty instance of the GrammarBuilder class.
Public methodGrammarBuilder(Choices)Initializes a new instance of the GrammarBuilder class from a set of alternatives.
Public methodGrammarBuilder(SemanticResultKey)Initializes a new instance of the GrammarBuilder class from a semantic key.
Public methodGrammarBuilder(SemanticResultValue)Initializes a new instance of the GrammarBuilder class from a semantic value.
Public methodGrammarBuilder(String)Initializes a new instance of the GrammarBuilder class from a sequence of words.
Public methodGrammarBuilder(String, SubsetMatchingMode)Initializes a new instance of the GrammarBuilder class for a subset of a sequence of words.
Public methodGrammarBuilder(GrammarBuilder, Int32, Int32)Initializes a new instance of the GrammarBuilder class from a repeated element.
Public methodGrammarBuilder(String, Int32, Int32)Initializes a new instance of the GrammarBuilder class from the sequence of words in a String and specifies how many times the String can be repeated.
Top

  NameDescription
Public propertyCultureGets or sets the culture of the speech recognition grammar.
Public propertyDebugShowPhrasesGets a string that shows the contents and structure of the grammar contained by the GrammarBuilder.
Top

  NameDescription
Public methodStatic memberAdd(Choices, GrammarBuilder)Creates a new GrammarBuilder that contains a Choices object followed by a GrammarBuilder object.
Public methodStatic memberAdd(GrammarBuilder, Choices)Creates a new GrammarBuilder that contains a GrammarBuilder object followed by a Choices object.
Public methodStatic memberAdd(GrammarBuilder, GrammarBuilder)Creates a new GrammarBuilder that contains a sequence of two GrammarBuilder objects.
Public methodStatic memberAdd(GrammarBuilder, String)Creates a new GrammarBuilder that contains a GrammarBuilder object followed by a phrase.
Public methodStatic memberAdd(String, GrammarBuilder)Creates a new GrammarBuilder that contains a phrase followed by a GrammarBuilder object.
Public methodAppend(Choices)Appends a set of alternatives to the current sequence of grammar elements.
Public methodAppend(GrammarBuilder)Appends a grammar element to the current sequence of grammar elements.
Public methodAppend(SemanticResultKey)Appends a semantic key to the current sequence of grammar elements.
Public methodAppend(SemanticResultValue)Appends a semantic value to the current sequence of grammar elements.
Public methodAppend(String)Appends a phrase to the current sequence of grammar elements.
Public methodAppend(String, SubsetMatchingMode)Appends an element for a subset of a phrase to the current sequence of grammar elements.
Public methodAppend(GrammarBuilder, Int32, Int32)Appends a repeated grammar element to the current sequence of grammar elements.
Public methodAppend(String, Int32, Int32)Appends a repeated phrase to the current sequence of grammar elements.
Public methodAppendDictation()Appends the default dictation grammar to the current sequence of grammar elements.
Public methodAppendDictation(String)Appends the specified dictation grammar to the current sequence of grammar elements.
Public methodAppendRuleReference(String)Appends a grammar definition file to the current sequence of grammar elements.
Public methodAppendRuleReference(String, String)Appends the specified rule of a grammar definition file to the current sequence of grammar elements.
Public methodAppendWildcardAppends a recognition grammar element that matches any input to the current sequence of grammar elements.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public operatorStatic memberAddition(Choices, GrammarBuilder)Creates a new GrammarBuilder that contains a Choices object followed by a GrammarBuilder object.
Public operatorStatic memberAddition(GrammarBuilder, Choices)Creates a new GrammarBuilder that contains a GrammarBuilder followed by a Choices.
Public operatorStatic memberAddition(GrammarBuilder, GrammarBuilder)Creates a new GrammarBuilder that contains a sequence of two GrammarBuilder objects.
Public operatorStatic memberAddition(GrammarBuilder, String)Creates a new GrammarBuilder that contains a GrammarBuilder followed by a phrase.
Public operatorStatic memberAddition(String, GrammarBuilder)Creates a new GrammarBuilder that contains a phrase followed by a GrammarBuilder.
Public operatorStatic memberImplicit(Choices to GrammarBuilder)Converts a Choices object to a GrammarBuilder object.
Public operatorStatic memberImplicit(SemanticResultKey to GrammarBuilder)Converts a SemanticResultKey object to a GrammarBuilder object.
Public operatorStatic memberImplicit(SemanticResultValue to GrammarBuilder)Converts a SemanticResultValue object to a GrammarBuilder object.
Public operatorStatic memberImplicit(String to GrammarBuilder)Converts a string to a GrammarBuilder object.
Top

Speech recognition grammars are commonly authored in the XML format defined by the Speech Recognition Grammar Specification (SRGS) Version 1.0. If you are familiar with SRGS but want to generate the grammars programmatically, you can use the System.Speech.Recognition.SrgsGrammar namespace, whose members correspond closely to the elements and attributes defined by SRGS. If you are unfamiliar with SRGS, or you want a lightweight, programmatic approach to authoring grammars with which you can efficiently accomplish many common scenarios; you can use the GrammarBuilder and Choices classes.

Use GrammarBuilder objects to build a hierarchical tree composed of Choices objects that contain alternate phrases, interspersed with preamble and post-amble phrases at each node, and seeded with semantic values that convey meaning back to the application.

To use a GrammarBuilder to create a Grammar object, use the following steps.

  1. Create a GrammarBuilder object.

  2. Append constraints to the GrammarBuilder, such as String objects, Choices, SemanticResultKey, SemanticResultValue, DictationGrammar, and other GrammarBuilder objects that define the constraints for the grammar.

  3. Use one of the Grammar() constructors to create a Grammar object from the completed GrammarBuilder grammar.

Authoring with GrammarBuilder is best suited to grammars that have a single rule containing lists, or perhaps lists of lists. To programmatically build grammars that have multiple rules, or that need to make internal rule references, use the classes of the System.Speech.Recognition.SrgsGrammar namespace.

Instances of GrammarBuilder can also be obtained by implicit conversions from certain other classes or by combining a GrammarBuilder with a second object that contains constraints for a grammar.. For more information, see the Implicit and Addition operators and the Add methods.

To add rules to an existing GrammarBuilder, use the Add, Append, AppendDictation, AppendRuleReference, and AppendWildcard methods.

Important noteImportant

The speech recognizer can throw an exception when using a speech recognition grammar that contains duplicate semantic elements with the same key name or multiple semantic elements that could repeatedly modify the value of the same semantic element. For more information about building a speech recognition grammar that contains semantic information, see Using SemanticResultKey and SemanticResultValue Objects.

To help with debugging, the DebugShowPhrases property returns the current status of the GrammarBuilder as a string.

For more information about building and using speech recognition grammars, see Speech Recognition and Creating GrammarBuilder Grammars.

The following example uses GrammarBuilder and Choices objects to construct a grammar that can recognize either of the two phrases, "Make background colorChoice" or "Set background to colorChoice".

The example uses a Choices object to create a list of acceptable values for colorChoice from an array of String objects. A Choices object is analogous to the one-of element in the SRGS specification, and contains a set of alternate phrases, any of which can be recognized when spoken. The example also uses a Choices object to group an array of two GrammarBuilder objects into a pair of alternative phrases that the resultant grammar can recognize. Alternate words or phrases are a component of most grammars, and the Choices object provides this functionality for grammars constructed with GrammarBuilder.

The example finally creates a Grammar object from a GrammarBuilder constructed from a Choices object.

private Grammar CreateColorGrammar()
{

  // Create a set of color choices.
  Choices colorChoice = new Choices(new string[] {"red", "green", "blue"});
  GrammarBuilder colorElement = new GrammarBuilder(colorChoice);

  // Create grammar builders for the two versions of the phrase.
  GrammarBuilder makePhrase = new GrammarBuilder("Make background");
  makePhrase.Append(colorElement);
  GrammarBuilder setPhrase = new GrammarBuilder("Set background to");
  setPhrase.Append(colorElement);

  // Create a Choices for the two alternative phrases, convert the Choices
  // to a GrammarBuilder, and construct the grammar from the result.
  Choices bothChoices = new Choices(new GrammarBuilder[] {makePhrase, setPhrase});
  Grammar grammar = new Grammar((GrammarBuilder)bothChoices);
  grammar.Name = "backgroundColor";
  return grammar;
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft