SrgsRule.Add(SrgsElement) 方法

定义

SrgsElement 对象添加一个 SrgsRule

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

参数

element
SrgsElement

SrgsElement 继承并指定能识别的对象。

例外

elementnull

示例

以下示例创建一个语法,该语法识别短语“赢得世界杯的国家是”,后跟赢得世界杯的国家的名称。 创建 SrgsRule 对象 winnerRule 并为其提供字符串标识符 WorldCupWinner后,该示例使用 Add 方法将字符串“赢得世界杯冠军的国家是”追加到规则中。 然后,该示例创建两个附加规则, ruleEuroperuleSAmerica。 再次使用 Add 方法,该示例将 对象SrgsOneOf追加到WorldCupWinner包含对 和 ruleSAmerica的规则引用ruleEurope的规则中。

// 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 对象,这些对象指定说话人可以说什么。 有效的规则元素必须至少包含一段可识别文本或一个规则引用。 有关详细信息 ,请参阅使用 SrgsGrammar 创建语法

对象 SrgsRule 还可以包含 SrgsNameValueTag 向规则添加语义值和语义键的 和 SrgsSemanticInterpretationTag 对象。 语音识别引擎在识别短语时将返回语义信息以及识别的短语。 有关详细信息 ,请参阅语义解释标记

适用于

另请参阅