Share via


Note

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

SrgsSemanticInterpretationTag Class

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

Inheritance Hierarchy

System.Object
  System.MarshalByRefObject
    Microsoft.Speech.Recognition.SrgsGrammar.SrgsElement
      Microsoft.Speech.Recognition.SrgsGrammar.SrgsSemanticInterpretationTag

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

Syntax

'Declaration
<SerializableAttribute> _
Public NotInheritable Class SrgsSemanticInterpretationTag _
    Inherits SrgsElement
'Usage
Dim instance As SrgsSemanticInterpretationTag
[SerializableAttribute]
public sealed class SrgsSemanticInterpretationTag : SrgsElement

Remarks

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 "$$".

Examples

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.Add(airports);
        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(cityRef);
        bookFlight.Add(new SrgsSemanticInterpretationTag("$.LeavingFrom=$flightCities;"));
        bookFlight.Add(new SrgsItem("to"));
        bookFlight.Add(cityRef);
        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);
        itinerary.Rules.Add(cities);

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

        // Configure recognizer input.                
        recognizer.SetInputToDefaultAudioDevice();

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

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

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

    // 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();
      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>

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

</grammar>

Thread Safety

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

See Also

Reference

SrgsSemanticInterpretationTag Members

Microsoft.Speech.Recognition.SrgsGrammar Namespace