Share via


Note

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

SrgsDocument Constructor (SrgsRule)

Initializes a new instance of the SrgsDocument class and specifies an SrgsRule object to be the root rule of the grammar.

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

Syntax

'Declaration
Public Sub New ( _
    grammarRootRule As SrgsRule _
)
'Usage
Dim grammarRootRule As SrgsRule

Dim instance As New SrgsDocument(grammarRootRule)
public SrgsDocument(
    SrgsRule grammarRootRule
)

Parameters

Exceptions

Exception Condition
ArgumentNullException

The value of grammarRootRule is a null reference (Nothing in Visual Basic).

Remarks

This constructor adds the specified rule to the SrgsRulesCollection of the SrgsDocument object and sets it as the Root rule for the grammar.

Examples

The following example creates two rules (chooseCities and destCities) for choosing origin and destination cities for a flight. The example initializes the SrgsDocument instance using the chooseCities rule as an argument. The example writes the contents of the rules collection and the name of the root rule to the console.

// Create a rule that contains a list of destination cities.
SrgsRule destCities = new SrgsRule("Destination");
SrgsOneOf toCities = new SrgsOneOf(new string[] { "New York", "Seattle", "Denver" });
destCities.Add(toCities);

// Create a list of origin cities and supporting phrases.
SrgsOneOf fromCities = new SrgsOneOf(new SrgsItem[] { 
  new SrgsItem("Dallas"), new SrgsItem("Miami"), new SrgsItem("Chicago") });
SrgsItem intro = new SrgsItem("I want to fly from");
SrgsItem to = new SrgsItem("to");

// Create the root rule of the grammar, and assemble the components.
SrgsRule chooseCities = new SrgsRule("Trip");
chooseCities.Add(intro);
chooseCities.Add(fromCities);
chooseCities.Add(to);
chooseCities.Add(new SrgsRuleRef(destCities));

// Create the SrgsDocument and specify the root rule to add.
SrgsDocument bookFlight = new SrgsDocument(chooseCities);

// Add the rule for the destination cities to the document's rule collection.
bookFlight.Rules.Add(new SrgsRule[] { destCities });

// Display the contents of the Rules collection and the name of the root rule.
foreach (SrgsRule rule in bookFlight.Rules)
{
  Console.WriteLine("Rule " + rule.Id + " is in the rules collection");
}
Console.WriteLine("Root Rule " + bookFlight.Root.Id);

// Create a Grammar object and load it to the recognizer.
Grammar g = new Grammar(bookFlight);
g.Name = ("City Chooser");
recognizer.LoadGrammarAsync(g);

See Also

Reference

SrgsDocument Class

SrgsDocument Members

SrgsDocument Overload

Microsoft.Speech.Recognition.SrgsGrammar Namespace