Share via


Note

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

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:  Microsoft.Speech.Recognition
Assembly:  Microsoft.Speech (in Microsoft.Speech.dll)

Syntax

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

Dim instance As New Grammar(stream, _
    ruleName, baseUri)
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 a null reference (Nothing in Visual Basic) 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 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 a stream is connected to 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.

The baseUri argument:

  • The value of baseUri should be an absolute URI, and can be a path or a file.

  • If the value of baseUri is a path, be sure to append a trailing slash.

  • The URI provided by baseUri is not validated when the Grammar object is constructed.

    Instead, if the URI is invalid or any relative references are inaccessible, the SpeechRecognitionEngine generates and exception when it loads the grammar.

    The recognition engine performs the following checks on Grammar objects it is loading:

    • Resolves all references with absolute URIs.

    • Checks the XML of Grammar being loaded for correct syntax.

    • Resolves references with the value of baseUri if it is non-null.

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>

See Also

Reference

Grammar Class

Grammar Members

Grammar Overload

Microsoft.Speech.Recognition Namespace

Microsoft.Speech.Recognition.SrgsGrammar

SpeechRecognitionEngine

Grammar

Other Resources

Speech Recognition Grammar Specification Version 1.0