Grammar Constructor (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.

Namespace:   System.Speech.Recognition
Assembly:  System.Speech (in System.Speech.dll)

public:
Grammar(
	Stream^ stream,
	String^ ruleName,
	Uri^ baseUri
)

Parameters

stream
Type: System.IO::Stream^

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

ruleName
Type: System::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
Type: System::Uri^

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

Exception Condition
ArgumentException

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

ArgumentNullException

stream is null.

FormatException

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

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.

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>

.NET Framework
Available since 3.0
Return to top
Show: