Initializes a new instance of a Grammar class from an SrgsDocument object and specifies a root rule.
Namespace:
Microsoft.Speech.Recognition
Assembly:
Microsoft.Speech (in Microsoft.Speech.dll)
Syntax
Visual Basic (Declaration) |
---|
Public Sub New ( _ srgsDocument As SrgsDocument, _ ruleName As String _ ) |
Visual Basic (Usage) |
---|
Dim srgsDocument As SrgsDocument Dim ruleName As String Dim instance As New Grammar(srgsDocument, _ ruleName) |
C# |
---|
public Grammar( SrgsDocument srgsDocument, string ruleName ) |
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).
Exceptions
Exception | Condition |
---|---|
ArgumentException | Thrown when srgsDocument or ruleName are invalid. Thrown when the SrgsDocument specified by srgsDocument does not contain the rule ruleName, or when ruleName is nullNothingnullptrunita null reference (Nothing in Visual Basic) and srgsDocument does not have a root rule. |
Remarks
The srgsDocument argument must:
Never be nullNothingnullptrunita null reference (Nothing in Visual Basic).
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.
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. If the stream was created from a file, this typically includes the directory where that file was located.
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 and specifies a rule to use as the root rule of the grammar. The example constructs a Grammar object from the SrgsDocument instance and loads it into the speech recognition engine.
C# | Copy Code |
---|---|
using System; using Microsoft.Speech.Recognition; using Microsoft.Speech.Recognition.SrgsGrammar; using System.Xml; namespace SampleRecognition { class Program { static void Main(string[] args) // Initialize an in-process speech recognition engine. { using (SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine()) { // Create the SrgsDocument. SrgsDocument document = new SrgsDocument(); // Create the Cities rule and add it to the document. SrgsRule citiesRule = new SrgsRule("Cities"); citiesRule.Scope = SrgsRuleScope.Public; 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; mainRule.Add(new SrgsItem("I would like to fly from")); mainRule.Add(new SrgsRuleRef(citiesRule)); mainRule.Add(new SrgsItem("to")); mainRule.Add(new SrgsRuleRef(citiesRule)); document.Rules.Add(mainRule); // Set the root rule. document.Root = mainRule; // Create the grammar and specify which rule to use as the root. Grammar citiesGrammar = new Grammar(document, mainRule.Id); citiesGrammar.Name = "SrgsDocument Cities Grammar 2"; // Attach event handlers. recognizer.LoadGrammarCompleted += new EventHandler<LoadGrammarCompletedEventArgs>(recognizer_LoadGrammarCompleted); recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized); recognizer.SpeechHypothesized += new EventHandler<SpeechHypothesizedEventArgs>(recognizer_SpeechHypothesized); // Load the grammar object to the recognizer. recognizer.LoadGrammarAsync(citiesGrammar); // Set the input to the recognizer. recognizer.SetInputToDefaultAudioDevice(); // Start recognition. recognizer.RecognizeAsync(); // Keep the console window open. Console.ReadLine(); } } // Handle the SpeechHypothesized event. static void recognizer_SpeechHypothesized(object sender, SpeechHypothesizedEventArgs e) { Console.WriteLine("Speech hypothesized: " + e.Result.Text); } // Handle the LoadGrammarCompleted event. static void recognizer_LoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs e) { Console.WriteLine("Grammar loaded: " + e.Grammar.Name); } // Handle the SpeechRecognized event. static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) { Console.WriteLine("Speech recognized: " + e.Result.Text); } } } |