SrgsSemanticInterpretationTag 类
定义
表示一个标记,该标记包含与规则匹配时运行的 ECMAScript 。Represents a tag that contains ECMAScript that's run when the rule is matched.
public ref class SrgsSemanticInterpretationTag : System::Speech::Recognition::SrgsGrammar::SrgsElement
[System.Serializable]
public class SrgsSemanticInterpretationTag : System.Speech.Recognition.SrgsGrammar.SrgsElement
[<System.Serializable>]
type SrgsSemanticInterpretationTag = class
inherit SrgsElement
Public Class SrgsSemanticInterpretationTag
Inherits SrgsElement
- 继承
- 属性
示例
以下示例创建了一个用于为航班选择城市的语法。The following example creates a grammar for choosing the cities for a flight. 该示例使用将 SrgsSemanticInterpretationTag 一个语义值分配给每个城市,即 city 的机场代码。The example uses SrgsSemanticInterpretationTag to assign a semantic value to each city, which is the code for the city's airport. 该示例还使用为名为的对象的 SrgsSemanticInterpretationTag 对象所执行的两个引用中的每个引用分配一个单独的语义键 SrgsRuleRef cityRef SrgsRule cities 。The example also uses SrgsSemanticInterpretationTag to assign a separate semantic key for each of the two references made by the SrgsRuleRef object named cityRef to the SrgsRule object named cities. 语义键将识别的城市识别为航班的出发城市或接收城市。The semantic keys identify a recognized city as the departure city or the arrival city for the flight. 事件的处理程序 SpeechRecognized 使用键来检索识别结果中的语义。The handler for the SpeechRecognized event uses the keys to retrieve the semantics from the recognition result.
在代码示例中,"out" 指的是包含的规则变量 SrgsRule 。In the code example, "out" refers to the Rule Variable of the containing SrgsRule. 表达式 "out。LeavingFrom "引用名为 LeavingFrom 的规则上规则变量的名为的属性 bookFlight 。The expression "out.LeavingFrom" refers to the property named LeavingFrom of the Rule Variable on the rule named bookFlight.
表达式 "flightCities" 指的是规则的规则变量 Id flightCities ,它是规则引用的目标。The expression "rules.flightCities" refers to the Rule Variable on the rule whose Id is flightCities, and which is the target of a rule reference. 在此示例中,表达式 "out"。LeavingFrom = flightCities; "将该规则中的值分配给名为的规则中的 Id flightCities LeavingFrom 规则变量名为的属性 bookFlight 。In the example, the expression "out.LeavingFrom=rules.flightCities;" assigns the value from the rule whose Id is flightCities to the property named LeavingFrom of the Rule Variable on the rule named bookFlight. 有关详细信息,请参阅 语义结果内容、 语法规则名称引用和 语法规则引用 引用。See Semantic Results Content, Grammar Rule Name Referencing, and Grammar Rule Reference Referencing for more information.
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 格式。The following is the XML form of the grammar generated by the code in the example above.
<?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.object 的默认语义格式符合 语音识别 (SISR) 版本1.0 的 W3C 语义解释,其中 tag 包含脚本的元素的格式为 semantics/1.0 。The default semantic format for System.Speech conforms to the W3C Semantic Interpretation for Speech Recognition (SISR) Version 1.0, where the format for tag elements that contain script is semantics/1.0. 必须 SrgsSemanticInterpretationTag 使用此格式指定对象的脚本。You must specify the script for SrgsSemanticInterpretationTag objects using this format. 语法为 semantics/1.0 :In the syntax of semantics/1.0:
包含规则元素的规则变量由 "out" 标识。The Rule Variable of the containing rule element is identified by "out".
有权访问包含规则元素外规则元素的规则变量的对象名称由 "规则" 标识。The name of the object that has access to the Rule Variable of rule elements outside the containing rule element is identified by "rules".
与查询文本匹配的最新引用规则的结果可由 " () 规则" 表示。The result from the latest referenced rule that matches the utterance can be represented by "rules.latest()".
你还可以在语法中将语义值与短语关联,而无需使用脚本,而使用 SrgsNameValueTag 对象。You can also associate a semantic value with a phrase in a grammar without using script, using the SrgsNameValueTag object.
构造函数
| SrgsSemanticInterpretationTag() |
创建 SrgsSemanticInterpretationTag 类的实例。Creates an instance of the SrgsSemanticInterpretationTag class. |
| SrgsSemanticInterpretationTag(String) |
创建 SrgsSemanticInterpretationTag 类的实例,指定标记的脚本内容。Creates an instance of the SrgsSemanticInterpretationTag class, specifying the script contents of the tag. |
属性
| Script |
获取或设置标记的 ECMAScript。Gets or sets the ECMAScript for the tag. |
方法
| CreateObjRef(Type) |
创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (继承自 MarshalByRefObject) |
| Equals(Object) |
确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object. (继承自 Object) |
| GetHashCode() |
作为默认哈希函数。Serves as the default hash function. (继承自 Object) |
| GetLifetimeService() |
已过时。
检索控制此实例的生存期策略的当前生存期服务对象。Retrieves the current lifetime service object that controls the lifetime policy for this instance. (继承自 MarshalByRefObject) |
| GetType() |
获取当前实例的 Type。Gets the Type of the current instance. (继承自 Object) |
| InitializeLifetimeService() |
已过时。
获取生存期服务对象来控制此实例的生存期策略。Obtains a lifetime service object to control the lifetime policy for this instance. (继承自 MarshalByRefObject) |
| MemberwiseClone() |
创建当前 Object 的浅表副本。Creates a shallow copy of the current Object. (继承自 Object) |
| MemberwiseClone(Boolean) |
创建当前 MarshalByRefObject 对象的浅表副本。Creates a shallow copy of the current MarshalByRefObject object. (继承自 MarshalByRefObject) |
| ToString() |
返回表示当前对象的字符串。Returns a string that represents the current object. (继承自 Object) |