Partilhar via


Note

Please see Azure Cognitive Services for Speech documentation for the latest supported speech solutions.

Grammar Constructor (Stream, String)

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

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

Syntax

'Declaration
Public Sub New ( _
    stream As Stream, _
    ruleName As String _
)
'Usage
Dim stream As Stream
Dim ruleName As String

Dim instance As New Grammar(stream, _
    ruleName)
public Grammar(
    Stream stream,
    string ruleName
)

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 a null reference (Nothing in Visual Basic) to use the default root rule of the grammar description.
    This parameter may be a null reference (Nothing in Visual Basic).

Exceptions

Exception Condition
ArgumentException

ruleName cannot be resolved or is not public, or ruleName is a null reference (Nothing in Visual Basic) and the grammar description does not define a root rule.

ArgumentNullException

stream is a null reference (Nothing in Visual Basic).

FormatException

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

Remarks

The stream argument:

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

  • In general, the object connected to a stream must be a grammar file 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 grammar files in binary format with the .cfg extension.

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

The ruleName argument:

  • May be a null reference (Nothing in Visual Basic) or Empty.

  • If ruleName is not a null reference (Nothing in Visual Basic) and the rule specified is not found in the grammar being loaded, an exception is generated.

  • If ruleName is a null reference (Nothing in Visual Basic), and the grammar contained in the file specified does not declare a root rule, an exception is generated.

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

See Also

Reference

Grammar Class

Grammar Members

Grammar Overload

Microsoft.Speech.Recognition Namespace

Microsoft.Speech.Recognition.SrgsGrammar

SrgsDocument

SpeechRecognitionEngine

Grammar

Other Resources

Speech Recognition Grammar Specification Version 1.0