Share via


GrammarBuilder 建構函式

定義

初始化 GrammarBuilder 類別的新執行個體。

多載

GrammarBuilder()

初始化 GrammarBuilder 類別的新的空執行個體。

GrammarBuilder(Choices)

從一組替代項目初始化 GrammarBuilder 類別的新執行個體。

GrammarBuilder(SemanticResultKey)

從語意索引鍵初始化 GrammarBuilder 類別的新執行個體。

GrammarBuilder(SemanticResultValue)

從語意值初始化 GrammarBuilder 類別的新執行個體。

GrammarBuilder(String)

從單字序列初始化 GrammarBuilder 類別的新執行個體。

GrammarBuilder(String, SubsetMatchingMode)

針對單字序列的子集,初始化 GrammarBuilder 類別的新執行個體。

GrammarBuilder(GrammarBuilder, Int32, Int32)

從重複的項目初始化 GrammarBuilder 類別的新執行個體。

GrammarBuilder(String, Int32, Int32)

GrammarBuilder 中的單字序列,初始化 String 類別的新執行個體,並指定 String 可重複次數。

備註

這個類別的實例也可以透過從其他類別隱含轉換,或將 物件與第二個 GrammarBuilder 物件結合至新的 GrammarBuilder 來取得。 如需詳細資訊,請參閱 ImplicitAddition 方法。

若要將條件約束新增至現有的 GrammarBuilder ,請使用 AddAppendAppendDictationAppendRuleReference 、 和 方法,以及 AppendWildcardAddition 運算子。

重要

使用包含相同索引鍵名稱的重複語意元素的語音辨識文法,或多個可重複修改相同語意元素值的語意元素時,語音辨識器可能會擲回例外狀況。

如需建置和使用語音辨識文法的詳細資訊,請參閱 語音辨識

GrammarBuilder()

來源:
GrammarBuilder.cs
來源:
GrammarBuilder.cs
來源:
GrammarBuilder.cs

初始化 GrammarBuilder 類別的新的空執行個體。

public:
 GrammarBuilder();
public GrammarBuilder ();
Public Sub New ()

範例

下列範例會使用 GrammarBuilderChoices 物件來建構文法,以辨識這兩個片語的任一個片語:「製作背景 色彩Choice」或「將背景設定為 colorChoice」。

此範例會使用 Choices 物件,從 物件的陣列 String 建立colorChoice可接受的值清單。 Choices物件類似于 one-of SRGS 規格中的 專案,並包含一組替代片語,其中任一個片語都可以在說出時加以辨識。 此範例也會使用 Choices 物件,將兩 GrammarBuilder 個物件的陣列分組成一對替代片語,讓結果文法可以辨識。 替代字組或片語是大部分文法的元件,而 Choices 物件會針對使用 GrammarBuilder 建構的文法提供這項功能。

此範例最後 Grammar 會從 從 GrammarBuilder 物件建構 Choices 建立 物件。

private Grammar CreateColorGrammar()
{

  // Create a set of color choices.
  Choices colorChoice = new Choices(new string[] {"red", "green", "blue"});
  GrammarBuilder colorElement = new GrammarBuilder(colorChoice);

  // Create grammar builders for the two versions of the phrase.
  GrammarBuilder makePhrase = new GrammarBuilder("Make background");
  makePhrase.Append(colorElement);
  GrammarBuilder setPhrase = new GrammarBuilder("Set background to");
  setPhrase.Append(colorElement);

  // Create a Choices for the two alternative phrases, convert the Choices
  // to a GrammarBuilder, and construct the Grammar object from the result.
  GrammarBuilder bothPhrases = new GrammarBuilder();
  Choices bothChoices = new Choices(new GrammarBuilder[] {makePhrase, setPhrase});
  bothPhrases.Append(bothChoices);
  Grammar grammar = new Grammar(bothPhrases);
  grammar.Name = "backgroundColor";
  return grammar;
}

備註

若要將規則新增至現有的 GrammarBuilder 物件,請使用 AddAppendAppendDictationAppendRuleReference 和 方法,以及 AppendWildcardAddition 運算子。

重要

使用包含相同索引鍵名稱的重複語意元素的語音辨識文法,或多個可重複修改相同語意元素值的語意元素時,語音辨識器可能會擲回例外狀況。

另請參閱

適用於

GrammarBuilder(Choices)

來源:
GrammarBuilder.cs
來源:
GrammarBuilder.cs
來源:
GrammarBuilder.cs

從一組替代項目初始化 GrammarBuilder 類別的新執行個體。

public:
 GrammarBuilder(System::Speech::Recognition::Choices ^ alternateChoices);
public GrammarBuilder (System.Speech.Recognition.Choices alternateChoices);
new System.Speech.Recognition.GrammarBuilder : System.Speech.Recognition.Choices -> System.Speech.Recognition.GrammarBuilder
Public Sub New (alternateChoices As Choices)

參數

alternateChoices
Choices

替代方式的集合。

範例

下列範例會使用 GrammarBuilderChoices 物件來建構文法,以辨識這兩個片語的任一個片語:「製作背景 色彩Choice」或「將背景設定為 colorChoice」。

此範例會使用 Choices 物件,從 物件的陣列 String 建立colorChoice可接受的值清單。 Choices物件類似于 one-of SRGS 規格中的 專案,並包含一組替代片語,其中任一片語都可以在說話時加以辨識。 此範例也會使用 Choices 物件,將兩 GrammarBuilder 個物件的陣列分組成一對替代片語,讓結果文法可以辨識。 替代字組或片語是大部分文法的元件,而 Choices 物件會針對使用 GrammarBuilder 建構的文法提供這項功能。

此範例最後 Grammar 會從 從 GrammarBuilder 物件建構 Choices 建立 物件。

private Grammar CreateColorGrammar()
{

  // Create a set of color choices.
  Choices colorChoice = new Choices(new string[] {"red", "green", "blue"});
  GrammarBuilder colorElement = new GrammarBuilder(colorChoice);

  // Create grammar builders for the two versions of the phrase.
  GrammarBuilder makePhrase = new GrammarBuilder("Make background");
  makePhrase.Append(colorElement);
  GrammarBuilder setPhrase = new GrammarBuilder("Set background to");
  setPhrase.Append(colorElement);

  // Create a Choices for the two alternative phrases, convert the Choices
  // to a GrammarBuilder, and construct the grammar from the result.
  Choices bothChoices = new Choices(new GrammarBuilder[] {makePhrase, setPhrase});
  Grammar grammar = new Grammar((GrammarBuilder)bothChoices);
  grammar.Name = "backgroundColor";
  return grammar;
}

備註

如需建置包含替代方案的語音辨識文法的詳細資訊,請參閱 使用選擇建立文法建置工具文法

另請參閱

適用於

GrammarBuilder(SemanticResultKey)

來源:
GrammarBuilder.cs
來源:
GrammarBuilder.cs
來源:
GrammarBuilder.cs

從語意索引鍵初始化 GrammarBuilder 類別的新執行個體。

public:
 GrammarBuilder(System::Speech::Recognition::SemanticResultKey ^ key);
public GrammarBuilder (System.Speech.Recognition.SemanticResultKey key);
new System.Speech.Recognition.GrammarBuilder : System.Speech.Recognition.SemanticResultKey -> System.Speech.Recognition.GrammarBuilder
Public Sub New (key As SemanticResultKey)

參數

key
SemanticResultKey

語意索引鍵。

範例

下列範例會建立語音辨識文法,以辨識這兩個片語「製作背景 色彩選擇」和「設定背景為 colorChoice」,其中 colorChoice 是從一組色彩中選取。 文法可讓使用者說出數個色彩名稱中的任何一個,並將已辨識色彩名稱的語意資訊傳回給應用程式。

此範例會使用單 SemanticResultKey 一 ,您可以擷取 SemanticValue 與使用者說出的色彩相關聯的 。 例如,如果輸入包含片語「設定背景為紅色」,辨識結果會包含 「#FF0000」 的語意值,您可以使用事件的處理常式來 SpeechRecognized 擷取。

此範例會使用 StringChoicesSemanticResultKeySemanticResultValueGrammarBuilder 物件來建置最後一個 GrammarBuilder 物件 bothPhrases 中包含的條件約束。 最後,此範例會 Grammar 從已完成 GrammarBuilder 的 建構 物件。

private Grammar CreateColorGrammar()
{

  // Create a set of color choices.
  // Include semantic information about each of the colors.
  Choices colorChoice = new Choices();

  GrammarBuilder colorBuilder = new GrammarBuilder("red");
  SemanticResultValue colorValue =
    new SemanticResultValue(colorBuilder, "#FF0000");
  colorChoice.Add(new GrammarBuilder(colorValue));

  colorBuilder = new GrammarBuilder("green");
  colorValue = new SemanticResultValue(colorBuilder, "#00FF00");
  colorChoice.Add(new GrammarBuilder(colorValue));

  colorBuilder = new GrammarBuilder("blue");
  colorValue = new SemanticResultValue(colorBuilder, "#0000FF");
  colorChoice.Add(new GrammarBuilder(colorValue));

  GrammarBuilder colorElement = new GrammarBuilder(colorChoice);

  // Create grammar builders for the two versions of the phrase.
  GrammarBuilder makePhrase = new GrammarBuilder("Make background");
  makePhrase.Append(colorElement);
  GrammarBuilder setPhrase = new GrammarBuilder("Set background to");
  setPhrase.Append(colorElement);

  // Create a Choices object for the two alternative phrases.
  Choices bothChoices = new Choices(new GrammarBuilder[] {makePhrase, setPhrase});
  GrammarBuilder bothPhrases = new GrammarBuilder(bothChoices);

  // Create the semantic key for referencing the color information.
  SemanticResultKey colorKey =
    new SemanticResultKey("ColorCode", bothPhrases);
  bothPhrases = new GrammarBuilder(colorKey);

  // Construct the Grammar object from the GrammarBuilder.
  Grammar grammar = new Grammar(bothPhrases);
  grammar.Name = "backgroundColor";
  return grammar;
}

備註

當您從 SemanticResultValue 物件建立 GrammarBuilder 實例時,會將語意資訊新增至可在辨識結果中傳回的文法。 您可以使用 的 RecognizedPhrase 屬性,存取 Semantics 辨識結果中的語意資訊,該屬性可在 事件的處理常式 SpeechRecognized 中使用。 GrammarBuilder如果 定義 SemanticResultKey ,這可用來擷取與索引鍵相關聯之辨識結果中的語意資訊。 請參閱 的 Append(SemanticResultKey) 範例,另請參閱 SemanticResultValueSemanticResultKey

重要

當您建構 GrammarBuilder 包含 SemanticResultValueSemanticResultKey 實例的物件時,請務必避免建立具有相同索引鍵名稱的重複語意專案,或多個可以重複修改 Value 物件屬性的 SemanticValue 語意專案。 如果遇到這些情況,語音辨識器可能會擲回例外狀況。

另請參閱

適用於

GrammarBuilder(SemanticResultValue)

來源:
GrammarBuilder.cs
來源:
GrammarBuilder.cs
來源:
GrammarBuilder.cs

從語意值初始化 GrammarBuilder 類別的新執行個體。

public:
 GrammarBuilder(System::Speech::Recognition::SemanticResultValue ^ value);
public GrammarBuilder (System.Speech.Recognition.SemanticResultValue value);
new System.Speech.Recognition.GrammarBuilder : System.Speech.Recognition.SemanticResultValue -> System.Speech.Recognition.GrammarBuilder
Public Sub New (value As SemanticResultValue)

參數

value
SemanticResultValue

語意值或名稱/值組。

範例

下列範例會建立語音辨識文法,以辨識這兩個片語「製作背景 色彩選擇」和「設定背景為 colorChoice」,其中 colorChoice 是從一組色彩中選取。 文法可讓使用者說出數個色彩名稱中的任何一個,並將已辨識色彩名稱的語意資訊傳回給應用程式。

此範例會使用單 SemanticResultKey 一 ,您可以擷取 SemanticValue 與使用者說出的色彩相關聯的 。 例如,如果輸入包含片語「設定背景為紅色」,辨識結果會包含 「#FF0000」 的語意值,您可以使用事件的處理常式來 SpeechRecognized 擷取。

此範例會使用 StringChoicesSemanticResultKeySemanticResultValueGrammarBuilder 物件來建置最後一個 GrammarBuilder 物件 bothPhrases 中包含的條件約束。 最後,此範例會 Grammar 從已完成 GrammarBuilder 的 建構 物件。

private Grammar CreateColorGrammar()
{

  // Create a set of color choices.
  // Include semantic information about each of the colors.
  Choices colorChoice = new Choices();

  GrammarBuilder colorBuilder = new GrammarBuilder("red");
  SemanticResultValue colorValue =
    new SemanticResultValue(colorBuilder, "#FF0000");
  colorChoice.Add(new GrammarBuilder(colorValue));

  colorBuilder = new GrammarBuilder("green");
  colorValue = new SemanticResultValue(colorBuilder, "#00FF00");
  colorChoice.Add(new GrammarBuilder(colorValue));

  colorBuilder = new GrammarBuilder("blue");
  colorValue = new SemanticResultValue(colorBuilder, "#0000FF");
  colorChoice.Add(new GrammarBuilder(colorValue));

  GrammarBuilder colorElement = new GrammarBuilder(colorChoice);

  // Create grammar builders for the two versions of the phrase.
  GrammarBuilder makePhrase = new GrammarBuilder("Make background");
  makePhrase.Append(colorElement);
  GrammarBuilder setPhrase = new GrammarBuilder("Set background to");
  setPhrase.Append(colorElement);

  // Create a Choices for the two alternative phrases.
  Choices bothChoices = new Choices(new GrammarBuilder[] {makePhrase, setPhrase});
  GrammarBuilder bothPhrases = new GrammarBuilder(bothChoices);

  // Create the semantic key for referencing the color information.
  SemanticResultKey colorKey =
    new SemanticResultKey("ColorCode", bothPhrases);
  bothPhrases = new GrammarBuilder(colorKey);

  // Construct the grammar from the grammar builder.
  Grammar grammar = new Grammar(bothPhrases);
  grammar.Name = "backgroundColor";
  return grammar;
}

備註

當您從 SemanticResultValue 物件建立 GrammarBuilder 實例時,會將語意資訊新增至可在辨識結果中傳回的文法。 您可以使用 的 RecognizedPhrase 屬性,存取 Semantics 辨識結果中的語意資訊,該屬性可在 事件的處理常式 SpeechRecognized 中使用。 GrammarBuilder如果 定義 SemanticResultKey ,這可用來擷取與索引鍵相關聯之辨識結果中的語意資訊。 請參閱 的 Append(SemanticResultKey) 範例,另請參閱 SemanticResultValueSemanticResultKey

重要

當您建構 GrammarBuilder 包含 SemanticResultValueSemanticResultKey 實例的物件時,請務必避免建立具有相同索引鍵名稱的重複語意專案,或多個可以重複修改 Value 物件屬性的 SemanticValue 語意專案。 如果遇到這些情況,語音辨識器可能會擲回例外狀況。

另請參閱

適用於

GrammarBuilder(String)

來源:
GrammarBuilder.cs
來源:
GrammarBuilder.cs
來源:
GrammarBuilder.cs

從單字序列初始化 GrammarBuilder 類別的新執行個體。

public:
 GrammarBuilder(System::String ^ phrase);
public GrammarBuilder (string phrase);
new System.Speech.Recognition.GrammarBuilder : string -> System.Speech.Recognition.GrammarBuilder
Public Sub New (phrase As String)

參數

phrase
String

單字序列。

範例

下列範例會使用 GrammarBuilderChoices 物件來建構文法,以辨識這兩個片語的任一個片語:「製作背景 色彩Choice」或「將背景設定為 colorChoice」。

使用 物件建立colorChoice可接受的值清單之後,此範例會初始化兩 GrammarBuilderChoices 物件, makePhrase 並使用 setPhrase 字串作為引數。

此範例最後會從 Choices 物件轉換成 GrammarBuilder 物件建立 Grammar 物件。

private Grammar CreateColorGrammar()
{

  // Create a set of color choices.
  Choices colorChoice = new Choices(new string[] {"red", "green", "blue"});
  GrammarBuilder colorElement = new GrammarBuilder(colorChoice);

  // Create grammar builders for the two versions of the phrase.
  GrammarBuilder makePhrase = new GrammarBuilder("Make background");
  makePhrase.Append(colorElement);
  GrammarBuilder setPhrase = new GrammarBuilder("Set background to");
  setPhrase.Append(colorElement);

  // Create a Choices for the two alternative phrases, convert the Choices
  // to a GrammarBuilder, and construct the Grammar object from the result.
  Choices bothChoices = new Choices(new GrammarBuilder[] {makePhrase, setPhrase});
  Grammar grammar = new Grammar((GrammarBuilder)bothChoices);
  grammar.Name = "backgroundColor";
  return grammar;
}

備註

片語代表語音辨識文法可以辨識的確切口語片語。 如需建置包含字串的語音辨識文法的詳細資訊,請參閱 使用字串建立 GrammarBuilder 文法

另請參閱

適用於

GrammarBuilder(String, SubsetMatchingMode)

來源:
GrammarBuilder.cs
來源:
GrammarBuilder.cs
來源:
GrammarBuilder.cs

針對單字序列的子集,初始化 GrammarBuilder 類別的新執行個體。

public:
 GrammarBuilder(System::String ^ phrase, System::Speech::Recognition::SubsetMatchingMode subsetMatchingCriteria);
public GrammarBuilder (string phrase, System.Speech.Recognition.SubsetMatchingMode subsetMatchingCriteria);
new System.Speech.Recognition.GrammarBuilder : string * System.Speech.Recognition.SubsetMatchingMode -> System.Speech.Recognition.GrammarBuilder
Public Sub New (phrase As String, subsetMatchingCriteria As SubsetMatchingMode)

參數

phrase
String

單字序列。

subsetMatchingCriteria
SubsetMatchingMode

語音辨識文法用來辨識片語的比對模式。

範例

下列範例會為每個 SubsetMatchingMode 值建立語音辨識文法,以及用於選擇比對模式文法的文法。 如果 的值 phrase 是 「one two two three three four five five six six7」,Subsequence 文法會辨識輸入 「two three three four」,但不能辨識輸入 「one three three five」。 不過,已排序子集文法可辨識這兩個輸入。

private static IEnumerable<Grammar>
  CreateMatchingModeGrammars(string phrase)
{
  List<Grammar> grammars = new List<Grammar>(5);

  Choices modeChoice = new Choices();
  Type enumType = typeof(SubsetMatchingMode);
  foreach (SubsetMatchingMode mode in Enum.GetValues(enumType))
  {
    string modeName = Enum.GetName(enumType, mode);
    modeName = BreakAtCaps(modeName);

    GrammarBuilder builder = new GrammarBuilder(phrase, mode);
    Grammar modeGrammar = new Grammar(builder);
    modeGrammar.Name = modeName;
    modeGrammar.Enabled = false;
    grammars.Add(modeGrammar);

    modeChoice.Add(modeName);
  }

  Grammar choiceGrammar = new Grammar(modeChoice);
  choiceGrammar.Name = "choice";
  grammars.Add(choiceGrammar);

  return grammars;
}

// Insert spaces preceding each uppercase letter in a string.
private static string BreakAtCaps(string item)
{
  if (item == null || item.Length == 0)
  {
    return item;
  }

  StringBuilder sb = new StringBuilder(item[0].ToString());
  for (int i = 1; i < item.Length; i++)
  {
    char c = item[i];
    if (char.IsUpper(c))
    {
      sb.Append(" ");
    }
    sb.Append(c);
  }

  return sb.ToString();
}

備註

參數 phrase 代表語音辨識文法可以辨識的片語。 參數 subsetMatchingMode 會指定片語的子集,可說出以達到整個片語的成功辨識。 您可以使用此選項來建立文法,其中包含具有長名稱的專案清單,而不需要使用者說出整個名稱以符合專案。

如需比對模式的詳細資訊,請參閱 SubsetMatchingMode 。 如需建置包含字串的語音辨識文法的詳細資訊,請參閱 使用字串建立 GrammarBuilder 文法

另請參閱

適用於

GrammarBuilder(GrammarBuilder, Int32, Int32)

來源:
GrammarBuilder.cs
來源:
GrammarBuilder.cs
來源:
GrammarBuilder.cs

從重複的項目初始化 GrammarBuilder 類別的新執行個體。

public:
 GrammarBuilder(System::Speech::Recognition::GrammarBuilder ^ builder, int minRepeat, int maxRepeat);
public GrammarBuilder (System.Speech.Recognition.GrammarBuilder builder, int minRepeat, int maxRepeat);
new System.Speech.Recognition.GrammarBuilder : System.Speech.Recognition.GrammarBuilder * int * int -> System.Speech.Recognition.GrammarBuilder
Public Sub New (builder As GrammarBuilder, minRepeat As Integer, maxRepeat As Integer)

參數

builder
GrammarBuilder

重複的項目。

minRepeat
Int32

為構成相符項目,輸入必須和 builder 所定義的項目比對相符的最少次數。

maxRepeat
Int32

為構成相符項目,輸入可以和 builder 所定義的項目比對相符的最大次數。

範例

下列範例會建立語音辨識文法來訂購披薩。 其開頭為選擇性的開頭片語,後面接著一到四個配料,並以 「pizza」 一詞關閉。

private static Grammar CreatePizzaGrammar()
{

  // Create a Choices object from a string array of alternative toppings.
  Choices toppings = new Choices(new string[] {
    "cheese", "mushroom", "tomato", "onion",
    "anchovy", "chicken", "pepperoni"});

  // Create a GrammarBuilder and append the Choices object.
  GrammarBuilder andToppings = new GrammarBuilder("and", 0, 1);
  andToppings.Append(toppings);

  // Construct the phrase.
  GrammarBuilder gb = new GrammarBuilder("I would like a", 0, 1);
  gb.Append(toppings);
  gb.Append(new GrammarBuilder(andToppings, 0, 3));
  gb.Append("pizza");

  // Create the Grammar from the GrammarBuilder.
  Grammar grammar = new Grammar(gb);
  grammar.Name = "Pizza Order";

  return grammar;
}

備註

如果 的值為 minRepeat 0,則新的 GrammarBuilder 代表選擇性專案。

的值 minRepeat 必須大於或等於 0,且小於或等於 的值 maxRepeat

重要

當您為 GrammarBuilder 包含 SemanticResultValueSemanticResultKey 實例的物件指定重複時,請確定您避免建立具有相同索引鍵名稱的重複語意專案,或多個可重複修改 Value 物件屬性的 SemanticValue 語意專案。 如果遇到這些情況,語音辨識器可能會擲回例外狀況。

另請參閱

適用於

GrammarBuilder(String, Int32, Int32)

來源:
GrammarBuilder.cs
來源:
GrammarBuilder.cs
來源:
GrammarBuilder.cs

GrammarBuilder 中的單字序列,初始化 String 類別的新執行個體,並指定 String 可重複次數。

public:
 GrammarBuilder(System::String ^ phrase, int minRepeat, int maxRepeat);
public GrammarBuilder (string phrase, int minRepeat, int maxRepeat);
new System.Speech.Recognition.GrammarBuilder : string * int * int -> System.Speech.Recognition.GrammarBuilder
Public Sub New (phrase As String, minRepeat As Integer, maxRepeat As Integer)

參數

phrase
String

單字的重複序列。

minRepeat
Int32

為構成相符項目,輸入必須和片語比對相符的最少次數。

maxRepeat
Int32

為構成相符項目,輸入可以和片語比對相符的最大次數。

範例

下列範例會建立語音辨識文法來訂購披薩。 其開頭為選擇性的開頭片語,後面接著一到四個配料,並以 「pizza」 一詞關閉。

private static Grammar CreatePizzaGrammar()
{

  // Create a Choices object with alternatives for toppings.
  Choices toppings = new Choices(new string[] {
    "cheese", "mushroom", "tomato", "onion",
    "anchovy", "chicken", "pepperoni"});

  // Create a GrammarBuilder and append the Choices object.
  GrammarBuilder andToppings = new GrammarBuilder("and", 0, 1);
  andToppings.Append(toppings);

  // Construct the phrase.
  GrammarBuilder gb = new GrammarBuilder("I would like a", 0, 1);
  gb.Append(toppings);
  gb.Append(new GrammarBuilder(andToppings, 0, 3));
  gb.Append("pizza");

  // Create the Grammar from the GrammarBuilder.
  Grammar grammar = new Grammar(gb);
  grammar.Name = "Pizza Order";

  return grammar;
}

備註

如果 的值為 minRepeat 0,則新的 GrammarBuilder 代表選擇性專案。

的值 minRepeat 必須大於或等於 0,且小於或等於 的值 maxRepeat 。 如需建置包含字串的語音辨識文法的詳細資訊,請參閱 使用字串建立 GrammarBuilder 文法

另請參閱

適用於