SemanticValue SemanticValue SemanticValue SemanticValue Class

Definition

Represents the semantic organization of a recognized phrase.

public ref class SemanticValue sealed : System::Collections::Generic::ICollection<System::Collections::Generic::KeyValuePair<System::String ^, System::Speech::Recognition::SemanticValue ^>>, System::Collections::Generic::IDictionary<System::String ^, System::Speech::Recognition::SemanticValue ^>, System::Collections::Generic::IEnumerable<System::Collections::Generic::KeyValuePair<System::String ^, System::Speech::Recognition::SemanticValue ^>>
[System.Serializable]
public sealed class SemanticValue : System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string,System.Speech.Recognition.SemanticValue>>, System.Collections.Generic.IDictionary<string,System.Speech.Recognition.SemanticValue>, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string,System.Speech.Recognition.SemanticValue>>
type SemanticValue = class
    interface IDictionary<string, SemanticValue>
    interface ICollection<KeyValuePair<string, SemanticValue>>
    interface seq<KeyValuePair<string, SemanticValue>>
    interface IEnumerable
Public NotInheritable Class SemanticValue
Implements ICollection(Of KeyValuePair(Of String, SemanticValue)), IDictionary(Of String, SemanticValue), IEnumerable(Of KeyValuePair(Of String, SemanticValue))
Inheritance
SemanticValueSemanticValueSemanticValueSemanticValue
Attributes
Implements

Examples

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

The handler identifies recognized phrases that have no underlying semantic structure by detecting a Count of zero and a Value of null. This recognition output is then processed directly by parsing the raw text.

In other cases, the handler uses keys to obtain the RGB components of a color name, to determine whether the command will change the foreground or background, or to indicate 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)  
      {  
        // Signifies recognition by a grammar with no semantics.  
        // Parse the string, assuming that the last word is color,  
        // and search for "background" or "foreground" in the 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"))   
      {  

        // Determine whether to change background or foreground.  
        if (semantics.ContainsKey("applyChgToBackground"))   
        {  
          changeBackGround = semantics["applyChgToBackground"].Value is bool;  
        }  

        // Get the RGB 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   
      {  

        // Throw an exception if the semantics do not contain the keys we  
        // support.  
        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 that text is readable regardless of the 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 that text is readable regardless of Foreground.  
      if (ForeColor.GetBrightness() <= .50)   
      {  
        BackColor = Color.White;  
      }  
      else   
      {  
        BackColor = Color.Black;  
      }  
    }  
    return;  
  };  

Remarks

SemanticValue is the primary object that implements the semantic technology in System.Speech. 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").

System.Speech 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 (KeyValuePair<TKey,TValue>) of child objects, which are also SemanticValue instances. Child nodes are accessible through the SemanticValue implementation of 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 System.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 null.

  • 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 IDictionary<TKey,TValue>, ICollection<T>, and IEnumerable<T> interfaces.

Constructors

SemanticValue(Object) SemanticValue(Object) SemanticValue(Object) SemanticValue(Object)

Initializes a new instance of the SemanticValue class and specifies a semantic value.

SemanticValue(String, Object, Single) SemanticValue(String, Object, Single) SemanticValue(String, Object, Single) SemanticValue(String, Object, Single)

Initializes a new instance of the SemanticValue class and specifies a semantic value, a key name, and a confidence level.

Properties

Confidence Confidence Confidence Confidence

Returns a relative measure of the certainty as to the correctness of the semantic parsing that returned the current instance of SemanticValue.

Count Count Count Count

Returns the number of child SemanticValue objects under the current SemanticValue instance.

Item[String] Item[String] Item[String] Item[String]

Returns child SemanticValue instances that belong to the current SemanticValue.

Value Value Value Value

A read-only property that returns the information contained in the current SemanticValue.

Methods

Contains(KeyValuePair<String,SemanticValue>) Contains(KeyValuePair<String,SemanticValue>) Contains(KeyValuePair<String,SemanticValue>) Contains(KeyValuePair<String,SemanticValue>)

Indicates whether the current SemanticValue instance collection contains a specific key and a specific instance of SemanticValue expressed as a key/value pair.

ContainsKey(String) ContainsKey(String) ContainsKey(String) ContainsKey(String)

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

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether a specified object is an instance of SemanticValue and equal to the current instance of SemanticValue.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Provides a hash code for a SemanticValue object.

GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

ICollection<KeyValuePair<String,SemanticValue>>.Add(KeyValuePair<String,SemanticValue>) ICollection<KeyValuePair<String,SemanticValue>>.Add(KeyValuePair<String,SemanticValue>) ICollection<KeyValuePair<String,SemanticValue>>.Add(KeyValuePair<String,SemanticValue>) ICollection<KeyValuePair<String,SemanticValue>>.Add(KeyValuePair<String,SemanticValue>)

Adds the specified key and SemanticValue to the collection.

ICollection<KeyValuePair<String,SemanticValue>>.Clear() ICollection<KeyValuePair<String,SemanticValue>>.Clear() ICollection<KeyValuePair<String,SemanticValue>>.Clear() ICollection<KeyValuePair<String,SemanticValue>>.Clear()

Removes all key/value pairs from the collection.

ICollection<KeyValuePair<String,SemanticValue>>.CopyTo(KeyValuePair<String,SemanticValue>[], Int32) ICollection<KeyValuePair<String,SemanticValue>>.CopyTo(KeyValuePair<String,SemanticValue>[], Int32) ICollection<KeyValuePair<String,SemanticValue>>.CopyTo(KeyValuePair<String,SemanticValue>[], Int32) ICollection<KeyValuePair<String,SemanticValue>>.CopyTo(KeyValuePair<String,SemanticValue>[], Int32)

Copies a key/value pair to a specific location in a targeted array.

ICollection<KeyValuePair<String,SemanticValue>>.IsReadOnly ICollection<KeyValuePair<String,SemanticValue>>.IsReadOnly ICollection<KeyValuePair<String,SemanticValue>>.IsReadOnly ICollection<KeyValuePair<String,SemanticValue>>.IsReadOnly

Gets a value that indicates whether the collection is read-only.

ICollection<KeyValuePair<String,SemanticValue>>.Remove(KeyValuePair<String,SemanticValue>) ICollection<KeyValuePair<String,SemanticValue>>.Remove(KeyValuePair<String,SemanticValue>) ICollection<KeyValuePair<String,SemanticValue>>.Remove(KeyValuePair<String,SemanticValue>) ICollection<KeyValuePair<String,SemanticValue>>.Remove(KeyValuePair<String,SemanticValue>)

Removes the specified key and SemanticValue from the collection.

IDictionary<String,SemanticValue>.Add(String, SemanticValue) IDictionary<String,SemanticValue>.Add(String, SemanticValue) IDictionary<String,SemanticValue>.Add(String, SemanticValue) IDictionary<String,SemanticValue>.Add(String, SemanticValue)

Adds the specified key and SemanticValue to the dictionary.

IDictionary<String,SemanticValue>.Keys IDictionary<String,SemanticValue>.Keys IDictionary<String,SemanticValue>.Keys IDictionary<String,SemanticValue>.Keys

Gets a collection that contains the keys from a dictionary of key/value pairs.

IDictionary<String,SemanticValue>.Remove(String) IDictionary<String,SemanticValue>.Remove(String) IDictionary<String,SemanticValue>.Remove(String) IDictionary<String,SemanticValue>.Remove(String)

Removes the specified key and SemanticValue from the dictionary.

IDictionary<String,SemanticValue>.TryGetValue(String, SemanticValue) IDictionary<String,SemanticValue>.TryGetValue(String, SemanticValue) IDictionary<String,SemanticValue>.TryGetValue(String, SemanticValue) IDictionary<String,SemanticValue>.TryGetValue(String, SemanticValue)

Gets the SemanticValue associated with the specified key.

IDictionary<String,SemanticValue>.Values IDictionary<String,SemanticValue>.Values IDictionary<String,SemanticValue>.Values IDictionary<String,SemanticValue>.Values

Gets a collection that contains the values from a dictionary of key/value pairs.

IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator()

Returns an enumerator that iterates through a collection.

IEnumerable<KeyValuePair<String,SemanticValue>>.GetEnumerator() IEnumerable<KeyValuePair<String,SemanticValue>>.GetEnumerator() IEnumerable<KeyValuePair<String,SemanticValue>>.GetEnumerator() IEnumerable<KeyValuePair<String,SemanticValue>>.GetEnumerator()

Returns an enumerator that iterates through a collection.

Extension Methods

CopyToDataTable<T>(IEnumerable<T>) CopyToDataTable<T>(IEnumerable<T>) CopyToDataTable<T>(IEnumerable<T>) CopyToDataTable<T>(IEnumerable<T>)

Returns a DataTable that contains copies of the DataRow objects, given an input IEnumerable<T> object where the generic parameter T is DataRow.

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption)

Copies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler)

Copies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

Converts an IEnumerable to an IQueryable.

Ancestors<T>(IEnumerable<T>) Ancestors<T>(IEnumerable<T>) Ancestors<T>(IEnumerable<T>) Ancestors<T>(IEnumerable<T>)

Returns a collection of elements that contains the ancestors of every node in the source collection.

Ancestors<T>(IEnumerable<T>, XName) Ancestors<T>(IEnumerable<T>, XName) Ancestors<T>(IEnumerable<T>, XName) Ancestors<T>(IEnumerable<T>, XName)

Returns a filtered collection of elements that contains the ancestors of every node in the source collection. Only elements that have a matching XName are included in the collection.

DescendantNodes<T>(IEnumerable<T>) DescendantNodes<T>(IEnumerable<T>) DescendantNodes<T>(IEnumerable<T>) DescendantNodes<T>(IEnumerable<T>)

Returns a collection of the descendant nodes of every document and element in the source collection.

Descendants<T>(IEnumerable<T>) Descendants<T>(IEnumerable<T>) Descendants<T>(IEnumerable<T>) Descendants<T>(IEnumerable<T>)

Returns a collection of elements that contains the descendant elements of every element and document in the source collection.

Descendants<T>(IEnumerable<T>, XName) Descendants<T>(IEnumerable<T>, XName) Descendants<T>(IEnumerable<T>, XName) Descendants<T>(IEnumerable<T>, XName)

Returns a filtered collection of elements that contains the descendant elements of every element and document in the source collection. Only elements that have a matching XName are included in the collection.

Elements<T>(IEnumerable<T>) Elements<T>(IEnumerable<T>) Elements<T>(IEnumerable<T>) Elements<T>(IEnumerable<T>)

Returns a collection of the child elements of every element and document in the source collection.

Elements<T>(IEnumerable<T>, XName) Elements<T>(IEnumerable<T>, XName) Elements<T>(IEnumerable<T>, XName) Elements<T>(IEnumerable<T>, XName)

Returns a filtered collection of the child elements of every element and document in the source collection. Only elements that have a matching XName are included in the collection.

InDocumentOrder<T>(IEnumerable<T>) InDocumentOrder<T>(IEnumerable<T>) InDocumentOrder<T>(IEnumerable<T>) InDocumentOrder<T>(IEnumerable<T>)

Returns a collection of nodes that contains all nodes in the source collection, sorted in document order.

Nodes<T>(IEnumerable<T>) Nodes<T>(IEnumerable<T>) Nodes<T>(IEnumerable<T>) Nodes<T>(IEnumerable<T>)

Returns a collection of the child nodes of every document and element in the source collection.

Remove<T>(IEnumerable<T>) Remove<T>(IEnumerable<T>) Remove<T>(IEnumerable<T>) Remove<T>(IEnumerable<T>)

Removes every node in the source collection from its parent node.

Applies to

See also