Append Method (SemanticResultKey)

Microsoft Speech Platform SDK 11

Collapse image Expand Image Copy image CopyHover image

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

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

Syntax

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

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

Parameters

key
Type: Microsoft.Speech.Recognition..::..SemanticResultKey

The semantic key to append.

Remarks

key is added to the end of the current sequence of elements.

Important noteImportant

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.

C# Copy imageCopy Code
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