Compartir a través de


Note

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

Grammar Constructor (String, String)

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

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

Dim instance As New Grammar(path, ruleName)
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 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, path is the empty string (""), or ruleName is a null reference (Nothing in Visual Basic) and the grammar description does not define 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 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.

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.

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