Share via


Note

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

SemanticResultValue.ToGrammarBuilder Method

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

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

Syntax

'Declaration
Public Function ToGrammarBuilder As GrammarBuilder
'Usage
Dim instance As SemanticResultValue
Dim returnValue As GrammarBuilder

returnValue = instance.ToGrammarBuilder()
public GrammarBuilder ToGrammarBuilder()

Return Value

Type: Microsoft.Speech.Recognition.GrammarBuilder
Returns an instance of GrammarBuilder constructed from the current SemanticResultValue instance.

Remarks

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

Examples

The example below creates Grammar objects that support commands which change background color.

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

A GrammarBuilder is then obtained by calling ToGrammarBuilder() on an instance of SemanticResultKey, 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 rgb
    //Choices constructors do not take SematicResultValue, so cast SematicResultValue to GramarBuilder
    Choices colorChoice = new Choices();
    foreach (string colorName in System.Enum.GetNames(typeof(KnownColor))) {
        SemanticResultValue colorValue=new SemanticResultValue(colorName, Color.FromName(colorName).ToArgb());
        colorChoice.Add(colorValue.ToGrammarBuilder());
        //Uses implicit conversion of SemanticResultValue to GrammarBuilder    
    }
    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;
}

See Also

Reference

SemanticResultValue Class

SemanticResultValue Members

Microsoft.Speech.Recognition Namespace