SemanticResultValue.ToGrammarBuilder Method ()


Returns an instance of GrammarBuilder constructed from the current SemanticResultValue instance.

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

public GrammarBuilder ToGrammarBuilder()

Return Value

Type: System.Speech.Recognition.GrammarBuilder

Returns an instance of GrammarBuilder constructed from the current SemanticResultValue instance.

The use of ToGrammarBuilder is equivalent to using the GrammarBuilder constructor that takes a SemanticResultValue as an argument (GrammarBuilder(SemanticResultValue)).

The following example creates Grammar objects that support commands to change the background color.

A Choices object (colorChoice) containing the list of options for background colors is filled using the Add(GrammarBuilder[]) method with GrammarBuilder instances that are obtained from the ToGrammarBuilder() method on the SemanticResultValue objects created from color strings.

A GrammarBuilder is then obtained by calling ToGrammarBuilder() on a SemanticResultKey instance, which will be used to key the semantic choices in the colorChoice instance.

private Grammar CreateGrammarBuilderRGBSemantics() 

  // Create a set of choices, each a lookup from a color name to RBG.
  // Choices constructors do not take a SemanticResultValue parameter, so
  // cast SemanticResultValue to GrammarBuilder.
  Choices colorChoice = new Choices();
  foreach (string colorName in System.Enum.GetNames(typeof(KnownColor))) 

    // Use implicit conversion of SemanticResultValue to GrammarBuilder.    
    SemanticResultValue colorValue = 
        new SemanticResultValue(colorName, Color.FromName(colorName).ToArgb());

  SemanticResultKey choiceKey = new SemanticResultKey("rgb", colorChoice);
  GrammarBuilder choiceBuilder = choiceKey.ToGrammarBuilder();

  // Create two intermediate grammars with an introductory phrase and the
  // color choice.
  GrammarBuilder makeBackgroundBuilder = "Make background";

  GrammarBuilder configureBackgroundBuilder = new GrammarBuilder("Configure background as");
  configureBackgroundBuilder.Append((new SemanticResultKey("rgb", colorChoice)).ToGrammarBuilder());

  // Create the final grammar, which recognizes either intermediate grammar.
  Grammar grammar = new Grammar(new Choices(new GrammarBuilder[] { makeBackgroundBuilder, configureBackgroundBuilder }));
  grammar.Name = "Set Background Color";

  return grammar;

.NET Framework
Available since 3.0
Return to top