Share via


Grammar Constructor (String)

Constructs a new instance of Grammar from file containing a grammar specification.

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

Syntax

'Declaration

Parameters

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

Exceptions

Exception type Condition
ArgumentException

Generated for invalid path values.

Generated if file does not contain a valid grammar, if the grammar is valid but does not specify a root rule, 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:

  • Can never be null, or System.String.Empty

  • Cannot be an absolute URI path.

  • The file specified by path can be an ordinary file or a DLL.

    • DLL must contain instances of Grammar.

    • All other files must contain a grammar defined by W3C Speech Recognition Grammar Specification (SRGS) or Context Free Grammar (CFG) syntax.

The grammar contained in the file specified by path must contain a root rule, as the constructor does not allow the specification of specify a particular rule to load.

No parameters are supplied to this constructor, therefore the loaded rule should either have no initialization handler defined, or the initialization handler should require no arguments.

As there is no Base URI specified, any relative rule references must:

  • Have with absolute URIs.

  • Be contained within the grammar being loaded.

  • Using 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.

Example

The following try/catch block attempts to create a new Grammar from a file (D:\Work\Samples\Inputs\MapLookup.xml).

The file must:

  • A valid grammar with a root rule.

  • Have all references to other rules and grammars accessible in default locations.

  • Have no initialization handler, or a handler that takes not arguments.

XML defining SRGS content that would satisfy these conditions can be found following the source to this try/catch block.

try {
    Grammar grammar = new Grammar("D:\\Work\\Samples\\Inputs\\MapLookup.xml");
    grammar.Enabled = true;
    _recognizer.LoadGrammar(grammar);
    
} catch (System.ArgumentException exp) {
    MessageBox.Show(String.Format("Invalid constructor arguments:\n{0}\n", exp.Message));
} catch (Exception exp) {
    // Catch all other exceptions
    MessageBox.Show(String.Format("Failed to load.\nError Message:\n{0}",
    exp.Message));
}

A sample MapLookup.xml:

<grammar xml:lang="en-us" version="1.0"
xmlns="http://www.w3.org/2001/06/grammar" tag-format="properties-ms/1.0"
xmlns:sapi="http://schemas.microsoft.com/Speech/2002/06/SRGSExtensions"
sapi:debug="True" root="MapLookup">
  <rule id="MapLookup" scope="public" sapi:onParse="OnParseMapLookup" sapi:onInit="OnInit" >
    bis
  </rule>
   <sapi:script sapi:rule="MapLookup">
     <![CDATA[ 
       public object OnParseMapLookup(SemanticValue result, RecognizedWordUnit[] words) {
    Console.WriteLine("OnParse MapLookup: " + _value);
    return _value;
}


public SrgsRule[] OnInit(string state_region, string city, string country) {
    Console.WriteLine(String.Format("Initializer using three arguments: {0},{1},{2}\n",
                                     state_region, city, country));
    _state_region = state_region;
    _city = city;
    _country = country;
    return null;
}

public SrgsRule[] OnInit( string city, string state_region)
{
   Console.WriteLine (String.Format("Initializer using two arguments: {0},{1}\n",
                                    city,state_region));
    _state_region = state_region;
    _city=city;
    _country="United States";
    return null;
}

public SrgsRule[] OnInit(string country) {
    Console.WriteLine(String.Format("Initializer using one argument: {0}\n",
    country));
    _state_region = null;
    _city = null;
    _country = country;
    return null;
}

public SrgsRule[] OnInit() {
    Console.WriteLine(String.Format("Initializer using no arguments\n"));
    _state_region = null;
    _city = null;
    _country = "United States";
    return null;
}

private string _state_region;
private string _city;
private string _state_region1;
private string _city1;
private string _value;
private string _country;
]]>
   </sapi:script>

</grammar>

Platforms

Development Platforms

Windows XP Professional with Service Pack 2 (SP2), Windows Server 2003, Windows Vista Ultimate Edition, Windows Vista Business Edition, Windows Vista Enterprise Edition

Target Platforms

See Also

Reference

Grammar Class
Grammar Members
Microsoft.Speech.Recognition Namespace
Microsoft.Speech.Recognition.SrgsGrammar
SrgsDocument
SpeechRecognizer
SpeechRecognitionEngine
Grammar Constructor

Other Resources

Speech Recognition Grammar Specification