SrgsSemanticInterpretationTag 類別

定義

表示標記,其中包含符合規則時執行的 ECMAScript

public ref class SrgsSemanticInterpretationTag : System::Speech::Recognition::SrgsGrammar::SrgsElement
[System.Serializable]
public class SrgsSemanticInterpretationTag : System.Speech.Recognition.SrgsGrammar.SrgsElement
public class SrgsSemanticInterpretationTag : System.Speech.Recognition.SrgsGrammar.SrgsElement
[<System.Serializable>]
type SrgsSemanticInterpretationTag = class
    inherit SrgsElement
type SrgsSemanticInterpretationTag = class
    inherit SrgsElement
Public Class SrgsSemanticInterpretationTag
Inherits SrgsElement
繼承
SrgsSemanticInterpretationTag
屬性

範例

下列範例會建立文法,以選擇航班城市。 此範例會使用 SrgsSemanticInterpretationTag 將語意值指派給每個城市,也就是城市機場的程式碼。 此範例也會使用 SrgsSemanticInterpretationTag ,為名為 cityRef 的物件所 SrgsRuleRef 建立的每個參考指派個別的語意索引鍵給 SrgsRule 名為 cities 的物件。 語意索引鍵會將已辨識的城市識別為航班的出發城市或抵達城市。 事件的處理常式 SpeechRecognized 會使用索引鍵從辨識結果擷取語意。

在程式碼範例中,「out」 是指包含 SrgsRule 的規則變數。 運算式 「out.LeavingFrom「 是指規則上 bookFlight 名為 LeavingFrom Rule Variable 的屬性。

運算式 「rules.flightCities」 是指規則上的規則變數,其 IdflightCities ,而該規則是規則參考的目標。 在範例中,運算式 「out」。LeavingFrom=rules.flightCities;「 會將規則 IdflightCities 的值指派給規則 bookFlight 上名為 LeavingFrom Rule Variable 的屬性。 如需詳細資訊,請參閱 語意結果內容文法規則名稱參考文法規則參考 參考。

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

以下是上述範例中程式碼所產生的文法 XML 形式。

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

備註

System.Speech 的預設語意格式符合適用于語音辨識的 W3C 語意解譯 (SISR) 1.0版,其中包含腳本的專案格式 tagsemantics/1.0 。 您必須使用此格式指定 SrgsSemanticInterpretationTag 物件的腳本。 在 的 semantics/1.0 語法中:

  • 包含規則專案的 Rule Variable 是由 「out」 所識別。

  • 可存取包含規則專案外部之規則專案之 Rule Variable 的物件名稱是由 「rules」 識別。

  • 符合語句的最新參考規則結果可由 「rules.latest () 」 表示。

您也可以使用 物件,將語意值與文法中的片語產生關聯,而不使用 SrgsNameValueTag 腳本。

建構函式

SrgsSemanticInterpretationTag()

建立 SrgsSemanticInterpretationTag 類別的執行個體。

SrgsSemanticInterpretationTag(String)

建立SrgsSemanticInterpretationTag類別的執行個體,指定標記的指令碼內容。

屬性

Script

取得或設定標記的 ECMAScript

方法

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。

(繼承來源 MarshalByRefObject)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetLifetimeService()
已淘汰.

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
InitializeLifetimeService()
已淘汰.

取得存留期服務物件,以控制這個執行個體的存留期原則。

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。

(繼承來源 MarshalByRefObject)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於