Compartilhar via


Note

Please see Azure Cognitive Services for Speech documentation for the latest supported speech solutions.

GrammarBuilder Class

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

Inheritance Hierarchy

System.Object
  Microsoft.Speech.Recognition.GrammarBuilder

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

Syntax

'Declaration
Public Class GrammarBuilder
'Usage
Dim instance As GrammarBuilder
public class GrammarBuilder

Remarks

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 Microsoft.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, 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 Microsoft.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 constraints to an existing GrammarBuilder, use the Add, Append, AppendDictation, AppendRuleReference, and AppendWildcard methods.

Important

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 Add Semantics to a GrammarBuilder Grammar (Microsoft.Speech).

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 (Microsoft.Speech) and Create Grammars Using GrammarBuilder (Microsoft.Speech).

Examples

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 Choicesobject 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 Choicesobject 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;
}

Thread Safety

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

See Also

Reference

GrammarBuilder Members

Microsoft.Speech.Recognition Namespace

Choices

Grammar

SemanticResultKey

SemanticResultValue