Grammar Constructor (Stream)

Microsoft Speech Platform SDK 11

Collapse imageExpand ImageCopy imageCopyHover image

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

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

Syntax

Visual Basic (Declaration)
Public Sub New ( _
	stream As Stream _
)
Visual Basic (Usage)
Dim stream As Stream

Dim instance As New Grammar(stream)
C#
public Grammar(
	Stream stream
)

Parameters

stream
Type: System.IO..::..Stream

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

Exceptions

ExceptionCondition
ArgumentException

Generated for invalid stream values.

Generated if the stream is connected to a grammar which has no root rule defined, requires initialization parameters, or has a relative rule reference not resolvable by the default base System.Uri rule for grammars.

Remarks

The stream argument:

  • Can never be nullNothingnullptrunita null reference (Nothing in Visual Basic) and must be readable.

  • In general, the object a stream is connected to must be a grammar that conforms to the W3C Speech Recognition Grammar Specification (SRGS) Version 1.0.

    However:

    • If the input stream is connected to a Visual Studio Resource, the Resource can only contain grammars files in binary format with the .cfg extension.

    • If the input stream is connected to a DLL, that DLL must contain instances of Grammar.

Any object connected to stream must contain a grammar that defines a root rule. To create a Grammar object from a stream and specify a root rule, use the Grammar(Stream, String) or Grammar(Stream, String, Uri) constructor.

As there is no Base URI specified, any rule references must:

  • Use absolute URIs.

  • Target rules within the grammar being loaded.

  • Use any paths defined in the grammar object being loaded. If the stream was created from a file, this typically includes the directory where that file was located.

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

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.

C# Copy imageCopy Code
// 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 Copy imageCopy Code
<?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>

See Also