SrgsRule.Add(SrgsElement) 方法


SrgsElement 对象添加一个 SrgsRuleAdds an SrgsElement to an SrgsRule object.

 void Add(System::Speech::Recognition::SrgsGrammar::SrgsElement ^ element);
public void Add (System.Speech.Recognition.SrgsGrammar.SrgsElement element);
member this.Add : System.Speech.Recognition.SrgsGrammar.SrgsElement -> unit
Public Sub Add (element As SrgsElement)



SrgsElement 继承并指定能识别的对象。An object that inherits from SrgsElement and specifies what can be recognized.


elementnullelement is null.


下面的示例创建一个语法,用于识别 "已赢得世界杯的国家" 的短语,后跟赢得世界杯的国家/地区的名称。The following example creates a grammar that recognizes the phrase "A nation that has won the World Cup is" followed by the name of a country that has won the World Cup. 创建 SrgsRule 对象 winnerRule 并向其提供字符串标识符 WorldCupWinner 后,该示例使用 Add 方法将“已赢得世界杯的国家/地区”字符串添加到规则中。After creating the SrgsRule object winnerRule and giving it the string identifier WorldCupWinner, the example uses the Add method to append the string "A nation that has won the World Cup is" to the rule. 然后,该示例将创建另外两个规则: ruleEuroperuleSAmericaThe example then creates two additional rules, ruleEurope and ruleSAmerica. 再次使用 Add 方法,该示例将一个 SrgsOneOf 对象追加到 WorldCupWinner 包含和的规则引用的规则 ruleEurope ruleSAmericaAgain using the Add method, the example appends an SrgsOneOf object to the WorldCupWinner rule that contains rule references to ruleEurope and ruleSAmerica.

// Create an SrgsDocument, create a new rule  
// and set its scope to public.  
SrgsDocument document = new SrgsDocument();  
SrgsRule winnerRule = new SrgsRule("WorldCupWinner");  
winnerRule.Scope = SrgsRuleScope.Public;  

// Add the introduction.  
winnerRule.Elements.Add(new SrgsItem("A nation that has won the world cup is: "));  

// Create the rule for the European nations.  
SrgsOneOf oneOfEurope = new SrgsOneOf(new SrgsItem[] {new SrgsItem("England"), new SrgsItem("France"), new SrgsItem("Germany"), new SrgsItem("Italy")});  
SrgsRule ruleEurope = (new SrgsRule("EuropeanNations", new SrgsElement[] {oneOfEurope}));  

// Create the rule for the South American nations.  
SrgsOneOf oneOfSAmerica = new SrgsOneOf(new SrgsItem[] {new SrgsItem("Argentina"), new SrgsItem("Brazil"), new SrgsItem("Uruguay")});  
SrgsRule ruleSAmerica = (new SrgsRule("SouthAmericanNations", new SrgsElement[] {oneOfSAmerica}));  

// Add references to winnerRule for ruleEurope and ruleSAmerica.  
winnerRule.Elements.Add(new SrgsOneOf(new SrgsItem[] {(new SrgsItem (new SrgsRuleRef(ruleEurope))), new SrgsItem(new SrgsRuleRef(ruleSAmerica))}));  

// Add all the rules to the document and make winnerRule   
// the root rule of the document.  
document.Rules.Add(new SrgsRule[] {winnerRule, ruleEurope, ruleSAmerica});  
document.Root = winnerRule;  


对象的典型添加内容 SrgsRule 包括 SrgsItemSrgsOneOfSrgsRuleRefSrgsToken 对象,这些对象指定扬声器可以说的内容。Typical additions to an SrgsRule object include SrgsItem, SrgsOneOf, SrgsRuleRef, and SrgsToken objects that specify what speakers can say. 有效的规则元素必须至少包含一段可识别文本或一个规则引用。A valid rule element must contain at least one piece of recognizable text or one rule reference. 有关详细信息,请参阅 使用 SrgsGrammar 创建语法See Create Grammars Using SrgsGrammar for more information.

SrgsRule对象也可以包含 SrgsNameValueTag SrgsSemanticInterpretationTag 将语义值和语义键添加到规则的对象。An SrgsRule object may also contain SrgsNameValueTag and SrgsSemanticInterpretationTag objects that add semantic values and semantic keys to the rule. 语音识别引擎将在识别到短语后返回语义信息,以及识别的短语。The speech recognition engine will return the semantic information, as well as the recognized phrase, when it recognizes the phrase. 有关详细信息,请参阅 语义解释标记See Semantic Interpretation Markup for more information.