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添加约束,请使用 Add、、AppendAppendDictationAppendRuleReferenceAppendWildcard 方法和 Addition 运算符。

重要

使用包含具有相同键名称的重复语义元素或多个语义元素(可重复修改同一语义元素的值)的语音识别语法时,语音识别器可能会引发异常。

有关构建和使用语音识别语法的详细信息,请参阅 语音识别

GrammarBuilder()

Source:
GrammarBuilder.cs
Source:
GrammarBuilder.cs
Source:
GrammarBuilder.cs

初始化 GrammarBuilder 类的新的空实例。

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

示例

下面的示例使用 GrammarBuilderChoices 对象构造一个语法,该语法可以识别两个短语中的任意一个:“Make background colorChoice”或“Set background to colorChoice”。

该示例使用 Choices 对象从 对象数组String创建 colorChoice 的可接受值列表。 Choices对象类似于 one-of SRGS 规范中的 元素,并且包含一组备用短语,其中任何一个短语都可以在说话时被识别。 该示例还使用 对象将两GrammarBuilderChoices 对象的数组分组为生成的语法可以识别的一对备用短语。 替换词或短语是大多数语法的组件,对象 Choices 为使用 GrammarBuilder构造的语法提供此功能。

该示例最后从 从 GrammarBuilder 对象构造的 Choices 创建 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.
  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对象添加规则,请使用 Add、、AppendAppendDictationAppendRuleReferenceAppendWildcard 方法和 Addition 运算符。

重要

使用包含具有相同键名称的重复语义元素或多个语义元素(可重复修改同一语义元素的值)的语音识别语法时,语音识别器可能会引发异常。

另请参阅

适用于

GrammarBuilder(Choices)

Source:
GrammarBuilder.cs
Source:
GrammarBuilder.cs
Source:
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 对象构造一个语法,该语法可以识别两个短语中的任意一个:“Make background colorChoice”或“Set background to colorChoice”。

该示例使用 Choices 对象从 对象数组String创建 colorChoice 的可接受值列表。 Choices对象类似于 one-of SRGS 规范中的 元素,并且包含一组备用短语,其中任何一个短语都可以在说话时识别。 该示例还使用 对象将两GrammarBuilderChoices 对象的数组分组为生成的语法可以识别的一对备用短语。 替换词或短语是大多数语法的组件,对象 Choices 为使用 GrammarBuilder构造的语法提供此功能。

该示例最后从 从 GrammarBuilder 对象构造的 Choices 创建 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 from the result.
  Choices bothChoices = new Choices(new GrammarBuilder[] {makePhrase, setPhrase});
  Grammar grammar = new Grammar((GrammarBuilder)bothChoices);
  grammar.Name = "backgroundColor";
  return grammar;
}

注解

有关生成包含替代项的语音识别语法的详细信息,请参阅 Using Choices to Create a GrammarBuilder Grammar

另请参阅

适用于

GrammarBuilder(SemanticResultKey)

Source:
GrammarBuilder.cs
Source:
GrammarBuilder.cs
Source:
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中的约束。 最后,该示例从完成GrammarBuilder的 构造 对象Grammar

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实例时,向可在识别结果中返回的语法添加语义信息。 可以使用 的 属性访问识别结果 Semantics 中的语义信息,该属性 RecognizedPhrase在 事件的处理程序 SpeechRecognized 中可用。 GrammarBuilder如果 定义了 ,SemanticResultKey则这可用于检索与 键关联的识别结果中的语义信息。 请参阅 的示例 Append(SemanticResultKey),另请参阅 SemanticResultValueSemanticResultKey

重要

构造 GrammarBuilder 包含 SemanticResultValueSemanticResultKey 实例的对象时,请确保避免创建具有相同键名称的重复语义元素或可重复修改 Value 对象的属性的 SemanticValue 多个语义元素。 如果遇到这些情况,语音识别器可能会引发异常。

另请参阅

适用于

GrammarBuilder(SemanticResultValue)

Source:
GrammarBuilder.cs
Source:
GrammarBuilder.cs
Source:
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中的约束。 最后,该示例从完成GrammarBuilder的 构造 对象Grammar

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实例时,向可在识别结果中返回的语法添加语义信息。 可以使用 的 属性访问识别结果 Semantics 中的语义信息,该属性 RecognizedPhrase在 事件的处理程序 SpeechRecognized 中可用。 GrammarBuilder如果 定义了 ,SemanticResultKey则这可用于检索与 键关联的识别结果中的语义信息。 请参阅 的示例 Append(SemanticResultKey),另请参阅 SemanticResultValueSemanticResultKey

重要

构造 GrammarBuilder 包含 SemanticResultValueSemanticResultKey 实例的对象时,请确保避免创建具有相同键名称的重复语义元素或可重复修改 Value 对象的属性的 SemanticValue 多个语义元素。 如果遇到这些情况,语音识别器可能会引发异常。

另请参阅

适用于

GrammarBuilder(String)

Source:
GrammarBuilder.cs
Source:
GrammarBuilder.cs
Source:
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 对象构造一个语法,该语法可以识别两个短语中的任意一个:“Make background colorChoice”或“Set background to colorChoice”。

在使用 对象创建 colorChoice 的可接受值列表后,此示例使用字符串作为参数初始化两GrammarBuilder个对象 和 setPhrasemakePhraseChoices

该示例最终创建 GrammarChoices 转换为 对象的 对象 GrammarBuilder

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)

Source:
GrammarBuilder.cs
Source:
GrammarBuilder.cs
Source:
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 值为“一二三五六七”,则子序列语法可识别输入“二三四”,但不识别输入“一三五”。 但是,有序子集语法可识别这两个输入。

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)

Source:
GrammarBuilder.cs
Source:
GrammarBuilder.cs
Source:
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)

Source:
GrammarBuilder.cs
Source:
GrammarBuilder.cs
Source:
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,则 new GrammarBuilder 表示可选元素。

的值 minRepeat 必须大于或等于 0 且小于或等于 的值 maxRepeat。 有关生成包含字符串的语音识别语法的详细信息,请参阅 使用字符串创建 GrammarBuilder 语法

另请参阅

适用于