Grammar Constructors

Definition

Initializes a new instance of the Grammar class.

Overloads

Grammar()

Initializes a new instance of the Grammar class.

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

Initializes a new instance of the Grammar class from an instance of SrgsDocument, and specifies the name of a rule to be the entry point to the grammar and a base URI to resolve relative references.

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

Initializes a new instance of the Grammar class a Stream and specifies a root rule and a base URI to resolve relative references.

Grammar(SrgsDocument, String, Uri)

Initializes a new instance of the Grammar class from an SrgsDocument object, specifies a root rule, and defines a base Uniform Resource Identifier (URI) to resolve relative rule references.

Grammar(SrgsDocument, String, Object[])

Initializes a new instance of the Grammar class from an instance of SrgsDocument, and specifies the name of a rule to be the entry point to the grammar.

Grammar(Stream, String, Uri)

Initializes a new instance of the Grammar class from a stream, specifies a root rule, and defines a base Uniform Resource Identifier (URI) to resolve relative rule references.

Grammar(Stream, String, Object[])

Initializes a new instance of the Grammar class from a Stream and specifies a root rule.

Grammar(String, String, Object[])

Initializes a new instance of the Grammar class from a file that contains a grammar definition, and specifies the name of a rule to be the entry point to the grammar.

Grammar(SrgsDocument, String)

Initializes a new instance of the Grammar class from an SrgsDocument object and specifies a root rule.

Grammar(Stream, String)

Initializes a new instance of the Grammar class from a Stream and specifies a root rule.

Grammar(String)

Initializes a new instance of the Grammar class from a file.

Grammar(SrgsDocument)

Initializes a new instance of the Grammar class from an SrgsDocument object.

Grammar(String, String)

Initializes a new instance of the Grammar class from a file and specifies a root rule.

Grammar(GrammarBuilder)

Initializes a new instance of the Grammar class from a GrammarBuilder object.

Grammar(Stream)

Initializes a new instance of the Grammar class from a Stream.

Remarks

You can use a Grammar constructor to create a Grammar instance from a GrammarBuilder or SrgsDocument object, or from a file or a Stream that contains a description of a grammar in a supported format. Supported formats include the following:

Grammar constructors that accept XML-format grammar files in their arguments compile the XML grammars to a binary format to optimize them for loading and consumption by a speech recognition engine. You can reduce the amount of time required to construct a Grammar object from an XML-format grammar by compiling the grammar in advance, using one of the Compile methods.

A speech recognition grammar can define a root rule. To create a Grammar object that specifies which rule to use as its root rule, use a constructor that accepts the ruleName parameter.

To create a Grammar object that specifies a base URI to resolve relative rule references, use a constructor that takes the baseUri parameter.

Grammar()

Source:
Grammar.cs
Source:
Grammar.cs
Source:
Grammar.cs

Initializes a new instance of the Grammar class.

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

Applies to

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

Source:
Grammar.cs
Source:
Grammar.cs
Source:
Grammar.cs

Initializes a new instance of the Grammar class from an instance of SrgsDocument, and specifies the name of a rule to be the entry point to the grammar and a base URI to resolve relative references.

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

Parameters

srgsDocument
SrgsDocument

An instance of SrgsDocument that contains the constraints for the speech recognition grammar.

ruleName
String

The identifier of the rule to use as the entry point of the speech recognition grammar, or null to use the default root rule of the grammar description.

baseUri
Uri

The base URI to use to resolve any relative rule reference in the grammar description, or null.

parameters
Object[]

Parameters to be passed to the initialization handler specified by the OnInit property for the entry point or the root rule of the Grammar to be created. This parameter may be null.

Exceptions

  • Any of the parameters contain an invalid value.

  • The SrgsDocument specified by srgsDocument does not contain the rule specified in ruleName.

  • The contents of the array parameters do not match the arguments of any of the rule's initialization handlers.

  • The grammar has a relative rule reference that cannot be resolved by the default base Uri rule for grammars or the URI supplied by baseUri.

Remarks

Parameters for an initialization handler may also be specified.

Applies to

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

Source:
Grammar.cs
Source:
Grammar.cs
Source:
Grammar.cs

Initializes a new instance of the Grammar class a Stream and specifies a root rule and a base URI to resolve relative references.

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

Parameters

stream
Stream

A Stream connected to an input/output object (including files, VisualStudio Resources, and DLLs) that contains a grammar specification.

ruleName
String

The identifier of the rule to use as the entry point of the speech recognition grammar, or null to use the default root rule of the grammar description.

baseUri
Uri

The base URI to use to resolve any relative rule reference in the grammar description, or null.

parameters
Object[]

Parameters to be passed to the initialization handler specified by the OnInit property for the entry point or the root rule of the Grammar to be created. This parameter may be null.

Exceptions

  • Any of the parameters contain an invalid value.

  • The stream is connected to a grammar that does not contain the rule specified by ruleName.

  • The contents of the array parameters do not match the arguments of any of the rule's initialization handlers.

  • The grammar contains a relative rule reference that cannot be resolved by the default base Uri rule for grammars or the URI supplied by baseUri.

Remarks

Parameters for an initialization handler may also be specified.

Applies to

Grammar(SrgsDocument, String, Uri)

Source:
Grammar.cs
Source:
Grammar.cs
Source:
Grammar.cs

Initializes a new instance of the Grammar class from an SrgsDocument object, specifies a root rule, and defines a base Uniform Resource Identifier (URI) to resolve relative rule references.

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)

Parameters

srgsDocument
SrgsDocument

The constraints for the speech recognition grammar.

ruleName
String

The identifier of the rule to use as the entry point of the speech recognition grammar, or null to use the default root rule of the SrgsDocument.

baseUri
Uri

The base URI to use to resolve any relative rule reference in the SrgsDocument, or null.

Exceptions

ruleName cannot be resolved or is not public, or ruleName is null and srgsDocument does not contain a root rule.

srgsDocument is null.

srgsDocument contains a rule reference that cannot be resolved.

Examples

The following example creates a speech recognition grammar in an SrgsDocument that contains a relative rule reference to the cities.xml file, and specifies a URI to use to resolve the rule reference. The content of the cities.xml file appears in the XML example that follows the C# example.

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>  

Remarks

This constructor does not pass any parameters to the initialization handler, and the SrgsDocument should not contain an initialization handler that requires arguments.

This constructor does not validate baseUri. However, the LoadGrammar method of a SpeechRecognitionEngine or SpeechRecognizer object throws an exception if it cannot resolve all of the rule references in the grammar description. If baseUri is not null, the LoadGrammar method uses the URI to resolve any rule references that it cannot otherwise resolve. If baseUri represents a file, then the LoadGrammar method uses both the designated file and the file's directory when it attempts to resolve relative rule references.

See also

Applies to

Grammar(SrgsDocument, String, Object[])

Source:
Grammar.cs
Source:
Grammar.cs
Source:
Grammar.cs

Initializes a new instance of the Grammar class from an instance of SrgsDocument, and specifies the name of a rule to be the entry point to the grammar.

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

Parameters

srgsDocument
SrgsDocument

An instance of SrgsDocument that contains the constraints for the speech recognition grammar.

ruleName
String

The identifier of the rule to use as the entry point of the speech recognition grammar, or null to use the default root rule of the grammar description.

parameters
Object[]

Parameters to be passed to the initialization handler specified by the OnInit property for the entry point or the root rule of the Grammar to be created. This parameter may be null.

Exceptions

  • Any of the parameters contain an invalid value.

  • The SrgsDocument specified by srgsDocument does not contain the rule specified by ruleName.

  • The contents of the array parameters do not match the arguments of any of the rule's initialization handlers.

Remarks

Parameters for an initialization handler may also be specified.

Applies to

Grammar(Stream, String, Uri)

Source:
Grammar.cs
Source:
Grammar.cs
Source:
Grammar.cs

Initializes a new instance of the Grammar class from a stream, specifies a root rule, and defines a base Uniform Resource Identifier (URI) to resolve relative rule references.

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)

Parameters

stream
Stream

A stream that describes a speech recognition grammar in a supported format.

ruleName
String

The identifier of the rule to use as the entry point of the speech recognition grammar, or null to use the default root rule of the grammar description.

baseUri
Uri

The base URI to use to resolve any relative rule reference in the grammar description, or null.

Exceptions

ruleName cannot be resolved or is not public, or ruleName is null and the grammar description does not define a root rule.

stream is null.

The stream does not contain a valid description or describes a grammar that contains a rule reference that cannot be resolved.

Examples

The following example loads a local SRGS file (shuttle.xml) from a file stream. The file contains a relative rule reference to a rule in the cities.xml file, and specifies a base URI to use to resolve the rule reference. The content of the shuttle.xml and cities.xml files appears in the XML examples that follow the C# example.

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>  

Remarks

This constructor does not pass any parameters to the initialization handler, and the description should not define an initialization handler that requires arguments.

This constructor can create a Grammar instance from the following formats:

This constructor compiles XML-format grammar files to a binary format to optimize them for loading and consumption by a speech recognition engine. You can reduce the amount of time required to construct a Grammar object from an XML-format grammar by compiling the grammar in advance, using one of the Compile methods.

This constructor does not validate baseUri. However, the LoadGrammar method of a SpeechRecognitionEngine or SpeechRecognizer object throws an exception if it cannot resolve all of the rule references in the grammar description. If baseUri is not null, the LoadGrammar method uses the URI to resolve any rule references that it cannot otherwise resolve. If baseUri represents a file, then the LoadGrammar uses both the designated file and the file's directory when it attempts to resolve relative rule references.

See also

Applies to

Grammar(Stream, String, Object[])

Source:
Grammar.cs
Source:
Grammar.cs
Source:
Grammar.cs

Initializes a new instance of the Grammar class from a Stream and specifies a root rule.

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

Parameters

stream
Stream

A Stream connected to an input/output object (including files, VisualStudio Resources, and DLLs) that contains a grammar specification.

ruleName
String

The identifier of the rule to use as the entry point of the speech recognition grammar, or null to use the default root rule of the grammar description.

parameters
Object[]

Parameters to be passed to the initialization handler specified by the OnInit property for the entry point or the root rule of the Grammar to be created. This parameter may be null.

Exceptions

stream is connected to a grammar that:

  • Does not contain the rule specified in ruleName

  • Requires initialization parameters different from those specified in parameters

  • Contains a relative rule reference that cannot be resolved by the default base Uri rule for grammars.

Remarks

Parameters for an initialization handler may also be specified.

Applies to

Grammar(String, String, Object[])

Source:
Grammar.cs
Source:
Grammar.cs
Source:
Grammar.cs

Initializes a new instance of the Grammar class from a file that contains a grammar definition, and specifies the name of a rule to be the entry point to the 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())

Parameters

path
String

The path to a file, including DLLs, that contains a grammar specification.

ruleName
String

The identifier of the rule to use as the entry point of the speech recognition grammar, or null to use the default root rule of the grammar description.

parameters
Object[]

Parameters to be passed to the initialization handler specified by the OnInit property for the entry point or the root rule of the Grammar to be created. This parameter may be null.

Exceptions

  • Any of the parameters contain an invalid value.

  • The file specified by path does not contain a valid grammar or the rule specified in ruleName.

  • The contents of the array parameters do not match the arguments of any of the rule's initialization handlers.

  • The grammar has a relative rule reference that cannot be resolved by the default base Uri rule for grammars.

Remarks

Parameters for an initialization handler may also be specified.

Applies to

Grammar(SrgsDocument, String)

Source:
Grammar.cs
Source:
Grammar.cs
Source:
Grammar.cs

Initializes a new instance of the Grammar class from an SrgsDocument object and specifies a root rule.

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)

Parameters

srgsDocument
SrgsDocument

The constraints for the speech recognition grammar.

ruleName
String

The identifier of the rule to use as the entry point of the speech recognition grammar, or null to use the default root rule of the SrgsDocument.

Exceptions

ruleName cannot be resolved or is not public, or ruleName is null and srgsDocument does not contain a root rule.

srgsDocument is null.

srgsDocument contains a rule reference that cannot be resolved.

Examples

The following example creates a speech recognition grammar in an SrgsDocument instance and specifies a rule to use as the root rule of the grammar. The example constructs a Grammar object from the SrgsDocument instance and loads it into the speech recognition engine.

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

Remarks

This constructor does not pass any parameters to the initialization handler, and the SrgsDocument should not contain an initialization handler that requires arguments.

To create a Grammar object from a SrgsDocument and specify a base URI to use to resolve relative rule references, use the Grammar constructor.

See also

Applies to

Grammar(Stream, String)

Source:
Grammar.cs
Source:
Grammar.cs
Source:
Grammar.cs

Initializes a new instance of the Grammar class from a Stream and specifies a root rule.

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)

Parameters

stream
Stream

A stream that describes a speech recognition grammar in a supported format.

ruleName
String

The identifier of the rule to use as the entry point of the speech recognition grammar, or null to use the default root rule of the grammar description.

Exceptions

ruleName cannot be resolved or is not public, or ruleName is null and the grammar description does not define a root rule.

stream is null.

The stream does not contain a valid description or describes a grammar that contains a rule reference that cannot be resolved.

Examples

The following example loads a local SRGS file (cities.xml) from a file stream and specifies a rule to use as the root of the grammar. The content of the cities.xml file appears in the XML example that follows the C# example.

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

Remarks

This constructor does not pass any parameters to the initialization handler, and the description should not define an initialization handler that requires arguments.

This constructor can create a Grammar instance from the following formats:

This constructor compiles XML-format grammar files to a binary format to optimize them for loading and consumption by a speech recognition engine. You can reduce the amount of time required to construct a Grammar object from an XML-format grammar by compiling the grammar in advance, using one of the Compile methods.

To create a Grammar from a stream and specify a base URI to use to resolve relative rule references, use the Grammar constructor.

See also

Applies to

Grammar(String)

Source:
Grammar.cs
Source:
Grammar.cs
Source:
Grammar.cs

Initializes a new instance of the Grammar class from a file.

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)

Parameters

path
String

The path of the file that describes a speech recognition grammar in a supported format.

Exceptions

path contains the empty string (""), or the file describes a grammar that does not contain a root rule.

path is null.

The file does not contain a valid description, or describes a grammar that contains a rule reference that cannot be resolved.

Examples

The following example loads a speech recognition grammar from a local SRGS file to build a Grammar object. The content of the cities.xml file appears in the XML example that follows the C# example.

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

Remarks

This constructor does not pass any parameters to the initialization handler, and the description should not define an initialization handler that requires arguments.

This constructor can create a Grammar instance from the following formats:

This constructor compiles XML-format grammar files to a binary format to optimize them for loading and consumption by a speech recognition engine. You can reduce the amount of time required to construct a Grammar object from an XML-format grammar by compiling the grammar in advance, using one of the Compile methods.

An SRGS grammar can define a root rule. To create a Grammar object from a string and specify a root rule, use the Grammar constructor.

To create a Grammar object that specifies a base URI to use to resolve relative rule references, open the file in a file stream and use the Grammar.Grammar constructor.

See also

Applies to

Grammar(SrgsDocument)

Source:
Grammar.cs
Source:
Grammar.cs
Source:
Grammar.cs

Initializes a new instance of the Grammar class from an SrgsDocument object.

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)

Parameters

srgsDocument
SrgsDocument

The constraints for the speech recognition grammar.

Exceptions

srgsDocument does not contain a root rule.

srgsDocument is null.

srgsDocument contains a rule reference that cannot be resolved.

Examples

The following example creates a speech recognition grammar in an SrgsDocument instance, which is then used to construct a Grammar object.

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

Remarks

This constructor does not pass any parameters to the initialization handler, and the SrgsDocument should not contain an initialization handler that requires arguments.

A SrgsDocument can have a root rule. To create a Grammar object that specifies a root rule, use the Grammar or Grammar constructor.

To create a speech recognition Grammar from an SrgsDocument and specify a base URI to use to resolve relative rule references, use the Grammar constructor.

See also

Applies to

Grammar(String, String)

Source:
Grammar.cs
Source:
Grammar.cs
Source:
Grammar.cs

Initializes a new instance of the Grammar class from a file and specifies a root rule.

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)

Parameters

path
String

The path of the file that describes a speech recognition grammar in a supported format.

ruleName
String

The identifier of the rule to use as the entry point of the speech recognition grammar, or null to use the default root rule of the grammar description.

Exceptions

ruleName cannot be resolved or is not public, path is the empty string (""), or ruleName is null and the grammar description does not define a root rule.

path is null.

The file does not contain a valid description or describes a grammar that contains a rule reference that cannot be resolved.

Examples

The following example loads a local SRGS file (cities.xml) from a file and specifies a rule to use as the root of the grammar. The content of the cities.xml file appears in the XML example that follows the C# example.

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

Remarks

This constructor does not pass any parameters to the initialization handler, and the description should not define an initialization handler that requires arguments.

This constructor can create a Grammar instance from the following formats:

This constructor compiles XML-format grammar files to a binary format to optimize them for loading and consumption by a speech recognition engine. You can reduce the amount of time required to construct a Grammar object from an XML-format grammar by compiling the grammar in advance, using one of the Compile methods.

To create a Grammar that specifies a base URI to use to resolve relative rule references, open a file stream for the file and use the Grammar constructor.

See also

Applies to

Grammar(GrammarBuilder)

Source:
Grammar.cs
Source:
Grammar.cs
Source:
Grammar.cs

Initializes a new instance of the Grammar class from a GrammarBuilder object.

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)

Parameters

builder
GrammarBuilder

An instance of GrammarBuilder that contains the constraints for the speech recognition grammar.

Examples

The following example creates a speech recognition grammar using Choices and GrammarBuilder objects. The Grammar constructor creates a Grammar object from the GrammarBuilder object.

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

See also

Applies to

Grammar(Stream)

Source:
Grammar.cs
Source:
Grammar.cs
Source:
Grammar.cs

Initializes a new instance of the Grammar class from a 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)

Parameters

stream
Stream

A stream that describes a speech recognition grammar in a supported format.

Exceptions

stream describes a grammar that does not contain a root rule.

stream is null.

The stream does not contain a valid description of a grammar, or describes a grammar that contains a rule reference that cannot be resolved.

Examples

The following example creates a speech recognition grammar from a local SRGS file (cities.xml) using a file stream. The content of the cities.xml file appears following C# example.

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

Remarks

This constructor does not pass any parameters to the initialization handler, and the description should not define an initialization handler that requires arguments.

This constructor can create a Grammar instance from the following formats:

This constructor compiles XML-format grammar files to a binary format to optimize them for loading and consumption by a speech recognition engine. You can reduce the amount of time required to construct a Grammar object from an XML-format grammar by compiling the grammar in advance, using one of the Compile methods.

An SRGS grammar can define a root rule. To create a Grammar object from a stream and specify a root rule, use the Grammar or Grammar constructor.

To create a Grammar object from a stream and specify a base URI to use to resolve relative rule references, use the Grammar constructor.

See also

Applies to