共用方式為


GrammarBuilder.Append 方法

定義

在目前的文法元素序列中附加一個文法元素。

多載

名稱 Description
Append(String, Int32, Int32)

在當前文法元素序列中附加重複的片語。

Append(GrammarBuilder, Int32, Int32)

在目前的文法元素序列中附加重複的文法元素。

Append(String)

在目前的文法元素序列中附加一個片語。

Append(String, SubsetMatchingMode)

將片語子集的元素附加到目前的文法元素序列中。

Append(SemanticResultKey)

在目前的文法元素序列中附加語意鍵。

Append(SemanticResultValue)

在目前的文法元素序列中附加語意值。

Append(GrammarBuilder)

在目前的文法元素序列中附加一個文法元素。

Append(Choices)

附加一組替代方案於目前的文法元素序列。

備註

利用這些方法將文法元素附加到現有 GrammarBuilder的 。 當你建立文法元素時,可以將它們附加到現有建構器中,逐步發展語音辨識文法的限制。 每個元素都被加入到目前元素序列的末尾。

此方法有用於附加 GrammarBuilderStringChoicesSemanticResultKey、 及 SemanticResultValue 物件的過載。

這很重要

語音辨識器在使用包含重複語意元素且鍵名相同,或多個語意元素可能反覆修改相同語意元素值的語音辨識文法時拋出例外。

欲了解更多關於建構與使用語音辨識文法的資訊,請參閱語音辨識。

Append(String, Int32, Int32)

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

在當前文法元素序列中附加重複的片語。

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)

參數

phrase
String

重複出現的字串。

minRepeat
Int32

該輸入匹配 phrase 必須發生的最少次數,才能構成匹配。

maxRepeat
Int32

該輸入匹配 phrase 最多可發生的次數,以構成匹配。

範例

以下範例為「Call James at work」和「Call Anne on her mobile phone」這類片語建立語音辨識文法,其中「phone」一詞為可選。 GrammarBuilder Choices而物件則用來構建文法。 這個例子突顯了該 Append 方法的使用。

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

備註

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

另請參閱

適用於

Append(GrammarBuilder, Int32, Int32)

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

在目前的文法元素序列中附加重複的文法元素。

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)

參數

builder
GrammarBuilder

重複的文法元素要加上。

minRepeat
Int32

該輸入與 定義 builder 的元素匹配的最少次數必須發生,才能構成匹配。

maxRepeat
Int32

該輸入與 定義 builder 的元素匹配的最大次數可構成匹配。

範例

以下範例為「Call James at work」和「Call Anne on her mobile phone」這類片語建立語音辨識文法,其中「phone」一詞為可選。 GrammarBuilder Choices而物件則用來構建文法。 這個例子突顯了該 Append 方法的使用。

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

備註

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

這很重要

當你在物件上附加GrammarBuilder包含或SemanticResultValueSemanticResultKey實例的物件時GrammarBuilder,務必避免建立具有相同鍵名的重複語意元素,或避免多次修改物件屬性SemanticValue的語意元素Value。 語音識別器在遇到這些情況時可以拋出例外。

另請參閱

適用於

Append(String)

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

在目前的文法元素序列中附加一個片語。

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

參數

phrase
String

要加上的字序。

備註

phrase 是加到當前元素序列的末尾。

另請參閱

適用於

Append(String, SubsetMatchingMode)

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

將片語子集的元素附加到目前的文法元素序列中。

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)

參數

phrase
String

要加上的字序。

subsetMatchingCriteria
SubsetMatchingMode

文法用來辨識片語的匹配模式。

範例

以下範例為每個 SubsetMatchingMode 值建立語音辨識文法。 例如,生成的文法 OrderedSubset 識別短語「three four five」和「one three five」,而文法 Subsequence 識別短語「three four five」,但不認識「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();
}

備註

子集元素會被加入到目前元素序列的末尾。 欲了解更多關於如何使用字串建立語音辨識文法的資訊,請參閱 「使用字串建立 GrammarBuilder 文法」。

關於子集匹配模式的詳細使用資訊,請參見 System.Speech.Recognition.SubsetMatchingMode

另請參閱

適用於

Append(SemanticResultKey)

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

在目前的文法元素序列中附加語意鍵。

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)

參數

key
SemanticResultKey

語意鍵要附加。

範例

以下範例是用於選擇航班起訖城市的主控台應用程式的一部分。 應用程式會辨識像「我想從邁阿密飛往芝加哥」這樣的短語。事件處理 SpeechRecognized 程序使用 以 SemanticResultKey 擷取起訖城市中指定的 SemanticResultValue 機場代碼。

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);
    }
  }
}

備註

key 是加到當前元素序列的末尾。

這很重要

當你對物件附加或實例時,務必避免建立具有相同鍵名的重複語意元素,或避免多次修改物件屬性SemanticValue的語意元素ValueSemanticResultKeySemanticResultValueGrammarBuilder 語音識別器在遇到這些情況時可以拋出例外。

另請參閱

適用於

Append(SemanticResultValue)

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

在目前的文法元素序列中附加語意值。

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)

參數

value
SemanticResultValue

要附加的語意值。

範例

以下範例是用於選擇航班起訖城市的主控台應用程式的一部分。 應用程式會辨識像「我想從邁阿密飛往芝加哥」這樣的短語。事件處理 SpeechRecognized 程序使用 以 SemanticResultKey 擷取起訖城市中指定的 SemanticResultValue 機場代碼。

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);
    }
  }
}

備註

value 是加到當前元素序列的末尾。

這很重要

當你對物件附加或實例時,務必避免建立具有相同鍵名的重複語意元素,或避免多次修改物件屬性SemanticValue的語意元素ValueSemanticResultKeySemanticResultValueGrammarBuilder 語音識別器在遇到這些情況時可以拋出例外。

另請參閱

適用於

Append(GrammarBuilder)

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

在目前的文法元素序列中附加一個文法元素。

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)

參數

builder
GrammarBuilder

文法元素要加上。

範例

以下範例為「Call James at work」和「Call Anne on her mobile phone」這類片語建立語音辨識文法,其中「phone」一詞為可選。 GrammarBuilder Choices而物件則用來構建文法。 這個例子突顯了該 Append 方法的使用。

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

備註

builder 被加在目前文法元素序列的末尾。

備註

當你在物件上附加GrammarBuilder包含或SemanticResultValueSemanticResultKey實例的物件時GrammarBuilder,務必避免建立具有相同鍵名的重複語意元素,或避免多次修改物件屬性SemanticValue的語意元素Value。 語音識別器在遇到這些情況時可以拋出例外。

另請參閱

適用於

Append(Choices)

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

附加一組替代方案於目前的文法元素序列。

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)

參數

alternateChoices
Choices

補充的替代方案集合。

範例

以下範例為「Call James at work」和「Call Anne on her mobile phone」這類片語建立語音辨識文法,其中「phone」一詞為可選。 這個例子突顯了該 Append 方法的使用。

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

備註

alternateChoices 是加到當前元素序列的末尾。

這很重要

當你在物件上附加Choices包含或SemanticResultValueSemanticResultKey實例的物件時GrammarBuilder,務必避免建立具有相同鍵名的重複語意元素,或避免多次修改物件屬性SemanticValue的語意元素Value。 語音識別器在遇到這些情況時可以拋出例外。

適用於