UserToken Property

Microsoft Speech Platform SDK 11

Collapse imageExpand ImageCopy imageCopyHover image

Gets the UserToken passed to the system when an application calls RequestRecognizerUpdate()()()().

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

Syntax

Visual Basic (Declaration)
Public ReadOnly Property UserToken As Object
	Get
Visual Basic (Usage)
Dim instance As RecognizerUpdateReachedEventArgs
Dim value As Object

value = instance.UserToken
C#
public Object UserToken { get; }

Property Value

Type: System..::..Object

Returns an object that contains the UserToken.

Remarks

An application specifies a UserToken when it requests the generation of a RecognizerUpdateReached event by calling one of the SpeechRecognitionEngine..::..RequestRecognizerUpdate methods that take a userToken parameter.

Examples

The following example shows a console application that loads and unloads Grammar objects. The application uses the RequestRecognizerUpdate(Object) method to request the speech recognition engine to pause so it can receive an update. The method passes in a String object for the userToken parameter that describes what the application will recognize after the update. The application then loads or unloads a Grammar object.

At each update, a handler for SpeechRecognitionEngine..::..RecognizerUpdateReached event writes the name and status of the currently loaded Grammar objects to the console, as well as the contents of userToken. As grammars are loaded and unloaded, the application first recognizes the names of farm animals, then the names of farm animals and the names of fruits, then only the names of fruits.

 Copy imageCopy Code
using System;
using Microsoft.Speech.Recognition;
using System.Collections.Generic;
using System.Threading;

namespace SampleRecognition
{
  class Program
  {
    private static SpeechRecognitionEngine recognizer;
    public static void Main(string[] args)
    {

      // Initialize a SpeechRecognitionEngine object and configure its input.
      recognizer = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
      recognizer.SetInputToDefaultAudioDevice();

      // Create the first grammar - Farm.
      Choices animals = new Choices(new string[] { "cow", "pig", "goat" });
      GrammarBuilder farm = new GrammarBuilder(animals);
      Grammar farmAnimals = new Grammar(farm);
      farmAnimals.Name = "Farm";

      // Create the second grammar - Fruit.
      Choices fruit = new Choices(new string[] { "apples", "peaches", "oranges" });
      GrammarBuilder favorite = new GrammarBuilder(fruit);
      Grammar favoriteFruit = new Grammar(favorite);
      favoriteFruit.Name = "Fruit";

      // Attach event handlers.
      recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
      recognizer.RecognizerUpdateReached += new EventHandler<RecognizerUpdateReachedEventArgs>(recognizer_RecognizerUpdateReached);

      // Load the farmAnimals grammar
      recognizer.LoadGrammar(farmAnimals);

      // Start recognition.
      recognizer.RecognizeAsync(RecognizeMode.Multiple);
      Console.WriteLine("Starting asynchronous recognition...");
      Console.WriteLine("Farm animals will now be recognized.");
      Thread.Sleep(7000);
      Console.WriteLine();

      //Load the Fruit grammar.
      string activeGrammars = "Farm animals and fruits will now be recognized.";
      recognizer.RequestRecognizerUpdate(activeGrammars);
      recognizer.LoadGrammarAsync(favoriteFruit);
      Console.WriteLine();
      Thread.Sleep(7000);
      Console.WriteLine();

      // Unload the Farm grammar.
      string onlyFruit = "Only fruits will now be recognized.";
      recognizer.RequestRecognizerUpdate(onlyFruit);
      recognizer.UnloadGrammar(farmAnimals);

      // Keep the console window open.
      Console.ReadLine();
    }

    public static void recognizer_RecognizerUpdateReached(object sender, RecognizerUpdateReachedEventArgs e)
    {
      // At the update, describe what the application will recognize next.
      // Display the names and enabled status of the currently loaded grammars.
      Console.WriteLine("Update reached: " + e.UserToken);
      Thread.Sleep(1000);
      
      string qualifier;
      List<Grammar> grammars = new List<Grammar>(recognizer.Grammars);
      foreach (Grammar g in grammars)
      {
        qualifier = (g.Enabled) ? "enabled" : "disabled";
        Console.WriteLine("  Grammar {0} is loaded and is {1}.",
        g.Name, qualifier);
      }
    }
    
    // Write the text of the recognized phrase to the console.
    static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
      Console.WriteLine("  Speech recognized: " + e.Result.Text);
    }
  }
}

See Also