SrgsSemanticInterpretationTag 类   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示一个标记,该标记包含匹配规则时运行的 ECMAScript 。
public ref class SrgsSemanticInterpretationTag : System::Speech::Recognition::SrgsGrammar::SrgsElementpublic class SrgsSemanticInterpretationTag : System.Speech.Recognition.SrgsGrammar.SrgsElement[System.Serializable]
public class SrgsSemanticInterpretationTag : System.Speech.Recognition.SrgsGrammar.SrgsElementtype SrgsSemanticInterpretationTag = class
    inherit SrgsElement[<System.Serializable>]
type SrgsSemanticInterpretationTag = class
    inherit SrgsElementPublic Class SrgsSemanticInterpretationTag
Inherits SrgsElement- 继承
- 属性
示例
以下示例创建一个语法,用于为航班选择城市。 该示例使用 SrgsSemanticInterpretationTag 将语义值分配给每个城市,即城市机场的代码。 该示例还使用 SrgsSemanticInterpretationTag 为由名为 的对象对名为 cityRefSrgsRulecities的对象发出的SrgsRuleRef两个引用分配单独的语义键。 语义键将已识别的城市标识为航班的出发城市或到达城市。 事件的处理程序 SpeechRecognized 使用键从识别结果中检索语义。
在代码示例中,“out”是指包含 SrgsRule的规则变量。 表达式“out.LeavingFrom“是指名为 LeavingFrom 的规则上规则变量的名为 的属性 bookFlight。
表达式“rules.flightCities”是指规则上的规则变量,该 Id 规则为 flightCities,并且是规则引用的目标。 在示例中,表达式“out.LeavingFrom=rules.flightCities;“ 将规则IdflightCities的值赋给名为 的规则上规则LeavingFrom变量的名为 bookFlight的属性。 有关详细信息,请参阅语义结果内容、语法规则名称引用和语法规则引用。
using System;
using System.Speech.Recognition;
using System.Speech.Recognition.SrgsGrammar;
namespace SampleRecognition
{
  class Program
  {
    static void Main(string[] args)
    // Initialize a SpeechRecognitionEngine object.
    {
      using (SpeechRecognitionEngine recognizer =
         new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US")))
      {
        // Create a rule for the cities, assign a semantic value to each city.
        SrgsRule cities = new SrgsRule("flightCities");
        SrgsItem chi = new SrgsItem("Chicago");
        chi.Add(new SrgsSemanticInterpretationTag("out = \"ORD\";"));
        SrgsItem bos = new SrgsItem("Boston");
        bos.Add(new SrgsSemanticInterpretationTag("out = \"BOS\";"));
        SrgsItem mia = new SrgsItem("Miami");
        mia.Add(new SrgsSemanticInterpretationTag("out = \"MIA\";"));
        SrgsItem dal = new SrgsItem("Dallas");
        dal.Add(new SrgsSemanticInterpretationTag("out = \"DFW\";"));
        SrgsOneOf airports = new SrgsOneOf(chi, bos, mia, dal);
        cities.Add(airports);
        cities.Scope = SrgsRuleScope.Private;
        // Create a rule reference to the rule for cities.
        SrgsRuleRef cityRef = new SrgsRuleRef(cities);
        // Create the root rule for the grammar.
        SrgsRule bookFlight = new SrgsRule("flightBooker");
        bookFlight.Add(new SrgsItem("I want to fly from"));
        bookFlight.Add(cityRef);
        bookFlight.Add(new SrgsSemanticInterpretationTag("out.LeavingFrom=rules.flightCities;"));
        bookFlight.Add(new SrgsItem("to"));
        bookFlight.Add(cityRef);
        bookFlight.Add(new SrgsSemanticInterpretationTag("out.GoingTo=rules.flightCities;"));
        bookFlight.Scope = SrgsRuleScope.Public;
        // Initialize the SrgsDocument, set the root rule, add rules to the collection.
        SrgsDocument itinerary = new SrgsDocument(bookFlight);
        itinerary.Rules.Add(cities);
        // Create a Grammar object and load it to the recognizer.
        Grammar g = new Grammar(itinerary);
        g.Name = ("City Chooser");
        recognizer.LoadGrammarAsync(g);
        // Configure recognizer input.
        recognizer.SetInputToDefaultAudioDevice();
        // Attach a handler for the SpeechRecognized event.
        recognizer.SpeechRecognized +=
          new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
        // Start recognition.
        recognizer.RecognizeAsync();
        Console.WriteLine("Starting asynchronous recognition...");
        // Keep the console window open.
        Console.ReadLine();
      }
    }
    // Write to the console the text and the semantics from the recognition result.
    static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
      Console.WriteLine("Speech recognized: " + e.Result.Text);
      Console.WriteLine();
      Console.WriteLine("Semantic results:");
      Console.WriteLine("  The departure city is: " + e.Result.Semantics["LeavingFrom"].Value);
      Console.WriteLine("  The arrival city is: " + e.Result.Semantics["GoingTo"].Value);
    }
  }
}
下面是上述示例中的代码生成的语法的 XML 形式。
<?xml version="1.0" encoding="utf-8"?>
<grammar xml:lang="en-US" root="flightBooker" tag-format="semantics/1.0"
version="1.0" xmlns="http://www.w3.org/2001/06/grammar">
  <rule id="flightBooker" scope="public">
    <item> I want to fly from </item>
    <ruleref uri="#flightCities" />
    <tag> out.LeavingFrom=rules.flightCities; </tag>
    <item> to </item>
    <ruleref uri="#flightCities" />
    <tag> out.GoingTo=rules.flightCities; </tag>
  </rule>
  <rule id="flightCities" scope="private">
    <one-of>
      <item> Chicago <tag> out="ORD"; </tag></item>
      <item> Boston <tag> out="BOS"; </tag></item>
      <item> Miami <tag> out="MIA"; </tag></item>
      <item> Dallas <tag> out="DFW"; </tag></item>
    </one-of>
  </rule>
</grammar>
注解
System.Speech 的默认语义格式符合适用于语音识别的 W3C 语义解释 (SISR) 版本 1.0,其中包含脚本的 tag 元素的格式为 semantics/1.0。 必须使用此格式为 SrgsSemanticInterpretationTag 对象指定脚本。 在 的 semantics/1.0语法中:
- 包含规则元素的 Rule 变量由“out”标识。
- 有权访问包含规则元素外部规则元素的 Rule Variable 的对象的名称由“rules”标识。
- 与言语匹配的最新引用规则的结果可以用“rules.latest () ”表示。
还可以使用 对象将语义值与语法中的短语相关联,而无需使用 SrgsNameValueTag 脚本。
构造函数
| SrgsSemanticInterpretationTag() | 创建 SrgsSemanticInterpretationTag 类的实例。 | 
| SrgsSemanticInterpretationTag(String) | 创建 SrgsSemanticInterpretationTag 类的实例,指定标记的脚本内容。 | 
属性
| Script | 获取或设置标记的 ECMAScript。 | 
方法
| CreateObjRef(Type) | 创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。(继承自 MarshalByRefObject) | 
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetLifetimeService() | 
		已过时.
	 检索控制此实例的生存期策略的当前生存期服务对象。(继承自 MarshalByRefObject) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| InitializeLifetimeService() | 
		已过时.
	 获取生存期服务对象来控制此实例的生存期策略。(继承自 MarshalByRefObject) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| MemberwiseClone(Boolean) | 创建当前 MarshalByRefObject 对象的浅表副本。(继承自 MarshalByRefObject) | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) |