Note

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

SemanticValue Class

Represents the semantic organization of a recognized phrase.

Inheritance Hierarchy

System.Object
  Microsoft.Speech.Recognition.SemanticValue

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

Syntax

'Declaration
<SerializableAttribute> _
Public NotInheritable Class SemanticValue _
    Implements IDictionary(Of String, SemanticValue),  _
    ICollection(Of KeyValuePair(Of String, SemanticValue)), IEnumerable(Of KeyValuePair(Of String, SemanticValue)),  _
    IEnumerable
'Usage
Dim instance As SemanticValue
[SerializableAttribute]
public sealed class SemanticValue : IDictionary<string, SemanticValue>, 
    ICollection<KeyValuePair<string, SemanticValue>>, IEnumerable<KeyValuePair<string, SemanticValue>>, 
    IEnumerable

Remarks

SemanticValue is the primary object that implements the semantic technology in the Microsoft Speech Platform SDK 11. Semantic interpretation allows grammars to define rules for use by a recognition engine to correctly interpret audio input. Semantic interpretation also enables recognition engines to organize their results so that they can be more easily processed, rather than returning only recognized words and sequences of words.

For example, the recognition engine output "Change background to red" would have to be parsed and interpreted by an application before it could be acted upon. A Grammar object can specify a semantic interpretation to make processing clearer by specifying that the phrase has two semantic substructures, one for selecting background or foreground (represented by the text "background"), and the other for selecting color (represented by the text "red").

The Speech Platform SDK 11 represents the semantics of a recognition operation in a tree of SemanticValue objects.

Each SemanticValue instance includes the following:

  • An Object, accessed by means of the Value property, used to key the instance of the SemanticValue.

  • A measure of the accuracy of semantic parsing, returned by the Confidence property.

  • A collection of name/value pairs ([T:System.Collections.Generic.KeyValuePair<TKey,TValue>]) of child objects, which are also SemanticValue instances. Child nodes are accessible through the SemanticValue implementation of [T:System.Collections.Generic.IDictionary(TKey,TValue)] using a string lookup key and a SemanticValue instance, as in the following example.

    foreach (KeyValuePair<String, SemanticValue> child in semantics) 
    {
      Utils.CreateSemanticsTreeNodes(semanticsNode.Nodes, child.Value, child.Key);
    }
    

Recognition engines based on Microsoft.Speech provide valid instances of SemanticValue for all output from recognition, even for phrases with no explicit semantic structure.

The SemanticValue instance for a phrase is obtained using the Semantics property on the RecognizedPhrase object (or objects that inherit from it, such as RecognitionResult).

SemanticValue objects obtained for recognized phrases without semantic structure are characterized by:

  • The lack of children (Count is 0).

  • The Value property is a null reference (Nothing in Visual Basic).

  • An artificial semantic confidence level of 1.0 (returned by Confidence).

Typically, applications create SemanticValue instances indirectly, adding them to Grammar objects by using SemanticResultValue and SemanticResultKey instances, in conjunction with Choices and GrammarBuilder objects.

Direct construction of a SemanticValue instance is useful during the creation of strongly-typed grammars.

SemanticValue implements the [T:System#Collections#Generic#IDictionary<TKey,TValue>], [T:System#Collections#Generic#ICollection(T)], and [T:System.Collections.Generic.IEnumerable<T>] interfaces.

Examples

The example below shows a handler for a SpeechRecognized event designed to handle commands to change foreground and background color.

Cases where phrases are recognized, but have no semantic structure, are identified by the handler by having Count of zero and a Value of a null reference (Nothing in Visual Basic). This recognition output is then processed directly from the parsing the raw text.

In other cases, the handler obtains specific information about whether the command is to set the foreground or background color, and the RGB of official name of a color setting based on keys, or indicates that no valid key was found.

newGrammar.SpeechRecognized +=
    delegate(object sender, SpeechRecognizedEventArgs eventArgs) {
        // Retrieve the value of the semantic property.
        bool changeBackGround = true;
        string errorString = "";
        SemanticValue semantics = eventArgs.Result.Semantics;

        Color newColor = Color.Empty;
        

        try {
            if (semantics.Count == 0 && semantics.Value==null){
                
                //This signature of recognition by grammar with no semantic
                ////We have to parse the string. hope last word is color, search for background or forground in input
                if (eventArgs.Result.Text.Contains("foreground")) {
                    changeBackGround = false;
                }
                string cName = eventArgs.Result.Words[eventArgs.Result.Words.Count - 1].Text;
                newColor = Color.FromName(cName);
            
            }else if (semantics.ContainsKey("colorStringList") ^ semantics.ContainsKey("colorRGBValueList")) {
                //Check background vs foreground

                if (semantics.ContainsKey("applyChgToBackground")) {
                    changeBackGround = semantics["applyChgToBackground"].Value is bool;
                }

                //Get color value
                if (semantics.ContainsKey("colorStringList")) {
                    newColor = Color.FromName((string)semantics["colorStringList"].Value);
                }
                if (semantics.ContainsKey("colorRGBValueList")) {
                    newColor = System.Drawing.Color.FromArgb((int)semantics["colorRGBValueList"].Value);
                }
            } else {
                //we have a semantics that does not contain the keys we support, throw an exception.
                throw(new Exception("Unsupported semantics keys found."));
            }

        } catch (Exception exp) {
            MessageBox.Show(String.Format("Unable to process color semantics.:\n{0}\n", exp.Message));
            return;
        }
        //Change colors, either foreground or background.
        if (changeBackGround) {
            BackColor = newColor;
            float Bright = BackColor.GetBrightness();
            float Hue = BackColor.GetHue();
            float Sat = BackColor.GetSaturation();
            //Make sure the text is readable regardless of background.
            if (BackColor.GetBrightness() <= .50) {
                ForeColor = Color.White;
            } else {
                ForeColor = Color.Black;
            }
        } else {
            ForeColor = newColor;
            float Bright = ForeColor.GetBrightness();
            float Hue = ForeColor.GetHue();
            float Sat = ForeColor.GetSaturation();
            //Make sure the text is readable regardless of Foreground.
            if (ForeColor.GetBrightness() <= .50) {
                BackColor = Color.White;
            } else {
                BackColor = Color.Black;
            }
        }
        Return;
    };

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

SemanticValue Members

Microsoft.Speech.Recognition Namespace

SemanticResultValue

SemanticResultKey