SemanticResultValue 类

定义

表示一个语义值和选择性地将该值与语音识别语法的组件关联。Represents a semantic value and optionally associates the value with a component of a speech recognition grammar.

public ref class SemanticResultValue
public class SemanticResultValue
type SemanticResultValue = class
Public Class SemanticResultValue
继承
SemanticResultValue

示例

下面的示例返回一个Grammar , 它可识别命令 "Set/Change/Alter 前台/Background ..."The following example returns a Grammar that recognizes the command "Set/Change/Alter Foreground/Background … [color list] "。[color list]". SemanticResultValueSemanticResultKey实例 (与ChoicesGrammarBuilder对象结合使用) 用于定义可在识别时进行分析的语义。SemanticResultValue and SemanticResultKey instances (in conjunction 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.

  
private Grammar FgBgColorGrammar()   
{  
  Grammar grammar = null;  
  
  // Allow the 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);  
  
  // Define the arguments for the command to select foreground or background   
  // and to change their color as semantic 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)))   
  {  
  
    // Use implicit conversion of SemanticResultValue to GrammarBuilder.      
    colorChoice.Add(  
        (GrammarBuilder) (new SemanticResultValue(colorName, (Color.FromName(colorName)).Name)));  
  }  
  
  // Create a GrammarBuilder for CmdArgs to be appended to CmdIntro 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;  
}  

注解

SemanticResultValue将和SemanticResultKey对象与GrammarBuilder和结合Choices使用是设计的语义结构的Grammar最简单方法。Use of SemanticResultValue and SemanticResultKey objects, in conjunction with GrammarBuilder and Choices, is the easiest way to design a semantic structure for a Grammar. 通过获取的SemanticValue实例, Semantics通过的属性RecognizedPhrase访问短语的语义信息。Semantic information for a phrase is accessed by obtaining an instance of SemanticValue, through the Semantics property on RecognizedPhrase.

备注

SemanticResultValue对象管理的值由传递Object给其构造函数的实例定义。Values managed by SemanticResultValue objects are defined by Object instances passed to their constructors. Object的基础类型必须是boolint float、或stringThe underlying type of this Object must be bool, int, float, or string. 任何其他类型都将阻止Grammar SemanticResultValue使用构造实例。Any other type will prevent construction of a Grammar instance with the SemanticResultValue.

SemanticResultValue实例的典型用法是将实例与的Grammar可识别组件 (如短语、规则或Choices对象) 相关联。The typical use of a SemanticResultValue instance associates the instance with a recognizable component of a Grammar, such as a phrase, a rule, or a Choices object. 如果将关联的组件用作识别操作的一部分, SemanticResultValue则将用于定义所返回短语的语义中的值。If the associated component is used as part of a recognition operation, the SemanticResultValue is used to define a value in the semantics of the returned phrase.

有两种将SemanticResultValue实例与语法元素相关联的基本方法, 具体取决于用于创建的SemanticResultValue构造函数。There are two basic methods for associating a SemanticResultValue instance with a grammar element, depending on the constructor used to create the SemanticResultValue.

要由Grammar in 识别使用, 所有SemanticResultValue实例都必须与使用GrammarSemanticValue对象之一相关联。To be used by a Grammar in recognition, all SemanticResultValue instances must be associated with one of the SemanticValue objects used by that Grammar. 这是通过将SemanticResultValue语义键与相关联来完成的。This is done by associating a semantic key with the SemanticResultValue.

语义键可以SemanticResultKey使用对象显式附加到SemanticResultValueSemantic keys can be explicitly attached to a SemanticResultValue, using a SemanticResultKey object. SemanticResultValue未显式附加到键的实例会附加到默认SemanticValue的根键。SemanticResultValue instances not explicitly attached to a key are attached to the root key of the default SemanticValue.

使用后, 无论是使用默认根密钥还是按任何特殊SemanticResultKey项进行标记, 都不能修改该值, 也不会在识别操作期间发生异常。 Value SemanticResultValueAfter a SemanticResultValue has been used to set the Value, whether it is tagged with the default root key or by any particular SemanticResultKey, that value must not be modified or an exception will occur during recognition operations.

下面的示例将导致异常, 因为它将设置然后修改的根ValueGrammarThe following example will cause an exception because it sets and then modifies the root Value of a Grammar.

GrammarBuilder gb=new GrammarBuilder();  
gb.Append(new SemanticResultValue("One"));  
gb.Append(new SemanticResultValue("Two"));  

另一方面, 允许使用以下示例中的代码。On the other hand, the code in the following example is permitted. 尽管它定义了的SemanticResultValue多个实例, 但它们包含在一个Choices对象中, 只会使用一个对象来设置键bgOrfgText的值。Although it defines multiple instances of SemanticResultValue, they are included in a Choices object, and only one will ever be used to set the value of the key bgOrfgText.

Choices fgOrbgChoice = new Choices();  
fgOrbgChoice.Add((GrammarBuilder)new SemanticResultValue("background"));  
fgOrbgChoice.Add((GrammarBuilder)new SemanticResultValue("foreground"));  
SemanticResultKey fgOrbgChoiceKey = new SemanticResultKey("BgOrFgText", fgOrbgChoice);  

构造函数

SemanticResultValue(GrammarBuilder, Object)

使用 SemanticResultValue 对象初始化 GrammarBuilder 类和关联语义值的一个新实例。Initializes a new instance of the SemanticResultValue class and associates a semantic value with a GrammarBuilder object.

SemanticResultValue(Object)

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

SemanticResultValue(String, Object)

使用 SemanticResultValue 对象初始化 String 类和关联语义值的一个新实例。Initializes a new instance of the SemanticResultValue class and associates a semantic value with a String object.

方法

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

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

(继承自 Object)
MemberwiseClone()

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

(继承自 Object)
ToGrammarBuilder()

GrammarBuilder 实例返回 SemanticResultValue 结构的实例。Returns an instance of GrammarBuilder constructed from the current SemanticResultValue instance.

ToString()

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

(继承自 Object)

适用于

另请参阅