Grammar Constructor (String, String)

Microsoft Speech Platform SDK 11

Collapse imageExpand ImageCopy imageCopyHover image

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

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

Syntax

Visual Basic (Declaration)
Public Sub New ( _
	path As String, _
	ruleName As String _
)
Visual Basic (Usage)
Dim path As String
Dim ruleName As String

Dim instance As New Grammar(path, ruleName)
C#
public Grammar(
	string path,
	string ruleName
)

Parameters

path
Type: System..::..String

Path to a file, including DLLs, containing a grammar specification.

ruleName
Type: System..::..String

The identifier of the rule to use as the entry point of the speech recognition grammar, or nullNothingnullptrunita null reference (Nothing in Visual Basic) to use the default root rule of the grammar description.

This parameter may be nullNothingnullptrunita null reference (Nothing in Visual Basic).

Exceptions

ExceptionCondition
ArgumentException

Thrown when path or ruleName are invalid.

Thrown when the file specified by path does not contain a valid grammar or the rule ruleName, if the root rule initialization handler requires arguments, or if it has a relative rule reference not resolvable by the default base System.Uri rule for grammars.

Remarks

The path argument:

The ruleName argument:

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

  • If ruleName is not nullNothingnullptrunita 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 nullNothingnullptrunita 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.

Examples

The following example loads a local SRGS file (cities.xml) from a file 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.

C# Copy imageCopy Code
// Load a cities grammar from a local file, use a specific
// rule as the root of the grammar, and return the new grammar.
private static Grammar CreateGrammarFromFile2()
{
  Grammar citiesGrammar = new Grammar(@"c:\temp\cities.xml", "Main");
  citiesGrammar.Name = "SRGS File Cities Grammar 2";
  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