.NET Framework Class Library
Grammar Class

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

Namespace:  System.Speech.Recognition
Assembly:  System.Speech (in System.Speech.dll)
Syntax

Visual Basic (Declaration)
Public Class Grammar
Visual Basic (Usage)
Dim instance As Grammar
C#
public class Grammar
Visual C++
public ref class Grammar
JScript
public class Grammar
Remarks

The System.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 SpeechRecognitionGrammarSpecification.

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 [T:System.Speech.Recognition.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.

NoteNote:

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 System.Speech.Recognition.SrgsGrammar..::.SrgsRule[class], the Language and Script properties on the System.Speech.Recognition.SrgsGrammar..::.SrgsDocument class, and the Script()()() property on the 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.

NoteNote:

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 [Internet][Zone], and the Common Language Runtime (CLR) isolates any DLL loaded to obtain a Grammar.

Examples

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://fred.com/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;
    }
  }
}
Inheritance Hierarchy

System..::.Object
  System.Speech.Recognition..::.Grammar
    System.Speech.Recognition..::.DictationGrammar
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0
See Also

Reference

SrgsTag
Document

Other Resources

Tags :


Page view tracker