SemanticValue 类

定义

表示已识别的短语的语义组织。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))
继承
SemanticValue
属性
实现

示例

下面的示例演示了一个事件处理SpeechRecognized程序, 该事件旨在处理命令以更改前景色和背景色。The following example shows a handler for a SpeechRecognized event designed to handle commands to change foreground and background color.

处理程序通过检测CountValuenull的来识别没有基础语义结构的识别的短语。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.

在其他情况下, 该处理程序使用键获取颜色名称的 RGB 组成部分, 以确定命令是否将更改前景色或背景, 或指示找不到有效的键。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;  
  };  

注解

SemanticValue是在系统中实现语义技术的主对象。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. Grammar对象可以指定语义解释, 通过指定该短语具有两个语义子结构, 一个用于选择背景或前景 (用文本 "背景" 表示), 另一个用于选择颜色 (用文本 "red" 表示)。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.object 表示SemanticValue对象树中的识别操作的语义。System.Speech represents the semantics of a recognition operation in a tree of SemanticValue objects.

SemanticValue个实例包括以下各项:Each SemanticValue instance includes the following:

基于 system.object 的识别引擎为所有来自识别的输出SemanticValue提供有效实例, 即使对于没有显式语义结构的短语也是如此。Recognition engines based on System.Speech provide valid instances of SemanticValue for all output from recognition, even for phrases with no explicit semantic structure.

使用对象的RecognitionResult属性 (或从它继承的对象 (如) 获取短语的实例。SemanticValue RecognizedPhrase SemanticsThe 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为不带语义结构的可识别短语获取的对象的特征是:SemanticValue objects obtained for recognized phrases without semantic structure are characterized by:

  • 缺少子级 (Count为 0)。The lack of children (Count is 0).

  • Value 属性为 nullThe Value property is null.

  • 人工语义置信度为 1.0 (由Confidence返回)。An artificial semantic confidence level of 1.0 (returned by Confidence).

通常, 应用程序SemanticValue会间接创建实例, 并Grammar使用SemanticResultValueSemanticResultKey实例将它们添加到对象, Choices以及GrammarBuilder和和对象。Typically, applications create SemanticValue instances indirectly, adding them to Grammar objects by using SemanticResultValue and SemanticResultKey instances, in conjunction with Choices and GrammarBuilder objects.

SemanticValue实例的直接构造在创建强类型语法期间很有用。Direct construction of a SemanticValue instance is useful during the creation of strongly-typed grammars.

SemanticValueIDictionary<TKey,TValue>实现、 ICollection<T>和接口。IEnumerable<T>SemanticValue implements the IDictionary<TKey,TValue>, ICollection<T>, and IEnumerable<T> interfaces.

构造函数

SemanticValue(Object)

使用指定的语义值初始化 SemanticValue 类的新实例。Initializes a new instance of the SemanticValue class and specifies a semantic value.

SemanticValue(String, Object, Single)

初始化 SemanticValue 类的新实例并指定一个语义值、键名称和置信度。Initializes a new instance of the SemanticValue class and specifies a semantic value, a key name, and a confidence level.

属性

Confidence

返回有关返回 SemanticValue 的当前实例的语义分析的正确性的某个相对度量。Returns a relative measure of the certainty as to the correctness of the semantic parsing that returned the current instance of SemanticValue.

Count

返回当前 SemanticValue 实例下子 SemanticValue 对象的数目。Returns the number of child SemanticValue objects under the current SemanticValue instance.

Item[String]

返回属于当前 SemanticValue的子 SemanticValue 实例。Returns child SemanticValue instances that belong to the current SemanticValue.

Value

返回当前 SemanticValue 中包含的信息的只读属性。A read-only property that returns the information contained in the current SemanticValue.

方法

Contains(KeyValuePair<String,SemanticValue>)

指示当前 SemanticValue 实例集合是否包含特定键和表示为键/值对的 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)

指示当前 SemanticValue 实例集合是否包含具有给定的键字符串的子 SemanticValue 实例。Indicates whether the current SemanticValue instance collection contains a child SemanticValue instance with a given key string.

Equals(Object)

确定指定的对象是否为 SemanticValue 的实例,并与当前 SemanticValue 实例是否相等。Determines whether a specified object is an instance of SemanticValue and equal to the current instance of SemanticValue.

GetHashCode()

提供 SemanticValue 对象的哈希代码。Provides a hash code for a SemanticValue object.

GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

显式界面实现

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

将指定的键和 SemanticValue 添加到集合中。Adds the specified key and SemanticValue to the collection.

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

从集合中移除所有键/值对。Removes all key/value pairs from the collection.

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

获取一个值,指示集合是否为只读集合。Gets a value that indicates whether the collection is read-only.

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

从集合中删除指定的键和 SemanticValueRemoves the specified key and SemanticValue from the collection.

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

将指定的键和 SemanticValue 添加到字典中。Adds the specified key and SemanticValue to the dictionary.

IDictionary<String,SemanticValue>.Keys

获取包含来自据键/值对的字典的密钥的集合。Gets a collection that contains the keys from a dictionary of key/value pairs.

IDictionary<String,SemanticValue>.Remove(String)

从字典中移除指定的键和 SemanticValueRemoves the specified key and SemanticValue from the dictionary.

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

获取与指定键关联的 SemanticValueGets the SemanticValue associated with the specified key.

IDictionary<String,SemanticValue>.Values

获取包含来自键或值对的值的集合。Gets a collection that contains the values from a dictionary of key/value pairs.

IEnumerable.GetEnumerator()

返回循环访问集合的枚举数。Returns an enumerator that iterates through a collection.

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

返回循环访问集合的枚举数。Returns an enumerator that iterates through a collection.

扩展方法

CopyToDataTable<T>(IEnumerable<T>)

在给定其泛型参数 TDataTable 的输入 DataRow 对象的情况下,返回包含 IEnumerable<T> 对象副本的 DataRowReturns 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)

在给定其泛型参数 TDataRow 的输入 DataTable 对象的情况下,将 IEnumerable<T> 对象复制到指定的 DataRowCopies 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)

在给定其泛型参数 TDataRow 的输入 DataTable 对象的情况下,将 IEnumerable<T> 对象复制到指定的 DataRowCopies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

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)

返回经过筛选的元素集合,其中包含源集合中每个节点的上级。Returns a filtered collection of elements that contains the ancestors of every node in the source collection. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

DescendantNodes<T>(IEnumerable<T>)

返回源集合中每个文档和元素的子代节点的集合。Returns a collection of the descendant nodes of every document and element in the source collection.

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)

返回经过筛选的元素集合,其中包含源集合中每个元素和文档的子代元素。Returns a filtered collection of elements that contains the descendant elements of every element and document in the source collection. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

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)

返回源集合中经过筛选的每个元素和文档的子元素集合。Returns a filtered collection of the child elements of every element and document in the source collection. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

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>)

返回源集合中每个文档和元素的子节点集合。Returns a collection of the child nodes of every document and element in the source collection.

Remove<T>(IEnumerable<T>)

将源集合中的每个节点从其父节点中移除。Removes every node in the source collection from its parent node.

适用于

另请参阅