Share via


GrammarBuilder.Add 方法

定义

创建包含两个语法元素序列的新 GrammarBuilder

重载

Add(Choices, GrammarBuilder)

创建一个新的 GrammarBuilder,其中包含一个 Choices 对象以及跟随在此对象后面的一个 GrammarBuilder 对象。

Add(GrammarBuilder, Choices)

创建一个新的 GrammarBuilder,其中包含一个 GrammarBuilder 对象以及跟随在此对象后面的一个 Choices 对象。

Add(GrammarBuilder, GrammarBuilder)

创建包含两个 GrammarBuilder 对象序列的新 GrammarBuilder

Add(GrammarBuilder, String)

创建包含后跟一个短语的 GrammarBuilder 对象的新 GrammarBuilder

Add(String, GrammarBuilder)

创建包含 GrammarBuilder 对象后跟的一个短语的新 GrammarBuilder

注解

静态 Add 方法提供了另一种机制,通过该机制可以组合各种类型,以在使用 GrammarBuilder构建的语法中创建多样性和灵活性。 这些方法对应于静态 Addition 方法,静态方法也在 类上 GrammarBuilder 定义。 参数的顺序决定了新 GrammarBuilder中元素的顺序。

GrammarBuilder也可以从 Choices、、 SemanticResultKeySemanticResultValueString 对象获取 。 有关详细信息, Implicit 请参阅 和 Addition 运算符。

重要

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

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

Add(Choices, GrammarBuilder)

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

创建一个新的 GrammarBuilder,其中包含一个 Choices 对象以及跟随在此对象后面的一个 GrammarBuilder 对象。

public:
 static System::Speech::Recognition::GrammarBuilder ^ Add(System::Speech::Recognition::Choices ^ choices, System::Speech::Recognition::GrammarBuilder ^ builder);
public static System.Speech.Recognition.GrammarBuilder Add (System.Speech.Recognition.Choices choices, System.Speech.Recognition.GrammarBuilder builder);
static member Add : System.Speech.Recognition.Choices * System.Speech.Recognition.GrammarBuilder -> System.Speech.Recognition.GrammarBuilder
Public Shared Function Add (choices As Choices, builder As GrammarBuilder) As GrammarBuilder

参数

choices
Choices

第一个语法元素,表示一组替代项。

builder
GrammarBuilder

第二个语法元素。

返回

一个 GrammarBuilder,顺序为 choices 元素后面跟随一个 builder 元素。

注解

GrammarBuilder 支持来自以下类的隐式转换:

此方法接受上面 builder 列出的 参数对象。

有关详细信息, Implicit 请参阅 和 Addition 运算符。

重要

合并Choices包含 SemanticResultValueSemanticResultKey 实例的 和 GrammarBuilder 对象时,请确保避免创建具有相同键名称的重复语义元素或可重复修改Value对象的属性的SemanticValue多个语义元素。 如果语音识别器遇到这些情况,可能会引发异常。 有关生成包含语义信息的语音识别语法的详细信息,请参阅 向 GrammarBuilder 语法添加语义

另请参阅

适用于

Add(GrammarBuilder, Choices)

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

创建一个新的 GrammarBuilder,其中包含一个 GrammarBuilder 对象以及跟随在此对象后面的一个 Choices 对象。

public:
 static System::Speech::Recognition::GrammarBuilder ^ Add(System::Speech::Recognition::GrammarBuilder ^ builder, System::Speech::Recognition::Choices ^ choices);
public static System.Speech.Recognition.GrammarBuilder Add (System.Speech.Recognition.GrammarBuilder builder, System.Speech.Recognition.Choices choices);
static member Add : System.Speech.Recognition.GrammarBuilder * System.Speech.Recognition.Choices -> System.Speech.Recognition.GrammarBuilder
Public Shared Function Add (builder As GrammarBuilder, choices As Choices) As GrammarBuilder

参数

builder
GrammarBuilder

第一个语法元素。

choices
Choices

第一个表示替换系列的语法元素。

返回

一个 GrammarBuilder,顺序为 builder 元素后面跟随一个 choices 元素。

示例

以下示例创建一个语音识别语法,该语法可以识别两个短语:“制作背景 ”和“将背景设置为 颜色”,其中 颜色 是从一组颜色中选择的。 各种类型用于生成最终语法,例如 StringChoicesGrammarBuilder 对象。 对 方法的调用 Add 中的显式强制转换运算符是可选的。

private Grammar CreateColorGrammar()
{

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

  // Create grammar builders for the two versions of the phrase.
  GrammarBuilder makePhrase =
    GrammarBuilder.Add((GrammarBuilder)"Make background", colorChoice);
  GrammarBuilder setPhrase =
    GrammarBuilder.Add("Set background to", (GrammarBuilder)colorChoice);

  // 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});
  GrammarBuilder bothPhrases = new GrammarBuilder(bothChoices);

  Grammar grammar = new Grammar(bothPhrases);
  grammar.Name = "backgroundColor";
  return grammar;
}

注解

GrammarBuilder 支持来自以下类的隐式转换:

此方法接受上面 builder 列出的 参数对象。

有关详细信息, Implicit 请参阅 和 Addition 运算符。

重要

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

另请参阅

适用于

Add(GrammarBuilder, GrammarBuilder)

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

创建包含两个 GrammarBuilder 对象序列的新 GrammarBuilder

public:
 static System::Speech::Recognition::GrammarBuilder ^ Add(System::Speech::Recognition::GrammarBuilder ^ builder1, System::Speech::Recognition::GrammarBuilder ^ builder2);
public static System.Speech.Recognition.GrammarBuilder Add (System.Speech.Recognition.GrammarBuilder builder1, System.Speech.Recognition.GrammarBuilder builder2);
static member Add : System.Speech.Recognition.GrammarBuilder * System.Speech.Recognition.GrammarBuilder -> System.Speech.Recognition.GrammarBuilder
Public Shared Function Add (builder1 As GrammarBuilder, builder2 As GrammarBuilder) As GrammarBuilder

参数

builder1
GrammarBuilder

第一个语法元素。

builder2
GrammarBuilder

第二个语法元素。

返回

一个 GrammarBuilder,顺序为 builder1 元素后面跟随一个 builder2 元素。

示例

以下示例创建一个语音识别语法,该语法可以识别两个短语:“制作背景 ”和“将背景设置为 颜色”,其中 颜色 是从一组颜色中选择的。 各种类型用于生成最终语法,例如 StringChoicesGrammarBuilder 对象。 对 方法的调用 Add 中的显式强制转换运算符是可选的。

private Grammar CreateColorGrammar()
{

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

  // Create grammar builders for the two versions of the phrase.
  GrammarBuilder makePhrase =
    GrammarBuilder.Add((GrammarBuilder)"Make background", colorChoice);
  GrammarBuilder setPhrase =
    GrammarBuilder.Add("Set background to", (GrammarBuilder)colorChoice);

  // 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});
  GrammarBuilder bothPhrases = new GrammarBuilder(bothChoices);

  Grammar grammar = new Grammar(bothPhrases);
  grammar.Name = "backgroundColor";
  return grammar;
}

注解

GrammarBuilder 支持来自以下类的隐式转换:

此方法接受上面 builder1 为 或 builder2 参数列出的 对象。

有关详细信息, Implicit 请参阅 和 Addition 运算符。

重要

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

另请参阅

适用于

Add(GrammarBuilder, String)

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

创建包含后跟一个短语的 GrammarBuilder 对象的新 GrammarBuilder

public:
 static System::Speech::Recognition::GrammarBuilder ^ Add(System::Speech::Recognition::GrammarBuilder ^ builder, System::String ^ phrase);
public static System.Speech.Recognition.GrammarBuilder Add (System.Speech.Recognition.GrammarBuilder builder, string phrase);
static member Add : System.Speech.Recognition.GrammarBuilder * string -> System.Speech.Recognition.GrammarBuilder
Public Shared Function Add (builder As GrammarBuilder, phrase As String) As GrammarBuilder

参数

builder
GrammarBuilder

第一个语法元素。

phrase
String

第二个表示字序列的语法元素。

返回

一个 GrammarBuilder,顺序为 builder 元素后面跟随一个 phrase 元素。

示例

以下示例创建一个语音识别语法,该语法可以识别两个短语:“制作背景 ”和“将背景设置为 颜色”,其中 颜色 是从一组颜色中选择的。 各种类型用于生成最终语法,例如 StringChoicesGrammarBuilder 对象。 对 方法的调用 Add 中的显式强制转换运算符是可选的。

private Grammar CreateColorGrammar()
{

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

  // Create grammar builders for the two versions of the phrase.
  GrammarBuilder makePhrase =
    GrammarBuilder.Add((GrammarBuilder)"Make background", colorChoice);
  GrammarBuilder setPhrase =
    GrammarBuilder.Add("Set background to", (GrammarBuilder)colorChoice);

  // 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});
  GrammarBuilder bothPhrases = new GrammarBuilder(bothChoices);

  Grammar grammar = new Grammar(bothPhrases);
  grammar.Name = "backgroundColor";
  return grammar;
}

注解

GrammarBuilder 支持来自以下类的隐式转换:

此方法接受上面 builder 列出的 参数对象。

有关详细信息, Implicit 请参阅 和 Addition 运算符。

另请参阅

适用于

Add(String, GrammarBuilder)

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

创建包含 GrammarBuilder 对象后跟的一个短语的新 GrammarBuilder

public:
 static System::Speech::Recognition::GrammarBuilder ^ Add(System::String ^ phrase, System::Speech::Recognition::GrammarBuilder ^ builder);
public static System.Speech.Recognition.GrammarBuilder Add (string phrase, System.Speech.Recognition.GrammarBuilder builder);
static member Add : string * System.Speech.Recognition.GrammarBuilder -> System.Speech.Recognition.GrammarBuilder
Public Shared Function Add (phrase As String, builder As GrammarBuilder) As GrammarBuilder

参数

phrase
String

第一个表示字序列的语法元素。

builder
GrammarBuilder

第二个语法元素。

返回

一个 GrammarBuilder,顺序为 phrase 元素后面跟随一个 builder 元素。

示例

以下示例创建一个语音识别语法,该语法可以识别两个短语:“制作背景 ”和“将背景设置为 颜色”,其中 颜色 是从一组颜色中选择的。 各种类型用于生成最终语法,例如 StringChoicesGrammarBuilder 对象。 对 方法的调用 Add 中的显式强制转换运算符是可选的。

private Grammar CreateColorGrammar()
{

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

  // Create grammar builders for the two versions of the phrase.
  GrammarBuilder makePhrase =
    GrammarBuilder.Add((GrammarBuilder)"Make background", colorChoice);
  GrammarBuilder setPhrase =
    GrammarBuilder.Add("Set background to", (GrammarBuilder)colorChoice);

  // 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});
  GrammarBuilder bothPhrases = new GrammarBuilder(bothChoices);

  Grammar grammar = new Grammar(bothPhrases);
  grammar.Name = "backgroundColor";
  return grammar;
}

注解

GrammarBuilder 支持来自以下类的隐式转换:

此方法接受上面 builder 列出的 参数对象。

有关详细信息, Implicit 请参阅 和 Addition 运算符。

另请参阅

适用于