ContainsKey Method

Microsoft Speech Platform SDK 11

Collapse imageExpand ImageCopy imageCopyHover image

Indicates whether the current SemanticValue instance collection contains a child SemanticValue instance with a given key string.

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

Syntax

Visual Basic (Declaration)
Public Function ContainsKey ( _
	key As String _
) As Boolean
Visual Basic (Usage)
Dim instance As SemanticValue
Dim key As String
Dim returnValue As Boolean

returnValue = instance.ContainsKey(key)
C#
public bool ContainsKey(
	string key
)

Parameters

key
Type: System..::..String

String containing the key string used to identify a child instance of SemanticValue under the current SemanticValue.

Return Value

Type: System..::..Boolean

Returns a bool, true if a child instance SemanticValue tagged with the string key is found, false if not.

Implements

IDictionary<(Of <(<'TKey, TValue>)>)>..::..ContainsKey(TKey)

Remarks

Because accessing data by key value, for instance semantic["myKey"].Value, can only be checked at run time, and generates an exception, it is recommended that before using Item with a given instance of SemanticValue, the object should be queried with ContainsKey.

Examples

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

After dealing with the case of phrase that are recognized, but have no semantic structure, the handler checks for the presence of appropriate keys using ContainsKey (applyChgToBackground, colorRGBValueList, or colorStringList) and then processes the semantically organized data.

The use of ContainsKey is highlighted below.

 Copy imageCopy Code
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);
                } // end featured code
            } 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;

};

See Also