Condividi tramite


SrgsSemanticInterpretationTag Classe

Definizione

Rappresenta un tag che contiene ECMAScript che viene eseguito quando la regola viene confrontata.

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
Ereditarietà
SrgsSemanticInterpretationTag
Attributi

Esempio

Nell'esempio seguente viene creata una grammatica per la scelta delle città per un volo. Nell'esempio viene SrgsSemanticInterpretationTag usato per assegnare un valore semantico a ogni città, ovvero il codice per l'aeroporto della città. L'esempio usa SrgsSemanticInterpretationTag anche per assegnare una chiave semantica separata per ognuno dei due riferimenti effettuati dall'oggetto SrgsRuleRef denominato cityRef all'oggetto SrgsRule denominato cities. Le chiavi semantiche identificano una città riconosciuta come città di partenza o la città di arrivo per il volo. Il gestore per l'evento SpeechRecognized usa le chiavi per recuperare la semantica dal risultato del riconoscimento.

Nell'esempio di codice "out" si riferisce alla variabile regola dell'oggetto contenente SrgsRule. Espressione "out. LeavingFrom" fa riferimento alla proprietà denominata LeavingFrom della variabile di regola nella regola denominata bookFlight.

L'espressione "rules.flightCities" fa riferimento alla variabile della regola la cui Id è flightCitiese che è la destinazione di un riferimento a una regola. Nell'esempio l'espressione "out". LeavingFrom=rules.flightCities;" assegna il valore dalla regola la cui Id proprietà è flightCities denominata LeavingFrom della variabile regola sulla regola denominata bookFlight.

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);
    }
  }
}

Di seguito è riportato il formato XML della grammatica generata dal codice nell'esempio precedente.

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

Commenti

Il formato semantico predefinito per System.Speech è conforme all'interpretazione semantica W3C per il riconoscimento vocale (SISR) versione 1.0, dove il formato per tag gli elementi che contengono script è semantics/1.0. È necessario specificare lo script per SrgsSemanticInterpretationTag gli oggetti usando questo formato. Nella sintassi di semantics/1.0:

  • La variabile rule dell'elemento della regola contenitore è identificata da "out".
  • Il nome dell'oggetto che ha accesso alla variabile regola degli elementi della regola all'esterno dell'elemento della regola contenitore viene identificato da "rules".
  • Il risultato della regola a cui si fa riferimento più recente che corrisponde all'espressione può essere rappresentato da "rules.latest()".

È anche possibile associare un valore semantico a una frase in una grammatica senza usare script, usando l'oggetto SrgsNameValueTag .

Costruttori

Nome Descrizione
SrgsSemanticInterpretationTag()

Crea un'istanza della classe SrgsSemanticInterpretationTag.

SrgsSemanticInterpretationTag(String)

Crea un'istanza della SrgsSemanticInterpretationTag classe , specificando il contenuto dello script del tag.

Proprietà

Nome Descrizione
Script

Ottiene o imposta l'ECMAScript per il tag.

Metodi

Nome Descrizione
CreateObjRef(Type)

Crea un oggetto che contiene tutte le informazioni pertinenti necessarie per generare un proxy utilizzato per comunicare con un oggetto remoto.

(Ereditato da MarshalByRefObject)
Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetLifetimeService()
Obsoleti.

Recupera l'oggetto servizio di durata corrente che controlla i criteri di durata per questa istanza.

(Ereditato da MarshalByRefObject)
GetType()

Ottiene il Type dell'istanza corrente.

(Ereditato da Object)
InitializeLifetimeService()
Obsoleti.

Ottiene un oggetto servizio di durata per controllare i criteri di durata per questa istanza.

(Ereditato da MarshalByRefObject)
MemberwiseClone()

Crea una copia superficiale del Objectcorrente.

(Ereditato da Object)
MemberwiseClone(Boolean)

Crea una copia superficiale dell'oggetto corrente MarshalByRefObject .

(Ereditato da MarshalByRefObject)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Si applica a