Compartilhar via


Note

Please see Azure Cognitive Services for Speech documentation for the latest supported speech solutions.

Grammar Constructor (SrgsDocument)

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

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

Syntax

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

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

Parameters

Exceptions

Exception Condition
ArgumentException

srgsDocument does not contain a root rule.

ArgumentNullException

srgsDocument is a null reference (Nothing in Visual Basic).

FormatException

srgsDocument contains a rule reference that cannot be resolved.

Remarks

The srgsDocument argument

  • Must never be a 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.

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

Reference

Grammar Class

Grammar Members

Grammar Overload

Microsoft.Speech.Recognition Namespace

Microsoft.Speech.Recognition.SrgsGrammar

SrgsDocument

SpeechRecognitionEngine

Other Resources

Speech Recognition Grammar Specification Version 1.0