GrammarBuilder.Append 方法

定义

为语法元素的当前顺序追加语法元素。

重载

Append(String, Int32, Int32)

为语法元素的当前顺序追加重复的短语。

Append(GrammarBuilder, Int32, Int32)

为语法元素的当前顺序追加重复的语法元素。

Append(String)

为语法元素的当前顺序追加短语。

Append(String, SubsetMatchingMode)

为语法元素的当前顺序追加一个短语子集元素。

Append(SemanticResultKey)

为语法元素的当前顺序追加语义密钥。

Append(SemanticResultValue)

为语法元素的当前顺序追加语义值。

Append(GrammarBuilder)

为语法元素的当前顺序追加语法元素。

Append(Choices)

为语法元素的当前顺序追加替代项集合。

注解

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

此方法具有用于追加 GrammarBuilder、、StringChoicesSemanticResultKeySemanticResultValue 对象的重载。

重要

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

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

Append(String, Int32, Int32)

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 的最大次数。

示例

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

为语法元素的当前顺序追加重复的语法元素。

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 的最大次数。

示例

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

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

为语法元素的当前顺序追加一个短语子集元素。

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

为语法元素的当前顺序追加语义密钥。

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

重要

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

另请参阅

适用于

Append(SemanticResultValue)

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

重要

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

另请参阅

适用于

Append(GrammarBuilder)

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

追加的语法元素。

示例

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

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

要追加的替代组。

示例

以下示例为短语(例如“在工作时呼叫 James”和“使用她的手机呼叫 Anne”)创建语音识别语法,其中“电话”一词是可选的。 该示例突出显示了 方法的使用 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元素。 如果遇到这些情况,语音识别器可能会引发异常。

适用于