Share via


SrgsRuleRef 建構函式

定義

初始化 SrgsRuleRef 類別的新執行個體。

多載

SrgsRuleRef(SrgsRule)

初始化 SrgsRuleRef 類別的新執行個體,並指定要參考的規則。

SrgsRuleRef(Uri)

初始化 SrgsRuleRef 類別的新執行個體,並指定要參考之外部文法檔案的位置。

SrgsRuleRef(SrgsRule, String)

初始化 SrgsRuleRef 類別的新執行個體,並指定要參考的規則和包含語意索引鍵的字串。

SrgsRuleRef(Uri, String)

初始化 SrgsRuleRef 類別的新執行個體,並指定要參考之外部文法檔案和規則識別項的位置。

SrgsRuleRef(SrgsRule, String, String)

初始化 SrgsRuleRef 類別的新執行個體,並指定要參考之規則、語意字典的字串別名,以及初始化參數。

SrgsRuleRef(Uri, String, String)

初始化 SrgsRuleRef 類別的新執行個體,並指定外部文法檔案的位置、規則識別項和語意字典的字串別名。

SrgsRuleRef(Uri, String, String, String)

初始化 SrgsRuleRef 類別的新執行個體,並指定外部文法檔案的位置、規則識別項、語意字典的字串別名,以及初始化參數。

SrgsRuleRef(SrgsRule)

來源:
SrgsRuleRef.cs
來源:
SrgsRuleRef.cs
來源:
SrgsRuleRef.cs

初始化 SrgsRuleRef 類別的新執行個體,並指定要參考的規則。

public:
 SrgsRuleRef(System::Speech::Recognition::SrgsGrammar::SrgsRule ^ rule);
public SrgsRuleRef (System.Speech.Recognition.SrgsGrammar.SrgsRule rule);
new System.Speech.Recognition.SrgsGrammar.SrgsRuleRef : System.Speech.Recognition.SrgsGrammar.SrgsRule -> System.Speech.Recognition.SrgsGrammar.SrgsRuleRef
Public Sub New (rule As SrgsRule)

參數

rule
SrgsRule

要參考的物件。

例外狀況

rulenull

範例

下列範例會建立一個文法,可辨識「已贏得 World Cup 的一名」片語,後面接著一個贏得 World Cup 的國家/地區名稱。 建立 SrgsRule 物件 winnerRule 並將字串識別碼 WorldCupWinner 提供給它之後,此範例會將「已贏得 World Cup 的一名國家」字串附加至規則。 使用 SrgsOneOf 物件時,此範例會建置歐洲國家/地區清單和南美洲國家/地區清單,並將每個清單新增至其各自的規則, ruleEuroperuleSAmerica 。 然後,此範例會建立 和 ruleSAmerica 的規則參考 ruleEurope ,並將其新增至 WorldCupWinner 規則。

public void WorldSoccerWinners ()
{

  // Create an SrgsDocument, create a new rule
  // and set its scope to public.
  SrgsDocument document = new SrgsDocument();
  SrgsRule winnerRule = new SrgsRule("WorldCupWinner");
  winnerRule.Scope = SrgsRuleScope.Public;

  // Add the introduction.
  winnerRule.Elements.Add(new SrgsItem("A nation that has won the world cup is: "));

  // Create the rule for the European nations.
  SrgsOneOf oneOfEurope = new SrgsOneOf(new SrgsItem[] {new SrgsItem("England"),
    new SrgsItem("France"), new SrgsItem("Germany"), new SrgsItem("Italy")});
  SrgsRule ruleEurope = (new SrgsRule("EuropeanNations", new SrgsElement[] {oneOfEurope}));

  // Create the rule for the South American nations.
  SrgsOneOf oneOfSAmerica = new SrgsOneOf(new SrgsItem[] {new SrgsItem("Argentina"),
    new SrgsItem("Brazil"), new SrgsItem("Uruguay")});
  SrgsRule ruleSAmerica = (new SrgsRule("SouthAmericanNations", new SrgsElement[] {oneOfSAmerica}));

  // Add references to winnerRule for ruleEurope and ruleSAmerica.
  winnerRule.Elements.Add(new SrgsOneOf(new SrgsItem[] {(new SrgsItem
    (new SrgsRuleRef(ruleEurope))), new SrgsItem(new SrgsRuleRef(ruleSAmerica))}));

  // Add all the rules to the document and make winnerRule
  // the root rule of the document.
  document.Rules.Add(new SrgsRule[] {winnerRule, ruleEurope, ruleSAmerica});
  document.Root = winnerRule;
}

建立的文法具有下列格式。

<grammar version="1.0" xml:lang="en-US" xmlns="http://www.w3.org/2001/06/grammar" root="WorldCupWinner">
    <rule id="WorldCupWinner" scope="public">
        <item> A nation that has won the world cup is </item>
        <one-of>
            <item>
                <ruleref uri="#EuropeanNations" />
            </item>
            <item>
                <ruleref uri="#SouthAmericanNations" />
            </item>
        </one-of>
    </rule>
    <rule id="EuropeanNations">
        <one-of>
            <item> England </item>
            <item> France </item>
            <item> Germany </item>
            <item> Italy </item>
        </one-of>
    </rule>
    <rule id="SouthAmericanNations">
        <one-of>
            <item> Argentina </item>
            <item> Brazil </item>
            <item> Uruguay </item>
        </one-of>
    </rule>
</grammar>

備註

此建構函式會在包含文法內建立物件的規則參考 SrgsRule 。 若要在外部文法檔案中建立元素的規則參考 rule ,請使用下列任何建構函式:

另請參閱

適用於

SrgsRuleRef(Uri)

來源:
SrgsRuleRef.cs
來源:
SrgsRuleRef.cs
來源:
SrgsRuleRef.cs

初始化 SrgsRuleRef 類別的新執行個體,並指定要參考之外部文法檔案的位置。

public:
 SrgsRuleRef(Uri ^ uri);
public SrgsRuleRef (Uri uri);
new System.Speech.Recognition.SrgsGrammar.SrgsRuleRef : Uri -> System.Speech.Recognition.SrgsGrammar.SrgsRuleRef
Public Sub New (uri As Uri)

參數

uri
Uri

在包含文法外部之文法檔案的位置。

例外狀況

urinull

範例

下列範例會建立應用程式的文法,以傳回公車公車服務的相關資訊。 第一個方法 GrammarUrlForRoute 會採用指定路由的字串,並將它附加至指定文法位置的字串。 這會指定該文法中的特定規則。 方法會 Uri 傳回該規則的 。

第二個方法 CreateGrammarForRouteSrgsDocument 建立名為 grammar 的專案,其規則參考是由 傳遞給 Uri 它的規則參考。 GrammarUrlForRoute 請注意,名為 _route 的變數是封入類別的成員。

private Uri GrammarUrlForRoute(string route)
{
  return new Uri("http://localhost/MyBus/MyBusLocations.grxml#LocationsForRoute" + route);
}

private SrgsDocument CreateGrammarForRoute()
{
  SrgsDocument grammar = new SrgsDocument();
  grammar.Mode = SrgsGrammarMode.Voice;
  SrgsRule rule = new SrgsRule("LocationsForRoute" + _route);
  SrgsRuleRef ruleref = new SrgsRuleRef(GrammarUrlForRoute(_route));

  SrgsSemanticInterpretationTag tag = new SrgsSemanticInterpretationTag ("$.Location = $$");

  rule.Elements.Add(ruleref);
  rule.Elements.Add(tag);
  grammar.Rules.Add(rule);
  grammar.Root = rule;
  return grammar;
}

注意

名為 _ route 的變數在上述範例中是未宣告且未定義的。 它應該宣告為 , String 並在編譯並執行上述範例之前,包含特定匯流排路線的路線號碼。

備註

此建構函式會建立外部文法檔案的規則參考。 URI 也可能包含要參考之規則的識別碼,例如 http://www.contoso.com/ExternalGrammar.grxml#targetRuleuri如果參數未指定規則識別碼,規則參考會指向目標文法的根規則。 若要在相同的文法中建立物件的規則參考 SrgsRule ,請使用下列任何建構函式:

另請參閱

適用於

SrgsRuleRef(SrgsRule, String)

來源:
SrgsRuleRef.cs
來源:
SrgsRuleRef.cs
來源:
SrgsRuleRef.cs

初始化 SrgsRuleRef 類別的新執行個體,並指定要參考的規則和包含語意索引鍵的字串。

public:
 SrgsRuleRef(System::Speech::Recognition::SrgsGrammar::SrgsRule ^ rule, System::String ^ semanticKey);
public SrgsRuleRef (System.Speech.Recognition.SrgsGrammar.SrgsRule rule, string semanticKey);
new System.Speech.Recognition.SrgsGrammar.SrgsRuleRef : System.Speech.Recognition.SrgsGrammar.SrgsRule * string -> System.Speech.Recognition.SrgsGrammar.SrgsRuleRef
Public Sub New (rule As SrgsRule, semanticKey As String)

參數

rule
SrgsRule

要參考的物件。

semanticKey
String

語意索引鍵。

範例

下列範例會建立文法,以選擇航班城市。 此範例會建構兩 SrgsRuleRef 個實例,每個實例都會指定語意索引鍵。 這兩個規則參考都會以相同的 SrgsRule 物件為目標,名為 cities ,但以不同的語意索引鍵標記規則參考的辨識結果。 語意索引鍵會將已辨識的城市識別為航班的出發城市或抵達城市。 事件的處理常式 SpeechRecognized 會使用索引鍵來擷取從辨識結果中 SrgsNameValueTag 建立的語意值。

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")))
      {

        // Build a rule with a list of cities, assign a semantic value to each city.
        SrgsItem chi = new SrgsItem("Chicago");
        chi.Add(new SrgsNameValueTag("ORD"));
        SrgsItem bos = new SrgsItem("Boston");
        bos.Add(new SrgsNameValueTag("BOS"));
        SrgsItem mia = new SrgsItem("Miami");
        mia.Add(new SrgsNameValueTag("MIA"));
        SrgsItem dal = new SrgsItem("Dallas");
        dal.Add(new SrgsNameValueTag("DFW"));

        SrgsOneOf cities = new SrgsOneOf(new SrgsItem[] { chi, bos, mia, dal });
        SrgsRule citiesRule = new SrgsRule("flightCities");
        citiesRule.Add(cities);

        // Build the root rule, add rule references to the cities rule.
        SrgsRule flightBooker = new SrgsRule("bookFlight");
        flightBooker.Add(new SrgsItem("I want to fly from"));
        flightBooker.Add(new SrgsRuleRef(citiesRule, "departureCity"));
        flightBooker.Add(new SrgsItem("to"));
        flightBooker.Add(new SrgsRuleRef(citiesRule, "arrivalCity"));

        // Build an SrgsDocument object from the flightBooker rule and add the cities rule.
        SrgsDocument cityChooser = new SrgsDocument(flightBooker);
        cityChooser.Rules.Add(citiesRule);

        // Create a Grammar object from the SrgsDocument and load it to the recognizer.
        Grammar departArrive = new Grammar(cityChooser);
        departArrive.Name = ("Cities Grammar");
        recognizer.LoadGrammarAsync(departArrive);

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

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

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

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

    // Handle the SpeechRecognized event.
    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["departureCity"].Value);
      Console.WriteLine("  The destination city is: " + e.Result.Semantics["arrivalCity"].Value);
    }
  }
}

備註

此建構函式會在包含文法內建立物件的規則參考 SrgsRule 。 若要在外部文法檔案中建立元素的規則參考 rule ,請使用下列任何建構函式:

另請參閱

適用於

SrgsRuleRef(Uri, String)

來源:
SrgsRuleRef.cs
來源:
SrgsRuleRef.cs
來源:
SrgsRuleRef.cs

初始化 SrgsRuleRef 類別的新執行個體,並指定要參考之外部文法檔案和規則識別項的位置。

public:
 SrgsRuleRef(Uri ^ uri, System::String ^ rule);
public SrgsRuleRef (Uri uri, string rule);
new System.Speech.Recognition.SrgsGrammar.SrgsRuleRef : Uri * string -> System.Speech.Recognition.SrgsGrammar.SrgsRuleRef
Public Sub New (uri As Uri, rule As String)

參數

uri
Uri

在包含文法外部之文法檔案的位置。

rule
String

要參之規則的識別項。

例外狀況

urinull

rulenull

rule 是空的。

備註

此建構函式會建立外部文法檔案中元素的規則參考 rule 。 若要在相同的文法中建立物件的規則參考 SrgsRule ,請使用下列任何建構函式:

另請參閱

適用於

SrgsRuleRef(SrgsRule, String, String)

來源:
SrgsRuleRef.cs
來源:
SrgsRuleRef.cs
來源:
SrgsRuleRef.cs

初始化 SrgsRuleRef 類別的新執行個體,並指定要參考之規則、語意字典的字串別名,以及初始化參數。

public:
 SrgsRuleRef(System::Speech::Recognition::SrgsGrammar::SrgsRule ^ rule, System::String ^ semanticKey, System::String ^ parameters);
public SrgsRuleRef (System.Speech.Recognition.SrgsGrammar.SrgsRule rule, string semanticKey, string parameters);
new System.Speech.Recognition.SrgsGrammar.SrgsRuleRef : System.Speech.Recognition.SrgsGrammar.SrgsRule * string * string -> System.Speech.Recognition.SrgsGrammar.SrgsRuleRef
Public Sub New (rule As SrgsRule, semanticKey As String, parameters As String)

參數

rule
SrgsRule

要參考的物件。

semanticKey
String

語意索引鍵。

parameters
String

SrgsRuleRef 物件的初始化參數。

備註

此建構函式會在包含文法內建立物件的規則參考 SrgsRule 。 若要在外部文法檔案中建立元素的規則參考 rule ,請使用下列任何建構函式:

另請參閱

適用於

SrgsRuleRef(Uri, String, String)

來源:
SrgsRuleRef.cs
來源:
SrgsRuleRef.cs
來源:
SrgsRuleRef.cs

初始化 SrgsRuleRef 類別的新執行個體,並指定外部文法檔案的位置、規則識別項和語意字典的字串別名。

public:
 SrgsRuleRef(Uri ^ uri, System::String ^ rule, System::String ^ semanticKey);
public SrgsRuleRef (Uri uri, string rule, string semanticKey);
new System.Speech.Recognition.SrgsGrammar.SrgsRuleRef : Uri * string * string -> System.Speech.Recognition.SrgsGrammar.SrgsRuleRef
Public Sub New (uri As Uri, rule As String, semanticKey As String)

參數

uri
Uri

在包含文法外部之文法檔案的位置。

rule
String

要參之規則的識別項。

semanticKey
String

語意字典的別名字串。

例外狀況

urinull

semanticKeynull

semanticKey 是空的。

備註

此建構函式會建立外部文法檔案中元素的規則參考 rule 。 若要在相同的文法中建立物件的規則參考 SrgsRule ,請使用下列任何建構函式:

另請參閱

適用於

SrgsRuleRef(Uri, String, String, String)

來源:
SrgsRuleRef.cs
來源:
SrgsRuleRef.cs
來源:
SrgsRuleRef.cs

初始化 SrgsRuleRef 類別的新執行個體,並指定外部文法檔案的位置、規則識別項、語意字典的字串別名,以及初始化參數。

public:
 SrgsRuleRef(Uri ^ uri, System::String ^ rule, System::String ^ semanticKey, System::String ^ parameters);
public SrgsRuleRef (Uri uri, string rule, string semanticKey, string parameters);
new System.Speech.Recognition.SrgsGrammar.SrgsRuleRef : Uri * string * string * string -> System.Speech.Recognition.SrgsGrammar.SrgsRuleRef
Public Sub New (uri As Uri, rule As String, semanticKey As String, parameters As String)

參數

uri
Uri

在包含文法外部之文法檔案的位置。

rule
String

要參之規則的識別項。

semanticKey
String

語意索引鍵。

parameters
String

SrgsRuleRef 物件的初始化參數。

備註

此建構函式會建立外部文法檔案中元素的規則參考 rule 。 若要在相同的文法中建立物件的規則參考 SrgsRule ,請使用下列任何建構函式:

另請參閱

適用於