Grammar Constructor (SrgsDocument, String, Uri)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Initializes a new instance of a Grammar class from an SrgsDocument object, specifies a root rule, and defines a base Uniform Resource Identifier (URI) to resolve relative rule references.

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

public Grammar(
	SrgsDocument srgsDocument,
	string ruleName,
	Uri baseUri
)

Parameters

srgsDocument
Type: System.Speech.Recognition.SrgsGrammar.SrgsDocument

The constraints for the speech recognition grammar.

ruleName
Type: System.String

The identifier of the rule to use as the entry point of the speech recognition grammar, or null to use the default root rule of the SrgsDocument.

baseUri
Type: System.Uri

The base URI to use to resolve any relative rule reference in the SrgsDocument, or null.

Exception Condition
ArgumentException

ruleName cannot be resolved or is not public, or ruleName is null and srgsDocument does not contain a root rule.

ArgumentNullException

srgsDocument is null.

FormatException

srgsDocument contains a rule reference that cannot be resolved.

This constructor does not pass any parameters to the initialization handler, and the SrgsDocument should not contain an initialization handler that requires arguments.

This constructor does not validate baseUri. However, the LoadGrammar method of a SpeechRecognitionEngine or SpeechRecognizer object throws an exception if it cannot resolve all of the rule references in the grammar description. If baseUri is not null, the LoadGrammar method uses the URI to resolve any rule references that it cannot otherwise resolve. If baseUri represents a file, then the LoadGrammar method uses both the designated file and the file's directory when it attempts to resolve relative rule references.

The following example creates a speech recognition grammar in an SrgsDocument that contains a relative rule reference to the cities.xml file, and specifies a URI to use to resolve the rule reference. The content of the cities.xml file appears in the XML example that follows the C# example.


private static Grammar CreateSrgsDocumentGrammar3()
{
  // Create the SrgsDocument.
  SrgsDocument document = new SrgsDocument();

  // Create the Main rule and add it to the document.
  SrgsRule mainRule = new SrgsRule("Main");
  mainRule.Scope = SrgsRuleScope.Public;

  SrgsItem item = new SrgsItem("Can I get a shuttle in");

  // Create a relative URI for the cities rule.
  Uri ruleUri = new Uri("cities.xml#Cities", UriKind.Relative);

  item.Add(new SrgsRuleRef(ruleUri));

  mainRule.Add(item);
  document.Rules.Add(mainRule);

  // Set the root rule.
  document.Root = mainRule;

  // Create the grammar.
  Uri baseUri = new Uri(@"file://c:\temp\");
  Grammar citiesGrammar = new Grammar(document, null, baseUri);
  citiesGrammar.Name = "SrgsDocument Cities Grammar 3";

  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>

.NET Framework
Available since 3.0
Return to top
Show: