Note

Please see Azure Cognitive Services for Speech documentation for the latest supported speech solutions.

GrammarBuilder.Append Method (SemanticResultKey)

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

Namespace:  Microsoft.Speech.Recognition
Assembly:  Microsoft.Speech (in Microsoft.Speech.dll)

Syntax

'Declaration
Public Sub Append ( _
    key As SemanticResultKey _
)
'Usage
Dim instance As GrammarBuilder
Dim key As SemanticResultKey

instance.Append(key)
public void Append(
    SemanticResultKey key
)

Parameters

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. For more information about building a speech recognition grammar that contains semantic information, see Add Semantics to a GrammarBuilder Grammar (Microsoft.Speech).

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

See Also

Reference

GrammarBuilder Class

GrammarBuilder Members

Append Overload

Microsoft.Speech.Recognition Namespace