Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Grammar Class

A run-time object that references a speech recognition grammar, which an application can use to define the constraints for speech recognition.

System.Object
  Microsoft.Speech.Recognition.Grammar

Namespace:  Microsoft.Speech.Recognition
Assembly:  Microsoft.Speech (in Microsoft.Speech.dll)
public class Grammar

A speech recognition grammar is a set of rules or constraints that define what a speech recognition engine can recognize as meaningful input. For more information about creating and using speech recognition grammars, see Speech Recognition (Microsoft.Speech), Create Grammars Using GrammarBuilder (Microsoft.Speech), Create Grammars Using SrgsGrammar (Microsoft.Speech), and Create Grammars Using SRGS XML (Microsoft.Speech).

After you author a grammar, you must build it into a Grammar object that a SpeechRecognitionEngine instance can load and that your application can use at run time to manage speech recognition. You can use a Grammar constructor to create a Grammar instance from a GrammarBuilder or a SrgsDocument object, or from a file or a Stream that contains a description of a grammar in a supported format. Supported formats include the following:

Grammar constructors that accept XML-format grammar files in their arguments compile the XML grammars to a binary format to optimize them for loading and consumption by a speech recognition engine. You can reduce the amount of time required to construct a Grammar object from an XML-format grammar by compiling the grammar in advance, using one of the Compile() methods.

An application's speech recognition engine, as managed by a SpeechRecognitionEngine object, can load multiple speech recognition grammars. The application can independently enable or disable individual grammars by setting the Enabled property, and modify recognition behavior through Grammar properties, such as the Priority and Weight properties.

The grammar’s SpeechRecognized event is raised when input matches a path through the grammar.

NoteNote

It is a best practice to verify the safety of any URI or DLL used to build a Grammar object.

The Microsoft Speech Platform SDK 11 provides security for applications constructing a Grammar instance from a DLL or from a grammar that supports scripting.

Scripts in Grammar objects are always run as if downloaded from a web page in the Internet Zone. The Common Language Runtime (CLR) isolates any DLL loaded to obtain a grammar definition.

The following example generates a file dialog to open a grammar as a stream, selecting a specific grammar rule, and then loading that grammar into an instance of a recognition engine.


private void _grammarAddButton_Click(object sender, EventArgs eventArgs) 
{
  // Create the Open Grammar dialog box.
  OpenFileDialog dialog = new OpenFileDialog();
  dialog.Filter = "SRGS files {*.xml;*.srgs}|*.xml;*.srgs|*.cfg|All files|*.*";
  dialog.Title = "Open Grammar";
  if (dialog.ShowDialog() == DialogResult.OK) 
  {
    try 
    {
      Uri baseURI = null;
      baseURI = new Uri("http://treyresearch.net/SRGS/");
      Stream stream = dialog.OpenFile();
      Uri uriTarget = new Uri(dialog.FileName);
      Grammar grammar = new Grammar(stream, "SetColorRule", baseURI, null);
      //;
      grammar.Name = String.Format("{0} ({1})",
                   grammar.RuleName, dialog.FileName);
      grammar.Enabled = true;
      _recognizer.LoadGrammar(grammar);
      
    } 
    catch (Exception exp) 
    {
      // Catch all exceptions while loading the grammar file.
      MessageBox.Show(String.Format("{0} failed to load.\nError Message:\n{1}",
                    dialog.FileName, exp.Message));
      return;
    }
  }
}
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.