Grammar Constructor (SrgsDocument)

Microsoft Speech Platform SDK 11

Collapse imageExpand ImageCopy imageCopyHover image

Initializes a new instance of the Grammar class from an SrgsDocument object.

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

Syntax

Visual Basic (Declaration)
Public Sub New ( _
	srgsDocument As SrgsDocument _
)
Visual Basic (Usage)
Dim srgsDocument As SrgsDocument

Dim instance As New Grammar(srgsDocument)
C#
public Grammar(
	SrgsDocument srgsDocument
)

Parameters

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

The constraints for the speech recognition grammar.

Exceptions

ExceptionCondition
ArgumentException

Thrown when one or more values in srgsDocument are invalid.

Thrown when the SrgsDocument specified by srgsDocument does not contain a root rule ruleName.

Remarks

The srgsDocument argument

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

  • Must contain a root rule.

To create a Grammar object from a SrgsDocument object and specify a root rule, use the Grammar(SrgsDocument, String) or Grammar(SrgsDocument, String, Uri) constructor.

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.

To create a Grammar object from an SrgsDocument and specify a base URI to use to resolve relative rule references, use the Grammar(SrgsDocument, String, Uri) constructor.

Examples

The following example creates a speech recognition grammar in an SrgsDocument instance, which is then used to construct a Grammar object.

C# Copy imageCopy Code
private static Grammar CreateSrgsDocumentGrammar()
{
  // Create the SrgsDocument.
  SrgsDocument document = new SrgsDocument();

  // Create the Cities rule and add it to the document.
  SrgsRule citiesRule = new SrgsRule("Cities");

  SrgsOneOf cityChoice = new SrgsOneOf();
  cityChoice.Add(new SrgsItem("Seattle"));
  cityChoice.Add(new SrgsItem("Los Angeles"));
  cityChoice.Add(new SrgsItem("New York"));
  cityChoice.Add(new SrgsItem("Miami"));

  citiesRule.Add(cityChoice);
  document.Rules.Add(citiesRule);

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

  SrgsItem item = new SrgsItem("I would like to fly from");
  item.Add(new SrgsRuleRef(citiesRule));
  item.Add(new SrgsText(" to "));
  item.Add(new SrgsRuleRef(citiesRule));

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

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

  // Create the Grammar object.
  Grammar citiesGrammar = new Grammar(document);
  citiesGrammar.Name = "SrgsDocument Cities Grammar";

  return citiesGrammar;
}

See Also