通过


GrammarBuilder.Append 方法

定义

将语法元素追加到当前语法元素序列中。

重载

名称 说明
Append(String, Int32, Int32)

将重复的短语追加到当前语法元素序列中。

Append(GrammarBuilder, Int32, Int32)

将重复的语法元素追加到当前语法元素序列中。

Append(String)

将短语追加到当前语法元素序列中。

Append(String, SubsetMatchingMode)

将短语子集的元素追加到当前语法元素序列中。

Append(SemanticResultKey)

将语义键追加到当前语法元素序列中。

Append(SemanticResultValue)

将语义值追加到语法元素的当前序列中。

Append(GrammarBuilder)

将语法元素追加到当前语法元素序列中。

Append(Choices)

将一组替代项追加到当前语法元素序列。

注解

使用这些方法将语法元素追加到现有 GrammarBuilder语法元素。 创建语法元素时,可以将它们追加到现有生成器,以逐步开发语音识别语法的约束。 每个元素将添加到当前元素序列的末尾。

此方法具有用于追加 GrammarBuilderStringChoicesSemanticResultKeySemanticResultValue 对象的重载。

重要

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

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

Append(String, Int32, Int32)

Source:
GrammarBuilder.cs
Source:
GrammarBuilder.cs
Source:
GrammarBuilder.cs
Source:
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 的最大次数。

示例

以下示例为短语(如“在工作时呼叫詹姆斯”和“在手机中呼叫安妮”)创建语音识别语法,其中“电话”一词是可选的。 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)

Source:
GrammarBuilder.cs
Source:
GrammarBuilder.cs
Source:
GrammarBuilder.cs
Source:
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 元素匹配的最大次数可能是构成匹配项。

示例

以下示例为短语(如“在工作时呼叫詹姆斯”和“在手机中呼叫安妮”)创建语音识别语法,其中“电话”一词是可选的。 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

重要

将包含或实例的对象追加GrammarBuilderGrammarBuilder对象时,请确保避免创建具有相同键名或多个语义元素的重复语义元素,这些元素可以重复修改Value对象的属性SemanticValueSemanticResultKeySemanticResultValue 如果遇到这些情况,语音识别器可能会引发异常。

另请参阅

适用于

Append(String)

Source:
GrammarBuilder.cs
Source:
GrammarBuilder.cs
Source:
GrammarBuilder.cs
Source:
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)

Source:
GrammarBuilder.cs
Source:
GrammarBuilder.cs
Source:
GrammarBuilder.cs
Source:
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 可识别短语“三四五”和“一三五”,语法 Subsequence 可识别短语“三四五”,但不能识别短语“一三五”。

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)

Source:
GrammarBuilder.cs
Source:
GrammarBuilder.cs
Source:
GrammarBuilder.cs
Source:
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 添加到当前元素序列的末尾。

重要

向对象追加 SemanticResultValueSemanticResultKey 实例 GrammarBuilder 时,请确保避免创建具有相同键名或多个语义元素的重复语义元素,这些元素可以重复修改 Value 对象的属性 SemanticValue 。 如果遇到这些情况,语音识别器可能会引发异常。

另请参阅

适用于

Append(SemanticResultValue)

Source:
GrammarBuilder.cs
Source:
GrammarBuilder.cs
Source:
GrammarBuilder.cs
Source:
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 添加到当前元素序列的末尾。

重要

向对象追加 SemanticResultValueSemanticResultKey 实例 GrammarBuilder 时,请确保避免创建具有相同键名或多个语义元素的重复语义元素,这些元素可以重复修改 Value 对象的属性 SemanticValue 。 如果遇到这些情况,语音识别器可能会引发异常。

另请参阅

适用于

Append(GrammarBuilder)

Source:
GrammarBuilder.cs
Source:
GrammarBuilder.cs
Source:
GrammarBuilder.cs
Source:
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

要追加的语法元素。

示例

以下示例为短语(如“在工作时呼叫詹姆斯”和“在手机中呼叫安妮”)创建语音识别语法,其中“电话”一词是可选的。 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 添加到当前语法元素序列的末尾。

注释

将包含或实例的对象追加GrammarBuilderGrammarBuilder对象时,请确保避免创建具有相同键名或多个语义元素的重复语义元素,这些元素可以重复修改Value对象的属性SemanticValueSemanticResultKeySemanticResultValue 如果遇到这些情况,语音识别器可能会引发异常。

另请参阅

适用于

Append(Choices)

Source:
GrammarBuilder.cs
Source:
GrammarBuilder.cs
Source:
GrammarBuilder.cs
Source:
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

要追加的替代项集。

示例

以下示例为短语(如“在工作时呼叫詹姆斯”和“在手机中呼叫安妮”)创建语音识别语法,其中“电话”一词是可选的。 该示例突出显示了该方法的使用 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 添加到当前元素序列的末尾。

重要

将包含或实例的对象追加ChoicesGrammarBuilder对象时,请确保避免创建具有相同键名或多个语义元素的重复语义元素,这些元素可以重复修改Value对象的属性SemanticValueSemanticResultKeySemanticResultValue 如果遇到这些情况,语音识别器可能会引发异常。

适用于