Grammar 建構函式

定義

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

多載

Grammar()

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

Grammar(SrgsDocument, String, Uri, Object[])

Grammar 執行個體初始化 SrgsDocument 類別的新執行個體,並指定要成為文法進入點的規則名稱和解析相對參考的基底 URI。

Grammar(Stream, String, Uri, Object[])

初始化 Grammar 類別的新執行個體 Stream,並指定根規則和解析相對參考的基底 URI。

Grammar(SrgsDocument, String, Uri)

SrgsDocument 物件初始化 類別的新實例 Grammar 、指定根規則,並定義基底統一資源識別項 (URI) 解析相對規則參考。

Grammar(SrgsDocument, String, Object[])

Grammar 執行個體初始化 SrgsDocument 類別的新執行個體,並指定要成為文法進入點的規則名稱。

Grammar(Stream, String, Uri)

從資料流初始化 Grammar 類別的新執行個體,指定根規則,並定義基底統一資源識別元 (URI) 來解析相對的規則參考。

Grammar(Stream, String, Object[])

Grammar 初始化 Stream 類別的新執行個體並指定根規則。

Grammar(String, String, Object[])

從包含文法定義的檔案中,初始化 Grammar 類別的新執行個體,並指定要成為文法進入點的規則名稱。

Grammar(SrgsDocument, String)

SrgsDocument 物件初始化 類別的新實例 Grammar ,並指定根規則。

Grammar(Stream, String)

Grammar 初始化 Stream 類別的新執行個體並指定根規則。

Grammar(String)

從檔案初始化 Grammar 類別的新執行個體。

Grammar(SrgsDocument)

Grammar 物件初始化 SrgsDocument 類別的新執行個體。

Grammar(String, String)

從檔案初始化 Grammar 類別的新執行個體並指定根規則。

Grammar(GrammarBuilder)

Grammar 物件初始化 GrammarBuilder 類別的新執行個體。

Grammar(Stream)

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

備註

您可以使用建 Grammar 構函 Grammar 式,從 GrammarBuilderSrgsDocument 物件建立實例,或是從包含支援格式之文法描述的 檔案或 Stream 。 支援的格式包括下列各項:

接受其引數中 XML 格式文法檔案的文法建構函式會將 XML 文法編譯成二進位格式,以優化它們以供語音辨識引擎載入和取用。 您可以使用其中 Compile 一種方法,預先編譯文法,以減少從 XML 格式文法建構 Grammar 物件所需的時間量。

語音辨識文法可以定義根規則。 若要建立 Grammar 物件,指定要做為其根規則使用的規則,請使用接受 參數的 ruleName 建構函式。

若要建立 Grammar 物件,指定解析相對規則參考的基底 URI,請使用採用 參數的 baseUri 建構函式。

Grammar()

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

protected:
 Grammar();
protected Grammar ();
Protected Sub New ()

適用於

Grammar(SrgsDocument, String, Uri, Object[])

Grammar 執行個體初始化 SrgsDocument 類別的新執行個體,並指定要成為文法進入點的規則名稱和解析相對參考的基底 URI。

public:
 Grammar(System::Speech::Recognition::SrgsGrammar::SrgsDocument ^ srgsDocument, System::String ^ ruleName, Uri ^ baseUri, cli::array <System::Object ^> ^ parameters);
public Grammar (System.Speech.Recognition.SrgsGrammar.SrgsDocument srgsDocument, string ruleName, Uri baseUri, object[] parameters);
new System.Speech.Recognition.Grammar : System.Speech.Recognition.SrgsGrammar.SrgsDocument * string * Uri * obj[] -> System.Speech.Recognition.Grammar
Public Sub New (srgsDocument As SrgsDocument, ruleName As String, baseUri As Uri, parameters As Object())

參數

srgsDocument
SrgsDocument

SrgsDocument的執行個體,其中包含語音辨識文法的條件約束。

ruleName
String

要當做語音辨識文法進入點之規則的識別項,若要使用文法描述的預設根規則則為 null

baseUri
Uri

在文法描述中用來解析任何相對規則參考的基底 URI,或 null

parameters
Object[]

要傳遞給初始化處理常式的參數,這個處理常式是即將建立之 OnInit 的進入點或根規則的 Grammar 屬性所指定。 這個參數可以是 null。

例外狀況

  • 包含無效值的任何參數。

  • srgsDocument 指定的 SrgsDocument 不包含 ruleName 所指定的規則。

  • 陣列參數的內容不符合任何規則之初始設定處理常式的引數。

  • 文法含有文法的預設基底 Uri 規則或 baseUri 提供的 URI 所無法解析的相對規則參考。

備註

您也可以指定初始化處理常式的參數。

適用於

Grammar(Stream, String, Uri, Object[])

初始化 Grammar 類別的新執行個體 Stream,並指定根規則和解析相對參考的基底 URI。

public:
 Grammar(System::IO::Stream ^ stream, System::String ^ ruleName, Uri ^ baseUri, cli::array <System::Object ^> ^ parameters);
public Grammar (System.IO.Stream stream, string ruleName, Uri baseUri, object[] parameters);
new System.Speech.Recognition.Grammar : System.IO.Stream * string * Uri * obj[] -> System.Speech.Recognition.Grammar
Public Sub New (stream As Stream, ruleName As String, baseUri As Uri, parameters As Object())

參數

stream
Stream

連接到輸入/輸出物件 (包括檔案、 VisualStudio 資源和 Dll) 的 Stream,其中包含文法規格。

ruleName
String

要當做語音辨識文法進入點之規則的識別項,若要使用文法描述的預設根規則則為 null

baseUri
Uri

在文法描述中用來解析任何相對規則參考的基底 URI,或 null

parameters
Object[]

要傳遞給初始化處理常式的參數,這個處理常式是即將建立之 OnInit 的進入點或根規則的 Grammar 屬性所指定。 這個參數可以是 null。

例外狀況

  • 包含無效值的任何參數。

  • stream 已連接到沒有包含 ruleName 所指定規則的文法。

  • 陣列參數的內容不符合任何規則之初始設定處理常式的引數。

  • 文法包含文法的預設基底 Uri 規則或 baseUri 提供的 URI 所無法解析的相對規則參考。

備註

您也可以指定初始化處理常式的參數。

適用於

Grammar(SrgsDocument, String, Uri)

SrgsDocument 物件初始化 類別的新實例 Grammar 、指定根規則,並定義基底統一資源識別項 (URI) 解析相對規則參考。

public:
 Grammar(System::Speech::Recognition::SrgsGrammar::SrgsDocument ^ srgsDocument, System::String ^ ruleName, Uri ^ baseUri);
public Grammar (System.Speech.Recognition.SrgsGrammar.SrgsDocument srgsDocument, string ruleName, Uri baseUri);
new System.Speech.Recognition.Grammar : System.Speech.Recognition.SrgsGrammar.SrgsDocument * string * Uri -> System.Speech.Recognition.Grammar
Public Sub New (srgsDocument As SrgsDocument, ruleName As String, baseUri As Uri)

參數

srgsDocument
SrgsDocument

語音辨識文法的條件約束。

ruleName
String

要當做語音辨識文法進入點的識別項,若要使用 SrgsDocument 的預設根規則則為 null

baseUri
Uri

SrgsDocument 中要用來解析任何相對規則參考的基底 URI,或 null

例外狀況

ruleName 無法解析或不是公用,或者 ruleNamenull,而且 srgsDocument 不包含根規則。

srgsDocumentnull

srgsDocument 包含無法解析的規則參考。

範例

下列範例會在 中 SrgsDocument 建立語音辨識文法,其中包含cities.xml檔案的相對規則參考,並指定用來解析規則參考的 URI。 cities.xml檔案的內容會出現在 C# 範例後面的 XML 範例中。

private static Grammar CreateSrgsDocumentGrammar3()  
{  
  // Create the SrgsDocument.  
  SrgsDocument document = new SrgsDocument();  

  // Create the Main rule and add it to the document.  
  SrgsRule mainRule = new SrgsRule("Main");  
  mainRule.Scope = SrgsRuleScope.Public;  

  SrgsItem item = new SrgsItem("Can I get a shuttle in");  

  // Create a relative URI for the cities rule.  
  Uri ruleUri = new Uri("cities.xml#Cities", UriKind.Relative);  

  item.Add(new SrgsRuleRef(ruleUri));  

  mainRule.Add(item);  
  document.Rules.Add(mainRule);  

  // Set the root rule.  
  document.Root = mainRule;  

  // Create the grammar.  
  Uri baseUri = new Uri(@"file://c:\temp\");  
  Grammar citiesGrammar = new Grammar(document, null, baseUri);  
  citiesGrammar.Name = "SrgsDocument Cities Grammar 3";  

  return citiesGrammar;  
}  
<?xml version="1.0" encoding="UTF-8" ?>  
<grammar version="1.0" xml:lang="en-US"  
         xmlns="http://www.w3.org/2001/06/grammar"  
         tag-format="semantics/1.0" root="Main">  

  <!-- cities.xml:   
    Defines an SRGS grammar for requesting a flight. This grammar includes  
    a Cities rule that lists the cities that can be used for departures  
    and destinations. -->  

  <rule id="Main">  
    <item>  
      I would like to fly from <ruleref uri="#Cities"/>  
      to <ruleref uri="#Cities"/>  
    </item>  
  </rule>  

  <rule id="Cities" scope="public">  
    <one-of>  
      <item>Seattle</item>  
      <item>Los Angeles</item>  
      <item>New York</item>  
      <item>Miami</item>  
    </one-of>  
  </rule>  
</grammar>  

備註

此建構函式不會將任何參數傳遞至初始化處理常式,而且 SrgsDocument 不應包含需要引數的初始化處理常式。

這個建構函式不會驗證 baseUri 。 不過, LoadGrammar 如果 或 SpeechRecognizer 物件的 方法 SpeechRecognitionEngine 無法解析文法描述中的所有規則參考,則會擲回例外狀況。 如果 baseUri 不是 null ,則 LoadGrammar 方法會使用 URI 來解析它無法解析的任何規則參考。 如果 baseUri 代表檔案,則 LoadGrammar 方法會在嘗試解析相對規則參考時,同時使用指定的檔案和檔案的目錄。

另請參閱

適用於

Grammar(SrgsDocument, String, Object[])

Grammar 執行個體初始化 SrgsDocument 類別的新執行個體,並指定要成為文法進入點的規則名稱。

public:
 Grammar(System::Speech::Recognition::SrgsGrammar::SrgsDocument ^ srgsDocument, System::String ^ ruleName, cli::array <System::Object ^> ^ parameters);
public Grammar (System.Speech.Recognition.SrgsGrammar.SrgsDocument srgsDocument, string ruleName, object[] parameters);
new System.Speech.Recognition.Grammar : System.Speech.Recognition.SrgsGrammar.SrgsDocument * string * obj[] -> System.Speech.Recognition.Grammar
Public Sub New (srgsDocument As SrgsDocument, ruleName As String, parameters As Object())

參數

srgsDocument
SrgsDocument

SrgsDocument的執行個體,其中包含語音辨識文法的條件約束。

ruleName
String

要當做語音辨識文法進入點之規則的識別項,若要使用文法描述的預設根規則則為 null

parameters
Object[]

要傳遞給初始化處理常式的參數,這個處理常式是即將建立之 OnInit 的進入點或根規則的 Grammar 屬性所指定。 這個參數可以是 null。

例外狀況

  • 包含無效值的任何參數。

  • srgsDocument 指定的 SrgsDocument 不包含 ruleName 所指定的規則。

  • 陣列參數的內容不符合任何規則之初始設定處理常式的引數。

備註

您也可以指定初始化處理常式的參數。

適用於

Grammar(Stream, String, Uri)

從資料流初始化 Grammar 類別的新執行個體,指定根規則,並定義基底統一資源識別元 (URI) 來解析相對的規則參考。

public:
 Grammar(System::IO::Stream ^ stream, System::String ^ ruleName, Uri ^ baseUri);
public Grammar (System.IO.Stream stream, string ruleName, Uri baseUri);
new System.Speech.Recognition.Grammar : System.IO.Stream * string * Uri -> System.Speech.Recognition.Grammar
Public Sub New (stream As Stream, ruleName As String, baseUri As Uri)

參數

stream
Stream

資料流,會以支援的格式描述語音辨識文法。

ruleName
String

要當做語音辨識文法進入點之規則的識別項,若要使用文法描述的預設根規則則為 null

baseUri
Uri

在文法描述中用來解析任何相對規則參考的基底 URI,或 null

例外狀況

ruleName 無法解析或不是公用,或者 ruleNamenull,而且文法描述未定義根規則。

streamnull

資料流未包含有效的描述,或其描述的文法包含無法解析的規則參考。

範例

下列範例會從檔案資料流程載入本機 SRGS 檔案 (shuttle.xml) 。 檔案包含cities.xml檔案中規則的相對規則參考,並指定用來解析規則參考的基底 URI。 shuttle.xml和cities.xml檔案的內容會出現在 C# 範例後面的 XML 範例中。

private static Grammar CreateGrammarFromStream3()  
{  
  FileInfo file = new FileInfo(@".\shuttle.xml");  
  Uri baseUri = new Uri(@"file://c:\temp\");  
  Grammar citiesGrammar = new Grammar(file.OpenRead(), null, baseUri);  
  citiesGrammar.Name = "Stream Cities Grammar 3";  
  return citiesGrammar;  
}  
<?xml version="1.0" encoding="UTF-8" ?>  
<grammar version="1.0" xml:lang="en-US"  
         xmlns="http://www.w3.org/2001/06/grammar"  
         tag-format="semantics/1.0" root="Main">  

  <!-- shuttle.xml:   
    Defines an SRGS grammar for asking about a shuttle service. This grammar  
    references a Cities rule that is defined in the cities.xml grammar. -->  

  <rule id="Main">  
    <item>  
      Can I get a shuttle in  
      <ruleref uri="cities.xml#Cities"/>  
    </item>  
  </rule>  
</grammar>  
<?xml version="1.0" encoding="UTF-8" ?>  
<grammar version="1.0" xml:lang="en-US"  
         xmlns="http://www.w3.org/2001/06/grammar"  
         tag-format="semantics/1.0" root="Main">  

  <!-- cities.xml:   
    Defines an SRGS grammar for requesting a flight. This grammar includes  
    a Cities rule that lists the cities that can be used for departures  
    and destinations. -->  

  <rule id="Main">  
    <item>  
      I would like to fly from <ruleref uri="#Cities"/>  
      to <ruleref uri="#Cities"/>  
    </item>  
  </rule>  

  <rule id="Cities" scope="public">  
    <one-of>  
      <item>Seattle</item>  
      <item>Los Angeles</item>  
      <item>New York</item>  
      <item>Miami</item>  
    </one-of>  
  </rule>  
</grammar>  

備註

此建構函式不會將任何參數傳遞至初始化處理常式,而且描述不應該定義需要引數的初始化處理常式。

此建構函式可以從下列格式建立 Grammar 實例:

此建構函式會將 XML 格式文法檔案編譯為二進位格式,以優化它們以供語音辨識引擎載入和取用。 您可以使用其中 Compile 一種方法,預先編譯文法,以減少從 XML 格式文法建構 Grammar 物件所需的時間量。

這個建構函式不會驗證 baseUri 。 不過, LoadGrammar 如果 或 SpeechRecognizer 物件的 方法 SpeechRecognitionEngine 無法解析文法描述中的所有規則參考,則會擲回例外狀況。 如果 baseUri 不是 null ,則 LoadGrammar 方法會使用 URI 來解析它無法解析的任何規則參考。 如果 baseUri 代表檔案,則 當嘗試解析相對規則參考時,會 LoadGrammar 同時使用指定的檔案和檔案的目錄。

另請參閱

適用於

Grammar(Stream, String, Object[])

Grammar 初始化 Stream 類別的新執行個體並指定根規則。

public:
 Grammar(System::IO::Stream ^ stream, System::String ^ ruleName, cli::array <System::Object ^> ^ parameters);
public Grammar (System.IO.Stream stream, string ruleName, object[] parameters);
new System.Speech.Recognition.Grammar : System.IO.Stream * string * obj[] -> System.Speech.Recognition.Grammar
Public Sub New (stream As Stream, ruleName As String, parameters As Object())

參數

stream
Stream

連接到輸入/輸出物件 (包括檔案、 VisualStudio 資源和 Dll) 的 Stream,其中包含文法規格。

ruleName
String

要當做語音辨識文法進入點之規則的識別項,若要使用文法描述的預設根規則則為 null

parameters
Object[]

要傳遞給初始化處理常式的參數,這個處理常式是即將建立之 OnInit 的進入點或根規則的 Grammar 屬性所指定。 這個參數可以是 null。

例外狀況

stream 已連接至文法:

  • 不包含 ruleName 中指定的規則

  • 需要不同於 parameters 中指定的初始化參數

  • 包含無法由文法的預設基底 Uri 規則解析的相對規則參考。

備註

您也可以指定初始化處理常式的參數。

適用於

Grammar(String, String, Object[])

從包含文法定義的檔案中,初始化 Grammar 類別的新執行個體,並指定要成為文法進入點的規則名稱。

public:
 Grammar(System::String ^ path, System::String ^ ruleName, cli::array <System::Object ^> ^ parameters);
public Grammar (string path, string ruleName, object[] parameters);
new System.Speech.Recognition.Grammar : string * string * obj[] -> System.Speech.Recognition.Grammar
Public Sub New (path As String, ruleName As String, parameters As Object())

參數

path
String

包含文法規格之檔案 (包括 DLL) 的路徑。

ruleName
String

要當做語音辨識文法進入點之規則的識別項,若要使用文法描述的預設根規則則為 null

parameters
Object[]

要傳遞給初始化處理常式的參數,這個處理常式是即將建立之 OnInit 的進入點或根規則的 Grammar 屬性所指定。 這個參數可以是 null。

例外狀況

  • 包含無效值的任何參數。

  • path 所指定的檔案不包含有效的文法或 ruleName 中指定的規則。

  • 陣列參數的內容不符合任何規則之初始設定處理常式的引數。

  • 文法含有文法的預設基底 Uri 規則無法解析的相對規則參考。

備註

您也可以指定初始化處理常式的參數。

適用於

Grammar(SrgsDocument, String)

SrgsDocument 物件初始化 類別的新實例 Grammar ,並指定根規則。

public:
 Grammar(System::Speech::Recognition::SrgsGrammar::SrgsDocument ^ srgsDocument, System::String ^ ruleName);
public Grammar (System.Speech.Recognition.SrgsGrammar.SrgsDocument srgsDocument, string ruleName);
new System.Speech.Recognition.Grammar : System.Speech.Recognition.SrgsGrammar.SrgsDocument * string -> System.Speech.Recognition.Grammar
Public Sub New (srgsDocument As SrgsDocument, ruleName As String)

參數

srgsDocument
SrgsDocument

語音辨識文法的條件約束。

ruleName
String

要當做語音辨識文法進入點的識別項,若要使用 SrgsDocument 的預設根規則則為 null

例外狀況

ruleName 無法解析或不是公用,或者 ruleNamenull,而且 srgsDocument 不包含根規則。

srgsDocumentnull

srgsDocument 包含無法解析的規則參考。

範例

下列範例會在 實例中 SrgsDocument 建立語音辨識文法,並指定要作為文法根規則的規則。 此範例會 Grammar 從 實例建構 物件, SrgsDocument 並將其載入語音辨識引擎。

using System;  
using System.Speech.Recognition;  
using System.Speech.Recognition.SrgsGrammar;  

namespace SampleRecognition  
{  
  class Program  
  {  
    static void Main(string[] args)  

    // Initialize an in-process speech recognition engine.  
    {  
      using (SpeechRecognitionEngine recognizer =  
         new SpeechRecognitionEngine())  
      {  

        // Create the SrgsDocument.  
        SrgsDocument document = new SrgsDocument();  

        // Create the Cities rule and add it to the document.  
        SrgsRule citiesRule = new SrgsRule("Cities");  
        citiesRule.Scope = SrgsRuleScope.Public;  

        SrgsOneOf cityChoice = new SrgsOneOf();  
        cityChoice.Add(new SrgsItem("Seattle"));  
        cityChoice.Add(new SrgsItem("Los Angeles"));  
        cityChoice.Add(new SrgsItem("New York"));  
        cityChoice.Add(new SrgsItem("Miami"));  

        citiesRule.Add(cityChoice);  
        document.Rules.Add(citiesRule);  

        // Create the Main rule and add it to the document.  
        SrgsRule mainRule = new SrgsRule("Main");  
        mainRule.Scope = SrgsRuleScope.Public;  

        mainRule.Add(new SrgsItem("I would like to fly from"));  
        mainRule.Add(new SrgsRuleRef(citiesRule));  
        mainRule.Add(new SrgsItem("to"));  
        mainRule.Add(new SrgsRuleRef(citiesRule));  

        document.Rules.Add(mainRule);  

        // Create the Grammar object and specify which rule to use as the root.  
        Grammar citiesGrammar = new Grammar(document,"Main");  

        // Load the grammar object to the recognizer.  
        recognizer.LoadGrammarAsync(citiesGrammar);  

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

        // Set the input to the recognizer.  
        recognizer.SetInputToDefaultAudioDevice();  

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

備註

此建構函式不會將任何參數傳遞至初始化處理常式,而且 SrgsDocument 不應包含需要引數的初始化處理常式。

若要從 SrgsDocument 建立 Grammar 物件,並指定要用來解析相對規則參考的基底 URI,請使用 建 Grammar 構函式。

另請參閱

適用於

Grammar(Stream, String)

Grammar 初始化 Stream 類別的新執行個體並指定根規則。

public:
 Grammar(System::IO::Stream ^ stream, System::String ^ ruleName);
public Grammar (System.IO.Stream stream, string ruleName);
new System.Speech.Recognition.Grammar : System.IO.Stream * string -> System.Speech.Recognition.Grammar
Public Sub New (stream As Stream, ruleName As String)

參數

stream
Stream

資料流,會以支援的格式描述語音辨識文法。

ruleName
String

要當做語音辨識文法進入點之規則的識別項,若要使用文法描述的預設根規則則為 null

例外狀況

ruleName 無法解析或不是公用,或者 ruleNamenull,而且文法描述未定義根規則。

streamnull

資料流未包含有效的描述,或其描述的文法包含無法解析的規則參考。

範例

下列範例會從檔案資料流程載入本機 SRGS 檔案 (cities.xml) ,並指定要作為文法根目錄的規則。 cities.xml檔案的內容會出現在 C# 範例後面的 XML 範例中。

// Load a cities grammar from an I/O stream, use a specific  
// rule as the root of the grammar, and return the new grammar.   
private static Grammar CreateGrammarFromStream2()  
{  
  FileInfo file = new FileInfo(@"c:\temp\cities.xml");  
  Grammar citiesGrammar = new Grammar(file.OpenRead(), "Main");  
  citiesGrammar.Name = "Stream Cities Grammar 2";  
  return citiesGrammar;  
}  
<?xml version="1.0" encoding="UTF-8" ?>  
<grammar version="1.0" xml:lang="en-US"  
         xmlns="http://www.w3.org/2001/06/grammar"  
         tag-format="semantics/1.0" root="Main">  

  <!-- cities.xml:   
    Defines an SRGS grammar for requesting a flight. This grammar includes  
    a Cities rule that lists the cities that can be used for departures  
    and destinations. -->  

  <rule id="Main">  
    <item>  
      I would like to fly from <ruleref uri="#Cities"/>  
      to <ruleref uri="#Cities"/>  
    </item>  
  </rule>  

  <rule id="Cities" scope="public">  
    <one-of>  
      <item>Seattle</item>  
      <item>Los Angeles</item>  
      <item>New York</item>  
      <item>Miami</item>  
    </one-of>  
  </rule>  
</grammar>  

備註

此建構函式不會將任何參數傳遞至初始化處理常式,而且描述不應該定義需要引數的初始化處理常式。

此建構函式可以從下列格式建立 Grammar 實例:

此建構函式會將 XML 格式文法檔案編譯為二進位格式,以優化它們以供語音辨識引擎載入和取用。 您可以使用其中 Compile 一種方法,預先編譯文法,以減少從 XML 格式文法建構 Grammar 物件所需的時間量。

若要從資料流程建立 Grammar ,並指定要用來解析相對規則參考的基底 URI,請使用 建 Grammar 構函式。

另請參閱

適用於

Grammar(String)

從檔案初始化 Grammar 類別的新執行個體。

public:
 Grammar(System::String ^ path);
public Grammar (string path);
new System.Speech.Recognition.Grammar : string -> System.Speech.Recognition.Grammar
Public Sub New (path As String)

參數

path
String

以支援格式描述語音辨識文法之檔案的路徑。

例外狀況

path 包含空字串 (""),或者該檔案描述不包含根規則的文法。

pathnull

檔案未包含有效的描述,或其描述的文法包含無法解析的規則參考。

範例

下列範例會從本機 SRGS 檔案載入語音辨識文法,以建置 Grammar 物件。 cities.xml檔案的內容會出現在 C# 範例後面的 XML 範例中。

// Load a cities grammar from a local file and  
// return the new grammar.   
private static Grammar CreateGrammarFromFile()  
{  
  Grammar citiesGrammar = new Grammar(@"c:\temp\cities.xml");  
  citiesGrammar.Name = "SRGS File Cities Grammar";  
  return citiesGrammar;  
}  
<?xml version="1.0" encoding="UTF-8" ?>  
<grammar version="1.0" xml:lang="en-US"  
         xmlns="http://www.w3.org/2001/06/grammar"  
         tag-format="semantics/1.0" root="Main">  

  <!-- cities.xml:   
    Defines an SRGS grammar for requesting a flight. This grammar includes  
    a Cities rule that lists the cities that can be used for departures  
    and destinations. -->  

  <rule id="Main">  
    <item>  
      I would like to fly from <ruleref uri="#Cities"/>  
      to <ruleref uri="#Cities"/>  
    </item>  
  </rule>  

  <rule id="Cities" scope="public">  
    <one-of>  
      <item>Seattle</item>  
      <item>Los Angeles</item>  
      <item>New York</item>  
      <item>Miami</item>  
    </one-of>  
  </rule>  
</grammar>  

備註

此建構函式不會將任何參數傳遞至初始化處理常式,而且描述不應該定義需要引數的初始化處理常式。

此建構函式可以從下列格式建立 Grammar 實例:

此建構函式會將 XML 格式文法檔案編譯為二進位格式,以優化它們以供語音辨識引擎載入和取用。 您可以使用其中 Compile 一種方法,預先編譯文法,以減少從 XML 格式文法建構 Grammar 物件所需的時間量。

SRGS 文法可以定義根規則。 若要從字串建立 Grammar 物件並指定根規則,請使用 建 Grammar 構函式。

若要建立 Grammar 物件,指定要用來解析相對規則參考的基底 URI,請在檔案資料流程中開啟檔案,並使用 建 Grammar.Grammar 構函式。

另請參閱

適用於

Grammar(SrgsDocument)

Grammar 物件初始化 SrgsDocument 類別的新執行個體。

public:
 Grammar(System::Speech::Recognition::SrgsGrammar::SrgsDocument ^ srgsDocument);
public Grammar (System.Speech.Recognition.SrgsGrammar.SrgsDocument srgsDocument);
new System.Speech.Recognition.Grammar : System.Speech.Recognition.SrgsGrammar.SrgsDocument -> System.Speech.Recognition.Grammar
Public Sub New (srgsDocument As SrgsDocument)

參數

srgsDocument
SrgsDocument

語音辨識文法的條件約束。

例外狀況

srgsDocument 不包含根規則。

srgsDocumentnull

srgsDocument 包含無法解析的規則參考。

範例

下列範例會在 實例中 SrgsDocument 建立語音辨識文法,然後用來建構 Grammar 物件。

private static Grammar CreateSrgsDocumentGrammar()  
{  
  // Create the SrgsDocument.  
  SrgsDocument document = new SrgsDocument();  

  // Create the Cities rule and add it to the document.  
  SrgsRule citiesRule = new SrgsRule("Cities");  

  SrgsOneOf cityChoice = new SrgsOneOf();  
  cityChoice.Add(new SrgsItem("Seattle"));  
  cityChoice.Add(new SrgsItem("Los Angeles"));  
  cityChoice.Add(new SrgsItem("New York"));  
  cityChoice.Add(new SrgsItem("Miami"));  

  citiesRule.Add(cityChoice);  
  document.Rules.Add(citiesRule);  

  // Create the Main rule and add it to the document.  
  SrgsRule mainRule = new SrgsRule("Main");  
  mainRule.Scope = SrgsRuleScope.Public;  

  SrgsItem item = new SrgsItem("I would like to fly from");  
  item.Add(new SrgsRuleRef(citiesRule));  
  item.Add(new SrgsText("to"));  
  item.Add(new SrgsRuleRef(citiesRule));  

  mainRule.Add(item);  
  document.Rules.Add(mainRule);  

  // Set the root rule.  
  document.Root = mainRule;  

  // Create the Grammar object.  
  Grammar citiesGrammar = new Grammar(document);  
  citiesGrammar.Name = "SrgsDocument Cities Grammar";  

  return citiesGrammar;  
}  

備註

此建構函式不會將任何參數傳遞至初始化處理常式,而且 SrgsDocument 不應包含需要引數的初始化處理常式。

SrgsDocument可以有根規則。 若要建立 Grammar 指定根規則的 物件,請使用 GrammarGrammar 建構函式。

若要從 SrgsDocument 建立語音辨識 Grammar ,並指定要用來解析相對規則參考的基底 URI,請使用 建 Grammar 構函式。

另請參閱

適用於

Grammar(String, String)

從檔案初始化 Grammar 類別的新執行個體並指定根規則。

public:
 Grammar(System::String ^ path, System::String ^ ruleName);
public Grammar (string path, string ruleName);
new System.Speech.Recognition.Grammar : string * string -> System.Speech.Recognition.Grammar
Public Sub New (path As String, ruleName As String)

參數

path
String

以支援格式描述語音辨識文法之檔案的路徑。

ruleName
String

要當做語音辨識文法進入點之規則的識別項,若要使用文法描述的預設根規則則為 null

例外狀況

ruleName 無法解析或不是公用,path 是空字串 (""),或者 ruleNamenull,而且文法描述未定義根規則。

pathnull

檔案未包含有效的描述,或其描述的文法包含無法解析的規則參考。

範例

下列範例會從檔案載入本機 SRGS 檔案 (cities.xml) ,並指定要作為文法根目錄的規則。 cities.xml檔案的內容會出現在 C# 範例後面的 XML 範例中。

// Load a cities grammar from a local file, use a specific  
// rule as the root of the grammar, and return the new grammar.  
private static Grammar CreateGrammarFromFile2()  
{  
  Grammar citiesGrammar = new Grammar(@"c:\temp\cities.xml", "Main");  
  citiesGrammar.Name = "SRGS File Cities Grammar 2";  
  return citiesGrammar;  
}  
<?xml version="1.0" encoding="UTF-8" ?>  
<grammar version="1.0" xml:lang="en-US"  
         xmlns="http://www.w3.org/2001/06/grammar"  
         tag-format="semantics/1.0" root="Main">  

  <!-- cities.xml:   
    Defines an SRGS grammar for requesting a flight. This grammar includes  
    a Cities rule that lists the cities that can be used for departures  
    and destinations. -->  

  <rule id="Main">  
    <item>  
      I would like to fly from <ruleref uri="#Cities"/>  
      to <ruleref uri="#Cities"/>  
    </item>  
  </rule>  

  <rule id="Cities" scope="public">  
    <one-of>  
      <item>Seattle</item>  
      <item>Los Angeles</item>  
      <item>New York</item>  
      <item>Miami</item>  
    </one-of>  
  </rule>  
</grammar>  

備註

此建構函式不會將任何參數傳遞至初始化處理常式,而且描述不應該定義需要引數的初始化處理常式。

此建構函式可以從下列格式建立 Grammar 實例:

此建構函式會將 XML 格式文法檔案編譯為二進位格式,以優化它們以供語音辨識引擎載入和取用。 您可以使用其中 Compile 一種方法,預先編譯文法,以減少從 XML 格式文法建構 Grammar 物件所需的時間量。

若要建立 Grammar ,指定要用來解析相對規則參考的基底 URI,請開啟檔案的檔案資料流程,並使用建 Grammar 構函式。

另請參閱

適用於

Grammar(GrammarBuilder)

Grammar 物件初始化 GrammarBuilder 類別的新執行個體。

public:
 Grammar(System::Speech::Recognition::GrammarBuilder ^ builder);
public Grammar (System.Speech.Recognition.GrammarBuilder builder);
new System.Speech.Recognition.Grammar : System.Speech.Recognition.GrammarBuilder -> System.Speech.Recognition.Grammar
Public Sub New (builder As GrammarBuilder)

參數

builder
GrammarBuilder

GrammarBuilder的執行個體,其中包含語音辨識文法的條件約束。

範例

下列範例會使用 ChoicesGrammarBuilder 物件建立語音辨識文法。 建 Grammar 構函式會 GrammarGrammarBuilder 物件建立 物件。

// Create a grammar using a GrammarBuilder and return the new grammar.   
private static Grammar CreateGrammarBuilderGrammar()  
{  
  GrammarBuilder builder = new GrammarBuilder();  

  Choices cityChoice = new Choices (new string[]   
  {"Seattle", "New York", "Miami", "Los Angeles"});  

  builder.Append("I would like to fly from");  
  builder.Append(cityChoice);  
  builder.Append("to");  
  builder.Append(cityChoice);  

  Grammar citiesGrammar = new Grammar(builder);  
  citiesGrammar.Name = "GrammarBuilder Cities Grammar";  

  return citiesGrammar;  
}  

另請參閱

適用於

Grammar(Stream)

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

public:
 Grammar(System::IO::Stream ^ stream);
public Grammar (System.IO.Stream stream);
new System.Speech.Recognition.Grammar : System.IO.Stream -> System.Speech.Recognition.Grammar
Public Sub New (stream As Stream)

參數

stream
Stream

資料流,會以支援的格式描述語音辨識文法。

例外狀況

stream 描述不包含根規則的文法。

streamnull

資料流未包含有效的文法描述,或其描述的文法包含無法解析的規則參考。

範例

下列範例會使用檔案資料流程,從本機 SRGS 檔案 (cities.xml 建立語音辨識文法) 。 cities.xml檔案的內容會出現在 C# 範例中。

// Load a cities grammar from an I/O stream and    
// return the new grammar.   
private static Grammar CreateGrammarFromStream()  
{  
  string fileName = @"c:\temp\cities.xml";  
  Grammar citiesGrammar =  
    new Grammar(new FileStream(fileName, FileMode.Open));  
  citiesGrammar.Name = "Stream Cities Grammar";  
  return citiesGrammar;  
}  
<?xml version="1.0" encoding="UTF-8" ?>  
<grammar version="1.0" xml:lang="en-US"  
         xmlns="http://www.w3.org/2001/06/grammar"  
         tag-format="semantics/1.0" root="Main">  

  <!-- cities.xml:   
    Defines an SRGS grammar for requesting a flight. This grammar includes  
    a Cities rule that lists the cities that can be used for departures  
    and destinations. -->  

  <rule id="Main">  
    <item>  
      I would like to fly from <ruleref uri="#Cities"/>  
      to <ruleref uri="#Cities"/>  
    </item>  
  </rule>  

  <rule id="Cities" scope="public">  
    <one-of>  
      <item>Seattle</item>  
      <item>Los Angeles</item>  
      <item>New York</item>  
      <item>Miami</item>  
    </one-of>  
  </rule>  
</grammar>  

備註

此建構函式不會將任何參數傳遞至初始化處理常式,而且描述不應該定義需要引數的初始化處理常式。

此建構函式可以從下列格式建立 Grammar 實例:

此建構函式會將 XML 格式文法檔案編譯為二進位格式,以優化它們以供語音辨識引擎載入和取用。 您可以使用其中 Compile 一種方法,預先編譯文法,以減少從 XML 格式文法建構 Grammar 物件所需的時間量。

SRGS 文法可以定義根規則。 若要從資料流程建立 Grammar 物件並指定根規則,請使用 GrammarGrammar 建構函式。

若要從資料流程建立 Grammar 物件,並指定要用來解析相對規則參考的基底 URI,請使用 建 Grammar 構函式。

另請參閱

適用於