Export (0) Print
Expand All

Grammar.Grammar(String, String, Object[]) Constructor

Constructs a new instance of Grammar from file containing a grammar specification; the name of a particular rule to be the grammar’s root rule and parameters for the appropriate initialization handler may also be specified.

Namespace: System.Speech.Recognition
Assembly: System.Speech (in system.speech.dll)

public Grammar (
	string path,
	string ruleName,
	Object[] parameters
)
public Grammar (
	String path, 
	String ruleName, 
	Object[] parameters
)
public function Grammar (
	path : String, 
	ruleName : String, 
	parameters : Object[]
)
Not applicable.

Parameters

path

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

ruleName

Name of a grammar rule used as the entry point or root of the Grammar object to be created.

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

parameters

Parameters to be passed to the appropriate initialization handler specified by the OnInit property on the SrgsRule for the entry point or root rule of the Grammar to be created.

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

Exception typeCondition

ArgumentException

Generated for invalid path,ruleName, or parameters values.

Generated if the file specified by path does not contain a valid grammar or the rule ruleName, if the contents of the array parameters do not match the arguments of any of the rule's initialization handlers, or has a relative rule reference not resolvable by the default base System.Uri rule for grammars.

The path argument:

  • Can never be a null reference (Nothing in Visual Basic), 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

The ruleName argument:

  • May be a null reference (Nothing in Visual Basic) or System.String.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), the grammar contained in the file specified by must specify a root rule. If there is not root rule in the grammar being loaded an exception is generated.

    (For more information on root rules, see the discussion of the Root property on the System.Speech.Recognition.SrgsGrammar.SrgsDocument class.)

The parameters argument:

  • May be null.

  • The constructor will validate the number and type of arguments provided in the parameters argument must match the arguments of one of the initialization handlers of the root rule being loaded.

    A parameters value of a null reference (Nothing in Visual Basic) supports rules without initialization handlers, or those rules whose initializer takes no arguments..

    NoteNote:

    A rule can support a number of initialization handlers, as long as each handler has a unique signature.

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.

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

The file must:

  • Contain the specification of a rule named "MapLookup"

  • Possess one initialization handler for the rule "MapLookup" that takes one string argument, allowing for the specification of a country.

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",
                          @"MapLookup", 
                          new object[] { "Canada" });
    grammar.Name = grammar.RuleName + "From File";

    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>

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions

ADD
Show:
© 2014 Microsoft