Прочетете на английски Редактиране

Споделяне чрез


GrammarBuilder.Add Method

Definition

Creates a new GrammarBuilder that contains a sequence of two grammar elements.

Overloads

Add(Choices, GrammarBuilder)

Creates a new GrammarBuilder that contains a Choices object followed by a GrammarBuilder object.

Add(GrammarBuilder, Choices)

Creates a new GrammarBuilder that contains a GrammarBuilder object followed by a Choices object.

Add(GrammarBuilder, GrammarBuilder)

Creates a new GrammarBuilder that contains a sequence of two GrammarBuilder objects.

Add(GrammarBuilder, String)

Creates a new GrammarBuilder that contains a GrammarBuilder object followed by a phrase.

Add(String, GrammarBuilder)

Creates a new GrammarBuilder that contains a phrase followed by a GrammarBuilder object.

Remarks

The static Add methods provide another mechanism by which you can combine various types to create diversity and flexibility in grammars built with GrammarBuilder. These methods correspond to the static Addition methods, which are also defined on the GrammarBuilder class. The order of the parameters determines the order of the elements in the new GrammarBuilder.

A GrammarBuilder can also be obtained from Choices, SemanticResultKey, SemanticResultValue, and String objects. For more information, see the Implicit and Addition operators.

Важно

The speech recognizer can throw an exception when using a speech recognition grammar that contains duplicate semantic elements with the same key name or multiple semantic elements that could repeatedly modify the value of the same semantic element.

For more information about building and using speech recognition grammars, see Speech Recognition.

Add(Choices, GrammarBuilder)

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

Creates a new GrammarBuilder that contains a Choices object followed by a GrammarBuilder object.

C#
public static System.Speech.Recognition.GrammarBuilder Add(System.Speech.Recognition.Choices choices, System.Speech.Recognition.GrammarBuilder builder);

Parameters

choices
Choices

The first grammar element, which represents a set of alternatives.

builder
GrammarBuilder

The second grammar element.

Returns

A GrammarBuilder for the sequence of the choices element followed by the builder element.

Remarks

GrammarBuilder supports implicit conversions from the following classes:

This method accepts the objects listed above for the builder parameter.

For more information, see the Implicit and Addition operators.

Важно

When you combine Choices and GrammarBuilder objects that contain SemanticResultValue or SemanticResultKey instances, make sure you avoid creating duplicate semantic elements with the same key name or multiple semantic elements that could repeatedly modify the Value property of a SemanticValue object. The speech recognizer can throw an exception if it encounters these circumstances. For more information about building a speech recognition grammar that contains semantic information, see Add Semantics to a GrammarBuilder Grammar.

See also

Applies to

.NET 10 (package-provided) и други версии
Продукт Версии
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

Add(GrammarBuilder, Choices)

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

Creates a new GrammarBuilder that contains a GrammarBuilder object followed by a Choices object.

C#
public static System.Speech.Recognition.GrammarBuilder Add(System.Speech.Recognition.GrammarBuilder builder, System.Speech.Recognition.Choices choices);

Parameters

builder
GrammarBuilder

The first grammar element.

choices
Choices

The second grammar element, which represents a set of alternatives.

Returns

A GrammarBuilder for the sequence of the builder element followed by the choices element.

Examples

The following example creates a speech recognition grammar that can recognize the two phrases, "Make background color" and "Set background to color", where color is selected from a set of colors. Various types are used to build the final grammar, such as String, Choices, and GrammarBuilder objects. The explicit cast operators in the calls to the Add methods are optional.

C#
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;
}

Remarks

GrammarBuilder supports implicit conversions from the following classes:

This method accepts the objects listed above for the builder parameter.

For more information, see the Implicit and Addition operators.

Важно

When you combine Choices and GrammarBuilder objects that contain SemanticResultValue or SemanticResultKey instances with other grammar elements, make sure you avoid creating duplicate semantic elements with the same key name or multiple semantic elements that could repeatedly modify the Value property of a SemanticValue object. The speech recognizer can throw an exception if it encounters these circumstances.

See also

Applies to

.NET 10 (package-provided) и други версии
Продукт Версии
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

Add(GrammarBuilder, GrammarBuilder)

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

Creates a new GrammarBuilder that contains a sequence of two GrammarBuilder objects.

C#
public static System.Speech.Recognition.GrammarBuilder Add(System.Speech.Recognition.GrammarBuilder builder1, System.Speech.Recognition.GrammarBuilder builder2);

Parameters

builder1
GrammarBuilder

The first grammar element.

builder2
GrammarBuilder

The second grammar element.

Returns

A GrammarBuilder for the sequence of the builder1 element followed by the builder2 element.

Examples

The following example creates a speech recognition grammar that can recognize the two phrases, "Make background color" and "Set background to color", where color is selected from a set of colors. Various types are used to build the final grammar, such as String, Choices, and GrammarBuilder objects. The explicit cast operators in the calls to the Add methods are optional.

C#
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;
}

Remarks

GrammarBuilder supports implicit conversions from the following classes:

This method accepts the objects listed above for the builder1 or builder2 parameter.

For more information, see the Implicit and Addition operators.

Важно

When you combine Choices and GrammarBuilder objects that contain SemanticResultValue or SemanticResultKey instances with other grammar elements, make sure you avoid creating duplicate semantic elements with the same key name or multiple semantic elements that could repeatedly modify the Value property of a SemanticValue object. The speech recognizer can throw an exception if it encounters these circumstances.

See also

Applies to

.NET 10 (package-provided) и други версии
Продукт Версии
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

Add(GrammarBuilder, String)

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

Creates a new GrammarBuilder that contains a GrammarBuilder object followed by a phrase.

C#
public static System.Speech.Recognition.GrammarBuilder Add(System.Speech.Recognition.GrammarBuilder builder, string phrase);

Parameters

builder
GrammarBuilder

The first grammar element.

phrase
String

The second grammar element, which represents a sequence of words.

Returns

A GrammarBuilder for the sequence of the builder element followed by the phrase element.

Examples

The following example creates a speech recognition grammar that can recognize the two phrases, "Make background color" and "Set background to color", where color is selected from a set of colors. Various types are used to build the final grammar, such as String, Choices, and GrammarBuilder objects. The explicit cast operators in the calls to the Add methods are optional.

C#
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;
}

Remarks

GrammarBuilder supports implicit conversions from the following classes:

This method accepts the objects listed above for the builder parameter.

For more information, see the Implicit and Addition operators.

See also

Applies to

.NET 10 (package-provided) и други версии
Продукт Версии
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

Add(String, GrammarBuilder)

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

Creates a new GrammarBuilder that contains a phrase followed by a GrammarBuilder object.

C#
public static System.Speech.Recognition.GrammarBuilder Add(string phrase, System.Speech.Recognition.GrammarBuilder builder);

Parameters

phrase
String

The first grammar element, which represents a sequence of words.

builder
GrammarBuilder

The second grammar element.

Returns

A GrammarBuilder for the sequence of the phrase element followed by the builder element.

Examples

The following example creates a speech recognition grammar that can recognize the two phrases, "Make background color" and "Set background to color", where color is selected from a set of colors. Various types are used to build the final grammar, such as String, Choices, and GrammarBuilder objects. The explicit cast operators in the calls to the Add methods are optional.

C#
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;
}

Remarks

GrammarBuilder supports implicit conversions from the following classes:

This method accepts the objects listed above for the builder parameter.

For more information, see the Implicit and Addition operators.

See also

Applies to

.NET 10 (package-provided) и други версии
Продукт Версии
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)