Κοινή χρήση μέσω


SrgsSemanticInterpretationTag Class

Definition

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

public ref class SrgsSemanticInterpretationTag : System::Speech::Recognition::SrgsGrammar::SrgsElement
public class SrgsSemanticInterpretationTag : System.Speech.Recognition.SrgsGrammar.SrgsElement
[System.Serializable]
public class SrgsSemanticInterpretationTag : System.Speech.Recognition.SrgsGrammar.SrgsElement
type SrgsSemanticInterpretationTag = class
    inherit SrgsElement
[<System.Serializable>]
type SrgsSemanticInterpretationTag = class
    inherit SrgsElement
Public Class SrgsSemanticInterpretationTag
Inherits SrgsElement
Inheritance
SrgsSemanticInterpretationTag
Attributes

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 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>

Remarks

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.

Constructors

SrgsSemanticInterpretationTag()

Creates an instance of the SrgsSemanticInterpretationTag class.

SrgsSemanticInterpretationTag(String)

Creates an instance of the SrgsSemanticInterpretationTag class, specifying the script contents of the tag.

Properties

Script

Gets or sets the ECMAScript for the tag.

Methods

CreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(Inherited from MarshalByRefObject)
Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetLifetimeService()
Obsolete.

Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
InitializeLifetimeService()
Obsolete.

Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to