GrammarBuilder.Append 方法

定義

將文法項目附加至目前的文法項目序列。

多載

Append(String, Int32, Int32)

將重複的片語附加至目前的文法項目序列。

Append(GrammarBuilder, Int32, Int32)

將重複的文法項目附加至目前的文法項目序列。

Append(String)

將片語附加至目前的文法項目序列。

Append(String, SubsetMatchingMode)

將片語子集的項目附加至目前的文法項目序列。

Append(SemanticResultKey)

將語意索引鍵附加至目前的文法項目序列。

Append(SemanticResultValue)

將語意值附加至目前的文法項目序列。

Append(GrammarBuilder)

將文法項目附加至目前的文法項目序列。

Append(Choices)

將一組替代項目附加至目前的文法項目序列。

備註

使用這些方法將文法專案附加至現有的 GrammarBuilder 。 當您建立文法元素時,您可以將這些專案附加至現有的產生器,以漸進方式開發語音辨識文法的條件約束。 每個元素都會新增至目前元素序列的結尾。

這個方法具有附加 GrammarBuilderStringChoices 、、 SemanticResultKeySemanticResultValue 物件的多載。

重要

使用包含相同索引鍵名稱的重複語意元素的語音辨識文法,或多個可重複修改相同語意元素值的語意元素時,語音辨識器可能會擲回例外狀況。

如需建置和使用語音辨識文法的詳細資訊,請參閱 語音辨識

Append(String, Int32, Int32)

來源:
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 cell phone」,其中 「phone」 一詞是選擇性的。 GrammarBuilderChoices 物件可用來建構文法。 此範例會醒目提示 方法的使用 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

將重複的文法項目附加至目前的文法項目序列。

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 cell phone」,其中 「phone」 一詞是選擇性的。 GrammarBuilderChoices 物件可用來建構文法。 此範例會醒目提示 方法的使用 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

重要

當您將包含 SemanticResultValue 或 實例的物件附加 GrammarBuilderGrammarBuilder 物件時,請務必避免建立具有相同索引鍵名稱的重複語意專案,或多個可重複修改 Value 物件屬性的 SemanticValue 語意 SemanticResultKey 專案。 如果遇到這些情況,語音辨識器可能會擲回例外狀況。

另請參閱

適用於

Append(String)

來源:
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

將片語子集的項目附加至目前的文法項目序列。

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 four five」,但無法辨識片語 「one three 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

將語意索引鍵附加至目前的文法項目序列。

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 會加入至目前專案序列的結尾。

重要

當您將 或 實例附加 SemanticResultValueSemanticResultKeyGrammarBuilder 物件時,請務必避免建立具有相同索引鍵名稱或多個可重複修改 Value 物件屬性的 SemanticValue 重複語意專案。 如果遇到這些情況,語音辨識器可能會擲回例外狀況。

另請參閱

適用於

Append(SemanticResultValue)

來源:
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 會加入至目前專案序列的結尾。

重要

當您將 或 實例附加 SemanticResultValueSemanticResultKeyGrammarBuilder 物件時,請務必避免建立具有相同索引鍵名稱或多個可重複修改 Value 物件屬性的 SemanticValue 重複語意專案。 如果遇到這些情況,語音辨識器可能會擲回例外狀況。

另請參閱

適用於

Append(GrammarBuilder)

來源:
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 cell phone」,其中 「phone」 一詞是選擇性的。 GrammarBuilderChoices 物件可用來建構文法。 此範例會醒目提示 方法的使用 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 會新增至目前文法元素序列的結尾。

注意

當您將包含 SemanticResultValue 或 實例的物件附加 GrammarBuilderGrammarBuilder 物件時,請務必避免建立具有相同索引鍵名稱的重複語意專案,或多個可重複修改 Value 物件屬性的 SemanticValue 語意 SemanticResultKey 專案。 如果遇到這些情況,語音辨識器可能會擲回例外狀況。

另請參閱

適用於

Append(Choices)

來源:
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 cell 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 會加入至目前專案序列的結尾。

重要

當您將包含 SemanticResultValue 或 實例的物件附加 ChoicesGrammarBuilder 物件時,請務必避免建立具有相同索引鍵名稱的重複語意專案,或多個可重複修改 Value 物件屬性的 SemanticValue 語意 SemanticResultKey 專案。 如果遇到這些情況,語音辨識器可能會擲回例外狀況。

適用於