This documentation is archived and is not being maintained.

SrgsSemanticInterpretationTag Class

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

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

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

[SerializableAttribute]
public ref class SrgsSemanticInterpretationTag : public SrgsElement

The SrgsSemanticInterpretationTag type exposes the following members.

  NameDescription
Public methodSrgsSemanticInterpretationTag()Creates an instance of the SrgsSemanticInterpretationTag class.
Public methodSrgsSemanticInterpretationTag(String)Creates an instance of the SrgsSemanticInterpretationTag class, specifying the script contents of the tag.
Top

  NameDescription
Public propertyScriptGets or sets the ECMAScript for the tag.
Top

  NameDescription
Public methodCreateObjRefCreates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetLifetimeServiceRetrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInitializeLifetimeServiceObtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected methodMemberwiseClone()Creates a shallow copy of the current Object. (Inherited from Object.)
Protected methodMemberwiseClone(Boolean)Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The default semantic format for System.Speech conforms to the W3C Semantic Interpretation for Speech Recognition (SISR) Version 1.0, where the format for tag elements that contain script is semantics/1.0. You must specify the script for SrgsSemanticInterpretationTag objects using this format. In the syntax of semantics/1.0:

  • The Rule Variable of the containing rule element is identified by "out".

  • The name of the object that has access to the Rule Variable of rule elements outside the containing rule element is identified by "rules".

  • The result from the latest referenced rule that matches the utterance can be represented by "rules.latest()".

You can also associate a semantic value with a phrase in a grammar without using script, using the SrgsNameValueTag object.

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 from the recognition result.

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

The expression "rules.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 "out.LeavingFrom=rules.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, Grammar Rule Name Referencing, and Grammar Rule Reference Referencing for more information.

using System;
using System.Speech.Recognition;
using System.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 cities, assign a semantic value to each city.
        SrgsRule cities = new SrgsRule("flightCities");
        SrgsItem chi = new SrgsItem("Chicago");
        chi.Add(new SrgsSemanticInterpretationTag("out = \"ORD\";"));
        SrgsItem bos = new SrgsItem("Boston");
        bos.Add(new SrgsSemanticInterpretationTag("out = \"BOS\";"));
        SrgsItem mia = new SrgsItem("Miami");
        mia.Add(new SrgsSemanticInterpretationTag("out = \"MIA\";"));
        SrgsItem dal = new SrgsItem("Dallas");
        dal.Add(new SrgsSemanticInterpretationTag("out = \"DFW\";"));

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

        // Create a rule reference to the 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("out.LeavingFrom=rules.flightCities;"));
        bookFlight.Add(new SrgsItem("to"));
        bookFlight.Add(cityRef);
        bookFlight.Add(new SrgsSemanticInterpretationTag("out.GoingTo=rules.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/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> out.LeavingFrom=rules.flightCities; </tag>
    <item> to </item>
    <ruleref uri="#flightCities" /> 
    <tag> out.GoingTo=rules.flightCities; </tag>
  </rule>

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

</grammar>

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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