Grammar Class

Provides run time support for obtaining and managing Speech grammar information.

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

Syntax

'Declaration
<DebuggerDisplayAttribute("Grammar: {_content.ToString()}")> _
Public Class Grammar
[DebuggerDisplayAttribute("Grammar: {_content.ToString()}")] 
public class Grammar
[DebuggerDisplayAttribute(L"Grammar: {_content.ToString()}")] 
public ref class Grammar
/** @attribute DebuggerDisplayAttribute("Grammar: {_content.ToString()}") */ 
public class Grammar
DebuggerDisplayAttribute("Grammar: {_content.ToString()}") 
public class Grammar

Remarks

The Microsoft.Speech.Recognition.Grammar class provides run time objects that allow an application to specify a specific combination of words, choices of words, and other speech elements that the Speech platform uses to identify meaningful phrases.

The Grammar object fully supports the W3C Speech Recognition Grammar Specification (SRGS) and Context Free Grammar (CFG) specifications. For more information, see Speech .

Grammars may be precise word phrases, such as "Turn the computer off," or provide choices, semantic lookup tables or wildcards, such as "Change color to" and a look up table of acceptable values.

An application's recognition engine, as managed by instances of SpeechRecognizer or SpeechRecognitionEngine, may create and load one or more instances Grammar, independently enabling or disabling particular grammars instance, and set Grammar properties such as priorities (Priority and weight (Weight)

The grammar a Grammar object manages can be loaded from

SRGS and CFG formats allow the inclusion of multiple grammar rules, so on construction applications can specify which rule to load as the grammar's entry point or root. Support for the inclusion of handler methods (for recognition, initialization, parsing and errors), scripts and executables in a loaded grammar is provided by allowing the specification of parameters during Grammar construction. When constructing a grammar using tags avoid using duplicate key names, as each key/value pair element is stored in a DictionaryEntry object and each pair must have contain a unique key.

Note

For details on how to programmatically construct a grammar containing a script, see discussion of the Script, OnInit, OnParse, OnError, and OnRecognition properties on the Microsoft.Speech.Recognition.SrgsGrammar.SrgsRuleclass, the Language and Script properties on the Microsoft.Speech.Recognition.SrgsGrammar.SrgsDocument class, and the Script property on the Microsoft.Speech.Recognition.SrgsGrammar.SrgsTag class.

Because grammar formats allow for references to other grammar objects as both relative and fully qualified URIs, a base URI can be specified during construction of a Grammar object.

Note

It is a best practice to verify the safety of any URI or DLL used to load a Grammar. Windows and the Speech platform provide security for applications loading a Grammar from a DLL or working with a Grammar that supports scripting. Scripts in Grammar objects are always run as if downloaded from a web page in the InternetZone, and the Common Language Runtime (CLR) isolates any DLL loaded to obtain a Grammar.

Inheritance Hierarchy

System.Object
  Microsoft.Speech.Recognition.Grammar

Example

The example below 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 files|*.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;
    }
  }
}

Thread Safety

All public static (Shared in Visual Basic) members of this type are thread-safe. Instance members are not guaranteed to be thread-safe.

Platforms

Development Platforms

Windows XP Professional with Service Pack 2 (SP2), Windows Server 2003, Windows Vista Ultimate Edition, Windows Vista Business Edition, Windows Vista Enterprise Edition

Target Platforms

See Also

Reference

Grammar Members
Microsoft.Speech.Recognition Namespace
Microsoft.Speech.Recognition.SrgsGrammar
SrgsDocument
SrgsRule
SrgsTag
OnInit
OnParse
OnError
OnRecognition
SrgsRule
Document
GrammarBuilder

Other Resources

Speech Recognition Grammar Specification