Export (0) Print
Expand All

SrgsSemanticInterpretationTag Class

Represents a tag that contains ECMAScript that is run when the rule is matched.


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

public sealed class SrgsSemanticInterpretationTag : SrgsElement

The default semantic format for the Microsoft Speech Platform SDK 11 is semantics-ms/1.0, a format defined by Microsoft. You must specify the script for SrgsSemanticInterpretationTag objects using this format. In the syntax of semantics-ms/1.0:

  • The Rule Variable of a rule is identified by "$".

  • The Rule Variable of a rule that is outside of the containing rule is identified by "$rulename", where rulename is the name of the referenced rule.

  • The result from the latest referenced rule that matches the utterance can be represented by the shorthand symbol "$$".

The following example creates a grammar for choosing the cities for a flight. The example uses SrgsSemanticInterpretationTag to assign a semantic value to each city, which is the code for the city's airport. The example also uses SrgsSemanticInterpretationTag to assign a separate semantic key for each of the two references made by the SrgsRuleRef object named cityRef to the SrgsRule object named cities. The semantic keys identify a recognized city as the departure city or the arrival city for the flight. The handler for the SpeechRecognized event uses the keys to retrieve the semantics of the recognition result.

In the code example, the symbol "$" refers to the Rule Variable of the containing SrgsRule. The expression "$.LeavingFrom" refers to the property named LeavingFrom of the Rule Variable on the rule named bookFlight.

The expression "$flightCities" refers to the Rule Variable on the rule whose Id is flightCities, and which is the target of a rule reference. In the example, the expression "$.LeavingFrom=$flightCities;" assigns the value from the rule whose Id is flightCities to the property named LeavingFrom of the Rule Variable on the rule named bookFlight. See Semantic Results Content (Microsoft.Speech), Grammar Rule Name Referencing (Microsoft.Speech), and Grammar Rule Reference Referencing (Microsoft.Speech) for more information.

using System;
using Microsoft.Speech.Recognition;
using Microsoft.Speech.Recognition.SrgsGrammar;

namespace SampleRecognition
  class Program
    static void Main(string[] args)

    // Initialize a SpeechRecognitionEngine object.
      using (SpeechRecognitionEngine recognizer =
         new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US")))

        // Create a rule for the origin cities, assign a semantic value to each city.
        SrgsRule cities = new SrgsRule("flightCities");
        SrgsItem chi = new SrgsItem("Chicago");
        chi.Add(new SrgsSemanticInterpretationTag("$ = \"ORD\";"));
        SrgsItem bos = new SrgsItem("Boston");
        bos.Add(new SrgsSemanticInterpretationTag("$ = \"BOS\";"));
        SrgsItem mia = new SrgsItem("Miami");
        mia.Add(new SrgsSemanticInterpretationTag("$ = \"MIA\";"));
        SrgsItem dal = new SrgsItem("Dallas");
        dal.Add(new SrgsSemanticInterpretationTag("$ = \"DFW\";"));

        SrgsOneOf airports = new SrgsOneOf(chi, bos, mia, dal);
        cities.Scope = SrgsRuleScope.Private;

        // Create a rule reference to rule for cities.
        SrgsRuleRef cityRef = new SrgsRuleRef(cities);

        // Create the root rule for the grammar.
        SrgsRule bookFlight = new SrgsRule("flightBooker");
        bookFlight.Add(new SrgsItem("I want to fly from"));
        bookFlight.Add(new SrgsSemanticInterpretationTag("$.LeavingFrom=$flightCities;"));
        bookFlight.Add(new SrgsItem("to"));
        bookFlight.Add(new SrgsSemanticInterpretationTag("$.GoingTo=$flightCities;"));
        bookFlight.Scope = SrgsRuleScope.Public;

        // Initialize the SrgsDocument, set the root rule, add rules to the collection.
        SrgsDocument itinerary = new SrgsDocument(bookFlight);

        // Create a Grammar object and load it to the recognizer.
        Grammar g = new Grammar(itinerary);
        g.Name = ("City Chooser");

        // Configure recognizer input.                

        // Attach a handler for the SpeechRecognized event.
        recognizer.SpeechRecognized +=
          new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);

        // Start recognition.
        Console.WriteLine("Starting asynchronous recognition...");

        // Keep the console window open.

    // Write to the console the text and the semantics from the recognition result.
    static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
      Console.WriteLine("Speech recognized: " + e.Result.Text);
      Console.WriteLine("Semantic results:");
      Console.WriteLine("  The departure city is: " + e.Result.Semantics["LeavingFrom"].Value);
      Console.WriteLine("  The arrival city is: " + e.Result.Semantics["GoingTo"].Value);

The following is the XML form of the grammar generated by the code in the example above.

<?xml version="1.0" encoding="utf-8"?>
<grammar xml:lang="en-US" root="flightBooker" tag-format="semantics-ms/1.0" 
version="1.0" xmlns="http://www.w3.org/2001/06/grammar">

  <rule id="flightBooker" scope="public">
    <item> I want to fly from </item>
    <ruleref uri="#flightCities" /> 
    <tag> $.LeavingFrom=$flightCities; </tag>
    <item> to </item>
    <ruleref uri="#flightCities" /> 
    <tag> $.GoingTo=$flightCities; </tag>

  <rule id="flightCities" scope="private">
      <item> Chicago <tag> $="ORD"; </tag></item>
      <item> Boston <tag> $="BOS"; </tag></item>
      <item> Miami <tag> $="MIA"; </tag></item>
      <item> Dallas <tag> $="DFW"; </tag></item>


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
© 2015 Microsoft