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

範例

下列範例會建立一個文法,以辨識「已贏得世界盃的國家/地區」片語,後面接著已贏得 World Cup 的國家/地區名稱。 建立 SrgsRule 物件 winnerRule 並為其提供字串識別碼 WorldCupWinner 之後,此範例會使用 Add 方法,將「已贏得 World Cup 的一個國家」字串附加至規則。 然後,此範例會建立兩個額外的規則和 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 也可能包含 SrgsNameValueTagSrgsSemanticInterpretationTag 物件,這些物件會將語意值和語意索引鍵新增至規則。 語音辨識引擎會在辨識片語時傳回語意資訊以及辨識的片語。 如需詳細資訊,請參閱 語意解譯標記

適用於

另請參閱