Grammar Constructor (SrgsDocument^, String^)
Initializes a new instance of a Grammar class from an SrgsDocument object and specifies a root rule.
Assembly: System.Speech (in System.Speech.dll)
Parameters
- srgsDocument
-
Type:
System.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 null to use the default root rule of the SrgsDocument.
| Exception | Condition |
|---|---|
| ArgumentException | ruleName cannot be resolved or is not public, or ruleName is null and srgsDocument does not contain a root rule. |
| ArgumentNullException | srgsDocument is null. |
| FormatException | srgsDocument contains a rule reference that cannot be resolved. |
This constructor does not pass any parameters to the initialization handler, and the SrgsDocument should not contain an initialization handler that requires arguments.
To create a Grammar object from a SrgsDocument and specify a base URI to use to resolve relative rule references, use the Grammar constructor.
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.
using System; using System.Speech.Recognition; using System.Speech.Recognition.SrgsGrammar; 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); // Create the Grammar object and specify which rule to use as the root. Grammar citiesGrammar = new Grammar(document,"Main"); // Load the grammar object to the recognizer. recognizer.LoadGrammarAsync(citiesGrammar); // Attach a handler for the SpeechRecognized event. recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized); // Set the input to the recognizer. recognizer.SetInputToDefaultAudioDevice(); // Start recognition. recognizer.RecognizeAsync(); Console.WriteLine("Starting aynchronous recognition..."); // Keep the console window open. Console.ReadLine(); } } // Handle the SpeechRecognized event. static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) { Console.WriteLine(" Speech recognized: " + e.Result.Text); } } }
Available since 3.0