Partilhar via


Note

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

SemanticResultValue Constructor (String, Object)

Initializes a new instance of the SemanticResultValue class and associates a semantic value with a String object.

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

Syntax

'Declaration
Public Sub New ( _
    phrase As String, _
    value As Object _
)
'Usage
Dim phrase As String
Dim value As Object

Dim instance As New SemanticResultValue(phrase, _
    value)
public SemanticResultValue(
    string phrase,
    Object value
)

Parameters

Remarks

If the String specified by phrase is used in the logic of recognition, the contents of value will be set in the Semantics property of the RecognizedPhrase object.

In the code fragment below, if the recognition logic constructed with the GrammarBuilder instance (myGb) uses the string "my mortgage" to identify input, the value true will be added to the recognized semantics.

myGb.Append(new SemanticResultValue("my mortgage", true);

Examples

The example below returns a Grammar for recognizing the command "Set/Change/Alter Foreground/Background … [color list]". SemanticResultValue, and SemanticResultKey instances (along with Choices and GrammarBuilder objects) are used to define semantics that can be parsed on recognition. The parsed semantics will determine which color was requested and whether the foreground or background is to be modified.

The use of the SemanticResultValue constructor is highlighted.

private Grammar FgBgColorGrammar() {
    Grammar grammar = null;
    //Allow command to begin with set, alter, change.
    Choices introChoices = new Choices();
    foreach (string introString in new string[] { "Change", "Set", "Alter" }) {
        GrammarBuilder introGB = new GrammarBuilder(introString);
        introChoices.Add(new SemanticResultValue(introGB,
                            String.Format("Command: {0}", introString)));
    }         
    GrammarBuilder cmdIntro = new GrammarBuilder(introChoices);
    //Now define the arguments to the command for Foreground or background and color as sementic Values
    Choices fgOrbgChoice = new Choices();
    GrammarBuilder backgroundGB=new GrammarBuilder("background");
    backgroundGB.Append(new SemanticResultValue(true));
    fgOrbgChoice.Add(backgroundGB);
    fgOrbgChoice.Add((GrammarBuilder)new SemanticResultValue("foreground", false));
    SemanticResultKey fgOrbgChoiceKey = new SemanticResultKey("BgOrFgBool", fgOrbgChoice);
    Choices colorChoice = new Choices();
    foreach (string colorName in System.Enum.GetNames(typeof(KnownColor))) {        colorChoice.Add((GrammarBuilder)                                        (new SemanticResultValue(colorName, (Color.FromName(colorName)).Name)));        //Uses implicit conversion of SemanticResultValue to GrammarBuilder        }

    //Create GrammarBuilder for CmdArgs to be appended to CmdInto using Semantic keys.
    GrammarBuilder cmdArgs = new GrammarBuilder();
    cmdArgs.Append(new SemanticResultKey("BgOrFgBool", fgOrbgChoice));
    cmdArgs.AppendWildcard();
    cmdArgs.Append(new SemanticResultKey("colorStringList", colorChoice));

    GrammarBuilder cmds = GrammarBuilder.Add(cmdIntro,
                                             new GrammarBuilder(new SemanticResultKey("Cmd Args", cmdArgs)));
    grammar = new Grammar(cmds);
    grammar.Name = "Tree [Set,change,alter] [foreground,background] * color";
    return grammar;
}

See Also

Reference

SemanticResultValue Class

SemanticResultValue Members

SemanticResultValue Overload

Microsoft.Speech.Recognition Namespace