Κοινή χρήση μέσω


GrammarBuilder.Append Method

Definition

Appends a grammar element to the current sequence of grammar elements.

Overloads

Append(String, Int32, Int32)

Appends a repeated phrase to the current sequence of grammar elements.

Append(GrammarBuilder, Int32, Int32)

Appends a repeated grammar element to the current sequence of grammar elements.

Append(String)

Appends a phrase to the current sequence of grammar elements.

Append(String, SubsetMatchingMode)

Appends an element for a subset of a phrase to the current sequence of grammar elements.

Append(SemanticResultKey)

Appends a semantic key to the current sequence of grammar elements.

Append(SemanticResultValue)

Appends a semantic value to the current sequence of grammar elements.

Append(GrammarBuilder)

Appends a grammar element to the current sequence of grammar elements.

Append(Choices)

Appends a set of alternatives to the current sequence of grammar elements.

Remarks

Use these methods to append grammar elements to an existing GrammarBuilder. As you create grammar elements, you can append them to the existing builder to progressively develop the constraints for a speech recognition grammar. Each element is added to the end of the current sequence of elements.

This method has overloads for appending GrammarBuilder, String, Choices, SemanticResultKey, and SemanticResultValue objects.

Important

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.

Append(String, Int32, Int32)

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

Appends a repeated phrase to the current sequence of grammar elements.

public:
 void Append(System::String ^ phrase, int minRepeat, int maxRepeat);
public void Append (string phrase, int minRepeat, int maxRepeat);
member this.Append : string * int * int -> unit
Public Sub Append (phrase As String, minRepeat As Integer, maxRepeat As Integer)

Parameters

phrase
String

The repeated sequence of words to append.

minRepeat
Int32

The minimum number of times that input matching phrase must occur to constitute a match.

maxRepeat
Int32

The maximum number of times that input matching phrase can occur to constitute a match.

Examples

The following example creates a speech recognition grammar for phrases such as "Call James at work" and "Call Anne on her cell phone", where the word "phone" is optional. GrammarBuilder and Choices objects are used to construct the grammar. The example highlights the use of the Append method.

public static Grammar CreatePhonePhrase()
{
  // Create alternatives for person names, locations, devices, and pronouns.
  Choices personChoice = new Choices(new string[] {"Anne", "James", "Mary", "Sam"});
  Choices locationChoice = new Choices(new string[] {"home", "work"});
  Choices deviceChoice = new Choices(new string[] {"home", "work", "cell"});
  Choices pronounChoice = new Choices(new string[] {"his", "her"});

  // Create a phrase for the receiving device, which optionally contains the word "phone".
  GrammarBuilder devicePhrase = new GrammarBuilder(pronounChoice);
  devicePhrase.Append(deviceChoice);
  devicePhrase.Append("phone", 0, 1);

  // Create alternatives for phrases specifying a device or a location.
  GrammarBuilder atLocation = new GrammarBuilder("at");
  atLocation.Append(locationChoice);

  GrammarBuilder onDevice = new GrammarBuilder("on");
  onDevice.Append(devicePhrase);

  Choices howChoice = new Choices(new GrammarBuilder[] {atLocation, onDevice});

  // Build the final phrase.
  GrammarBuilder callWho = new GrammarBuilder("Call");
  callWho.Append(personChoice);
  callWho.Append(howChoice);

  // Create the Grammar object.
  Grammar callGrammar = new Grammar(callWho);
  callGrammar.Name = "Call Grammar";

  return callGrammar;
}

Remarks

The value of minRepeat must be greater than or equal to 0 and less than or equal to the value of maxRepeat.

See also

Applies to

Append(GrammarBuilder, Int32, Int32)

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

Appends a repeated grammar element to the current sequence of grammar elements.

public:
 void Append(System::Speech::Recognition::GrammarBuilder ^ builder, int minRepeat, int maxRepeat);
public void Append (System.Speech.Recognition.GrammarBuilder builder, int minRepeat, int maxRepeat);
member this.Append : System.Speech.Recognition.GrammarBuilder * int * int -> unit
Public Sub Append (builder As GrammarBuilder, minRepeat As Integer, maxRepeat As Integer)

Parameters

builder
GrammarBuilder

The repeated grammar element to append.

minRepeat
Int32

The minimum number of times that input matching the element defined by builder must occur to constitute a match.

maxRepeat
Int32

The maximum number of times that input matching the element defined by builder can occur to constitute a match.

Examples

The following example creates a speech recognition grammar for phrases such as "Call James at work" and "Call Anne on her cell phone", where the word "phone" is optional. GrammarBuilder and Choices objects are used to construct the grammar. The example highlights the use of the Append method.

public static Grammar CreatePhonePhrase()
{
  // Create alternatives for person names, locations, devices, and pronouns.
  Choices personChoice = new Choices(new string[] {"Anne", "James", "Mary", "Sam"});
  Choices locationChoice = new Choices(new string[] {"home", "work"});
  Choices deviceChoice = new Choices(new string[] {"home", "work", "cell"});
  Choices pronounChoice = new Choices(new string[] {"his", "her"});

  // Create a phrase for the receiving device, which optionally contains the word "phone".
  GrammarBuilder devicePhrase = new GrammarBuilder(pronounChoice);
  devicePhrase.Append(deviceChoice);
  devicePhrase.Append("phone", 0, 1);

  // Create alternatives for phrases specifying a device or a location.
  GrammarBuilder atLocation = new GrammarBuilder("at");
  atLocation.Append(locationChoice);

  GrammarBuilder onDevice = new GrammarBuilder("on");
  onDevice.Append(devicePhrase);

  Choices howChoice = new Choices(new GrammarBuilder[] {atLocation, onDevice});

  // Build the final phrase.
  GrammarBuilder callWho = new GrammarBuilder("Call");
  callWho.Append(personChoice);
  callWho.Append(howChoice);

  // Create the Grammar object.
  Grammar callGrammar = new Grammar(callWho);
  callGrammar.Name = "Call Grammar";

  return callGrammar;
}

Remarks

The value of minRepeat must be greater than or equal to 0 and less than or equal to the value of maxRepeat.

Important

When you append GrammarBuilder objects that contain SemanticResultValue or SemanticResultKey instances to a GrammarBuilder object, 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

Append(String)

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

Appends a phrase to the current sequence of grammar elements.

public:
 void Append(System::String ^ phrase);
public void Append (string phrase);
member this.Append : string -> unit
Public Sub Append (phrase As String)

Parameters

phrase
String

The sequence of words to append.

Remarks

phrase is added to the end of the current sequence of elements.

See also

Applies to

Append(String, SubsetMatchingMode)

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

Appends an element for a subset of a phrase to the current sequence of grammar elements.

public:
 void Append(System::String ^ phrase, System::Speech::Recognition::SubsetMatchingMode subsetMatchingCriteria);
public void Append (string phrase, System.Speech.Recognition.SubsetMatchingMode subsetMatchingCriteria);
member this.Append : string * System.Speech.Recognition.SubsetMatchingMode -> unit
Public Sub Append (phrase As String, subsetMatchingCriteria As SubsetMatchingMode)

Parameters

phrase
String

The sequence of words to append.

subsetMatchingCriteria
SubsetMatchingMode

The matching mode the grammar uses to recognize the phrase.

Examples

The following example creates a speech recognition grammar for each SubsetMatchingMode value. For example, the generated grammar OrderedSubset recognizes the phrases, "three four five" and "one three five", and the grammar Subsequence recognizes the phrase "three four five", but not the phrase, "one three five".

private Grammar[] CreateSubsetMatchTest()
{
  List<Grammar> grammars = new List<Grammar>(4);

  string phrase = "one two three four five six";
  foreach (SubsetMatchingMode mode in
    Enum.GetValues(typeof(SubsetMatchingMode)))
  {
    GrammarBuilder gb = new GrammarBuilder();
    gb.Append(phrase, mode);

    Grammar grammar = new Grammar(gb);
    grammar.Name = mode.ToString();
    grammars.Add(grammar);
  }

  return grammars.ToArray();
}

Remarks

The subset element is added to the end of the current sequence of elements. For more information about building a speech recognition grammar using strings, see Using Strings to Create a GrammarBuilder Grammar.

For detailed information on the use of subset matching modes, see System.Speech.Recognition.SubsetMatchingMode.

See also

Applies to

Append(SemanticResultKey)

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

Appends a semantic key to the current sequence of grammar elements.

public:
 void Append(System::Speech::Recognition::SemanticResultKey ^ key);
public void Append (System.Speech.Recognition.SemanticResultKey key);
member this.Append : System.Speech.Recognition.SemanticResultKey -> unit
Public Sub Append (key As SemanticResultKey)

Parameters

key
SemanticResultKey

The semantic key to append.

Examples

The following example is part of a console application for choosing origin and destination cities for a flight. The application recognizes phrases such as "I want to fly from Miami to Chicago." The handler for the SpeechRecognized event uses the SemanticResultKey to extract the airport code specified in the SemanticResultValue for the origin and destination cities.

using System;
using System.Speech.Recognition;

namespace SampleRecognition
{
  class Program
  {
    static void Main(string[] args)

    // Initialize an in-process speech recognition engine.
    {
      using (SpeechRecognitionEngine recognizer =
         new SpeechRecognitionEngine())
      {

        // Create a Choices object and add  cities and airport codes
        // using SemanticResultValue objects.
        Choices cities = new Choices();
        cities.Add(new SemanticResultValue("Chicago", "ORD"));
        cities.Add(new SemanticResultValue("Boston", "BOS"));
        cities.Add(new SemanticResultValue("Miami", "MIA"));
        cities.Add(new SemanticResultValue("Dallas", "DFW"));

        // Build the phrase and add SemanticResultKeys.
        GrammarBuilder chooseCities = new GrammarBuilder();
        chooseCities.Append("I want to fly from");
        chooseCities.Append(new SemanticResultKey("origin", cities));
        chooseCities.Append("to");
        chooseCities.Append(new SemanticResultKey("destination", cities));

        // Build a Grammar object from the GrammarBuilder.
        Grammar bookFlight = new Grammar(chooseCities);
        bookFlight.Name = "Book Flight";

        // Add a handler for the LoadGrammarCompleted event.
        recognizer.LoadGrammarCompleted +=
          new EventHandler<LoadGrammarCompletedEventArgs>(recognizer_LoadGrammarCompleted);

        // Add a handler for the SpeechRecognized event.
        recognizer.SpeechRecognized +=
          new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);

        // Configure the input to the recognizer.
        recognizer.SetInputToDefaultAudioDevice();

        // Load the grammar object and start recognition.
        recognizer.LoadGrammarAsync(bookFlight);
        recognizer.RecognizeAsync();

        // Keep the console window open.
        Console.ReadLine();
      }
    }

    // Handle the LoadGrammarCompleted event.
    static void recognizer_LoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs e)
    {
      Console.WriteLine("Grammar loaded: " + e.Grammar.Name);
      Console.WriteLine();
    }

    // Handle the SpeechRecognized event.
    static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
      Console.WriteLine("Speech recognized:  " + e.Result.Text);
      Console.WriteLine();
      Console.WriteLine("Semantic results:");
      Console.WriteLine("  The flight origin is " + e.Result.Semantics["origin"].Value);
      Console.WriteLine("  The flight destination is " + e.Result.Semantics["destination"].Value);
    }
  }
}

Remarks

key is added to the end of the current sequence of elements.

Important

When you append SemanticResultValue or SemanticResultKey instances to a GrammarBuilder object, 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

Append(SemanticResultValue)

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

Appends a semantic value to the current sequence of grammar elements.

public:
 void Append(System::Speech::Recognition::SemanticResultValue ^ value);
public void Append (System.Speech.Recognition.SemanticResultValue value);
member this.Append : System.Speech.Recognition.SemanticResultValue -> unit
Public Sub Append (value As SemanticResultValue)

Parameters

value
SemanticResultValue

The semantic value to append.

Examples

The following example is part of a console application for choosing origin and destination cities for a flight. The application recognizes phrases such as "I want to fly from Miami to Chicago." The handler for the SpeechRecognized event uses the SemanticResultKey to extract the airport code specified in the SemanticResultValue for the origin and destination cities.

using System;
using System.Speech.Recognition;

namespace SampleRecognition
{
  class Program
  {
    static void Main(string[] args)

    // Initialize an in-process speech recognition engine.
    {
      using (SpeechRecognitionEngine recognizer =
         new SpeechRecognitionEngine())
      {

        // Create GrammarBuilder objects and append SemanticResultValue objects
        // that contain cities and airport codes.

        GrammarBuilder chicago = new GrammarBuilder();
        chicago.Append(new SemanticResultValue("Chicago", "ORD"));

        GrammarBuilder boston = new GrammarBuilder();
        boston.Append(new SemanticResultValue("Boston", "BOS"));

        GrammarBuilder miami = new GrammarBuilder();
        miami.Append(new SemanticResultValue("Miami", "MIA"));

        GrammarBuilder dallas = new GrammarBuilder();
        dallas.Append(new SemanticResultValue("Dallas", "DFW"));

        // Create a Choices object and add the cities using implicit conversion from
        // SemanticResultValue to GrammarBuilder.
        Choices cities = new Choices();
        cities.Add(new Choices(new GrammarBuilder[] { chicago, boston, miami, dallas }));

        // Build the phrase and add SemanticResultKeys.
        GrammarBuilder chooseCities = new GrammarBuilder();
        chooseCities.Append("I want to fly from");
        chooseCities.Append(new SemanticResultKey("origin", cities));
        chooseCities.Append("to");
        chooseCities.Append(new SemanticResultKey("destination", cities));

        // Build a Grammar object from the GrammarBuilder.
        Grammar bookFlight = new Grammar(chooseCities);
        bookFlight.Name = "Book Flight";

        // Add a handler for the LoadGrammarCompleted event.
        recognizer.LoadGrammarCompleted +=
          new EventHandler<LoadGrammarCompletedEventArgs>(recognizer_LoadGrammarCompleted);

        // Add a handler for the SpeechRecognized event.
        recognizer.SpeechRecognized +=
          new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);

        // Configure the input to the recognizer.
        recognizer.SetInputToDefaultAudioDevice();

        // Load the grammar object and start recognition.
        recognizer.LoadGrammarAsync(bookFlight);
        recognizer.RecognizeAsync();

        // Keep the console window open.
        Console.ReadLine();
      }
    }
    // Handle the LoadGrammarCompleted event.
    static void recognizer_LoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs e)
    {
      Console.WriteLine("Grammar loaded: " + e.Grammar.Name);
      Console.WriteLine();
    }

    // Handle the SpeechRecognized event.
    static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
      Console.WriteLine("Speech recognized:  " + e.Result.Text);
      Console.WriteLine();
      Console.WriteLine("Semantic results:");
      Console.WriteLine("  The flight origin is " + e.Result.Semantics["origin"].Value);
      Console.WriteLine("  The flight destination is " + e.Result.Semantics["destination"].Value);
    }
  }
}

Remarks

value is added to the end of the current sequence of elements.

Important

When you append SemanticResultValue or SemanticResultKey instances to a GrammarBuilder object, 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

Append(GrammarBuilder)

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

Appends a grammar element to the current sequence of grammar elements.

public:
 void Append(System::Speech::Recognition::GrammarBuilder ^ builder);
public void Append (System.Speech.Recognition.GrammarBuilder builder);
member this.Append : System.Speech.Recognition.GrammarBuilder -> unit
Public Sub Append (builder As GrammarBuilder)

Parameters

builder
GrammarBuilder

The grammar element to append.

Examples

The following example creates a speech recognition grammar for phrases such as "Call James at work" and "Call Anne on her cell phone", where the word "phone" is optional. GrammarBuilder and Choices objects are used to construct the grammar. The example highlights the use of the Append method.

public static Grammar CreatePhonePhrase()
{
  // Create alternatives for person names, locations, devices, and pronouns.
  Choices personChoice = new Choices(new string[] {"Anne", "James", "Mary", "Sam"});
  Choices locationChoice = new Choices(new string[] {"home", "work"});
  Choices deviceChoice = new Choices(new string[] {"home", "work", "cell"});
  Choices pronounChoice = new Choices(new string[] {"his", "her"});

  // Create a phrase for the receiving device, which optionally contains the word "phone".
  GrammarBuilder devicePhrase = new GrammarBuilder(pronounChoice);
  devicePhrase.Append(deviceChoice);
  devicePhrase.Append("phone", 0, 1);

  // Create alternatives for phrases specifying a device or a location.
  GrammarBuilder atLocation = new GrammarBuilder("at");
  atLocation.Append(locationChoice);

  GrammarBuilder onDevice = new GrammarBuilder("on");
  onDevice.Append(devicePhrase);

  Choices howChoice = new Choices(new GrammarBuilder[] {atLocation, onDevice});

  // Build the final phrase.
  GrammarBuilder callWho = new GrammarBuilder("Call");
  callWho.Append(personChoice);
  callWho.Append(howChoice);

  // Create the Grammar object.
  Grammar callGrammar = new Grammar(callWho);
  callGrammar.Name = "Call Grammar";

  return callGrammar;
}

Remarks

builder is added to the end of the current sequence of grammar elements.

Note

When you append GrammarBuilder objects that contain SemanticResultValue or SemanticResultKey instances to a GrammarBuilder object, 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

Append(Choices)

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

Appends a set of alternatives to the current sequence of grammar elements.

public:
 void Append(System::Speech::Recognition::Choices ^ alternateChoices);
public void Append (System.Speech.Recognition.Choices alternateChoices);
member this.Append : System.Speech.Recognition.Choices -> unit
Public Sub Append (alternateChoices As Choices)

Parameters

alternateChoices
Choices

The set of alternatives to append.

Examples

The following example creates a speech recognition grammar for phrases such as "Call James at work" and "Call Anne on her cell phone", where the word "phone" is optional. The example highlights the use of the Append method.

public static Grammar CreatePhonePhrase()
{
  // Create alternatives for person names, locations, devices, and pronouns.
  Choices personChoice = new Choices(new string[] {"Anne", "James", "Mary", "Sam"});
  Choices locationChoice = new Choices(new string[] {"home", "work"});
  Choices deviceChoice = new Choices(new string[] {"home", "work", "cell"});
  Choices pronounChoice = new Choices(new string[] {"his", "her"});

  // Create a phrase for the receiving device, which optionally contains the word "phone".
  GrammarBuilder devicePhrase = new GrammarBuilder(pronounChoice);
  devicePhrase.Append(deviceChoice);
  devicePhrase.Append("phone", 0, 1);

  // Create alternatives for phrases specifying a device or a location.
  GrammarBuilder atLocation = new GrammarBuilder("at");
  atLocation.Append(locationChoice);

  GrammarBuilder onDevice = new GrammarBuilder("on");
  onDevice.Append(devicePhrase);

  Choices howChoice = new Choices(new GrammarBuilder[] {atLocation, onDevice});

  // Build the final phrase.
  GrammarBuilder callWho = new GrammarBuilder("Call");
  callWho.Append(personChoice);
  callWho.Append(howChoice);

  // Create the Grammar object.
  Grammar callGrammar = new Grammar(callWho);
  callGrammar.Name = "Call Grammar";

  return callGrammar;
}

Remarks

alternateChoices is added to the end of the current sequence of elements.

Important

When you append Choices objects that contain SemanticResultValue or SemanticResultKey instances to a GrammarBuilder object, 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.

Applies to