







|
| SemanticResultKey..::..ToGrammarBuilder Method |
| SemanticResultKey Class Example See Also Send Feedback |
Returns an instance of GrammarBuilder constructed from the current SemanticResultKey instance.
Namespace:
Microsoft.Speech.Recognition
Assembly:
Microsoft.Speech (in Microsoft.Speech.dll)
Syntax
| Visual Basic (Declaration) |
|---|
Public Function ToGrammarBuilder As GrammarBuilder |
| Visual Basic (Usage) |
|---|
Dim instance As SemanticResultKey Dim returnValue As GrammarBuilder returnValue = instance.ToGrammarBuilder() |
| C# |
|---|
public GrammarBuilder ToGrammarBuilder() |
Remarks
The use of ToGrammarBuilder is equivalent to using the GrammarBuilder constructor that takes a SemanticResultKey object as an argument (GrammarBuilder(SemanticResultKey)).
Examples
The following example creates a Grammar object that supports commands to change the background color.
A Choices object (colorChoice) containing the list of options for background colors is filled using the Add(array<GrammarBuilder>[]()[][]) method with GrammarBuilder instances. The GrammarBuilder instances are obtained through 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 colorChoice.
| C# | Copy Code |
|---|---|
private Grammar CreateGrammarBuilderRGBSemantics()
{
// Create a set of choices, each a lookup from a color name to the RGB value.
// Choices constructors do not take SemanticResultValue arguments,
// so cast SemanticResultValue to GramarBuilder.
Choices colorChoice = new Choices();
foreach (string colorName in System.Enum.GetNames(typeof(KnownColor)))
{
SemanticResultValue colorValue=new SemanticResultValue(colorName, Color.FromName(colorName).ToArgb());
// Uses implicit conversion of SemanticResultValue to GrammarBuilder
colorChoice.Add(colorValue.ToGrammarBuilder());
}
SemanticResultKey choiceKey = new SemanticResultKey("rgb", colorChoice);
GrammarBuilder choiceBuilder = choiceKey.ToGrammarBuilder();
// Create two intermediate grammars with introductory phrase and the color choice.
GrammarBuilder makeBackgroundBuilder = "Make background";
makeBackgroundBuilder.Append(choiceBuilder);
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(makeBackgroundBuilder, configureBackgroundBuilder));
grammar.Name = "Make Background /Configure background as";
return grammar;
}
| |
