Grammar Constructor (SrgsDocument, String, Uri)

Microsoft Speech Platform SDK 11

Collapse imageExpand ImageCopy imageCopyHover image

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:  Microsoft.Speech.Recognition
Assembly:  Microsoft.Speech (in Microsoft.Speech.dll)

Syntax

Visual Basic (Declaration)
Public Sub New ( _
	srgsDocument As SrgsDocument, _
	ruleName As String, _
	baseUri As Uri _
)
Visual Basic (Usage)
Dim srgsDocument As SrgsDocument
Dim ruleName As String
Dim baseUri As Uri

Dim instance As New Grammar(srgsDocument, _
	ruleName, baseUri)
C#
public Grammar(
	SrgsDocument srgsDocument,
	string ruleName,
	Uri baseUri
)

Parameters

srgsDocument
Type: Microsoft.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 nullNothingnullptrunita null reference (Nothing in Visual Basic) to use the default root rule of the SrgsDocument.

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

baseUri
Type: System..::..Uri

[System.Uri] object defining base path that any relative rule references, for example to other grammars, within the grammar are resolved against.

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

Exceptions

ExceptionCondition
ArgumentException

Generated for invalid srgsDocuments,ruleName, or baseUri values.

Generated if the SrgsDocument specified by srgsDocument does not contain the rule ruleName, or has a relative rule reference not resolvable by the default base System.Uri rule for grammars or the Uri supplied by baseUri .

Remarks

The srgsDocument argument

  • Must never be nullNothingnullptrunita null reference (Nothing in Visual Basic).

  • Must contain the rule specified by ruleName, if ruleName is not nullNothingnullptrunita null reference (Nothing in Visual Basic), and a root rule if ruleName is nullNothingnullptrunita null reference (Nothing in Visual Basic).

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.

The baseUri argument:

  • The value of baseUri should be an absolute URI, and can be a path or a file.

  • If the value of baseUri is a path, be sure to append a trailing slash.

  • The URI provided by baseUri is not validated when the Grammar object is constructed.

    Instead, if the URI is invalid or any relative references are inaccessible, the SpeechRecognitionEngine generates and exception when it loads the grammar.

    The recognition engine performs the following checks on Grammar objects it is loading:

    • Resolves all references with absolute URIs.

    • Checks the XML of Grammar being loaded for correct syntax.

    • Resolves references used to create the instance of srgsDocument from which the Grammar was created. If the srgsDocument has been created from a file, this typically includes the directory where that file was located.

    • Resolves references with the value of baseUri if it is non-null.

Examples

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.

C# Copy imageCopy Code
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 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