Note

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

Grammar Constructor (String)

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

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

Syntax

'Declaration
Public Sub New ( _
    path As String _
)
'Usage
Dim path As String

Dim instance As New Grammar(path)
public Grammar(
    string path
)

Parameters

  • path
    Type: System.String
    The path of the file that describes a speech recognition grammar in a supported format.

Exceptions

Exception Condition
ArgumentException

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

ArgumentNullException

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

FormatException

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

Remarks

The path argument:

The grammar contained in the file specified by path must contain a root rule, as the constructor does not allow the specification of a particular rule to load. To create a Grammar object from a string and specify a root rule, use the Grammar(String, String) 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.

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(Stream, String, Uri) constructor.

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>

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