1 out of 2 rated this helpful - Rate this topic

DictationGrammar Class

Represents a speech recognition grammar used for free text dictation.

System.Object
  System.Speech.Recognition.Grammar
    System.Speech.Recognition.DictationGrammar

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

The DictationGrammar type exposes the following members.

  Name Description
Public method DictationGrammar() Initializes a new instance of the DictationGrammar class for the default dictation grammar provided by Windows Desktop Speech Technology.
Public method DictationGrammar(String) Initializes a new instance of the DictationGrammar class with a specific dictation grammar.
Top
  Name Description
Public property Enabled Gets or sets a value that controls whether a Grammar can be used by a speech recognizer to perform recognition. (Inherited from Grammar.)
Protected property IsStg Gets whether a grammar is strongly typed. (Inherited from Grammar.)
Public property Loaded Gets whether a Grammar has been loaded by a speech recognizer. (Inherited from Grammar.)
Public property Name Gets or sets the name of a Grammar object. (Inherited from Grammar.)
Public property Priority Gets or sets the priority value of a Grammar object. (Inherited from Grammar.)
Protected property ResourceName Gets or sets a value with the name of a binary resource that was used to load the current Grammar. (Inherited from Grammar.)
Public property RuleName Gets the name of the root rule or entry point of a Grammar object. (Inherited from Grammar.)
Public property Weight Gets or sets the weight value of a Grammar object. (Inherited from Grammar.)
Top
  Name Description
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method SetDictationContext Adds a context to the dictation grammar.
Protected method StgInit The StgInit method initializes a strongly-typed grammar. (Inherited from Grammar.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Top
  Name Description
Public event SpeechRecognized Raised when a speech recognizer performs recognition using the Grammar object. (Inherited from Grammar.)
Top

This class provides applications with a predefined language model that can process spoken user input into text. This class supports both default and custom DictationGrammar objects. For information about selecting a dictation grammar, see the DictationGrammar(String) constructor.

By default, the DictationGrammar language model is context free. It does not make use of specific words or word order to identify and interpret audio input. To add context to the dictation grammar, use the SetDictationContext method.

Note Note

DictationGrammar objects do not support the Priority property. DictationGrammar throws a NotSupportedException if Priority is set.

The following example creates three dictation grammars, adds them to a new SpeechRecognitionEngine object, and returns the new object. The first grammar is the default dictation grammar. The second grammar is the spelling dictation grammar. The third grammar is the default dictation grammar that includes a context phrase. The SetDictationContext method is used to associate the context phrase with the dictation grammar.

 
private SpeechRecognitionEngine LoadDictationGrammars()
{

  // Create a default dictation grammar.
  DictationGrammar defaultDictationGrammar = new DictationGrammar();
  defaultDictationGrammar.Name = "default dictation";
  defaultDictationGrammar.Enabled = true;

  // Create the spelling dictation grammar.
  DictationGrammar spellingDictationGrammar =
    new DictationGrammar("grammar:dictation#spelling");
  spellingDictationGrammar.Name = "spelling dictation";
  spellingDictationGrammar.Enabled = true;

  // Create the question dictation grammar.
  DictationGrammar customDictationGrammar =
    new DictationGrammar("grammar:dictation");
  customDictationGrammar.Name = "question dictation";
  customDictationGrammar.SetDictationContext("How do you", null);
  customDictationGrammar.Enabled = true;

  // Create a SpeechRecognitionEngine object and add the grammars to it.
  SpeechRecognitionEngine recoEngine = new SpeechRecognitionEngine();
  recoEngine.LoadGrammar(defaultDictationGrammar);
  recoEngine.LoadGrammar(spellingDictationGrammar);
  recoEngine.LoadGrammar(customDictationGrammar);

  return recoEngine;
}

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
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)
Community Content Add
Annotations FAQ
SetDictationContext exception
PepsiBot,  add the Grammer to the SpeachRecognizer BEFORE calling SetDictationContext.   That seems to fix the runtime error.
Invalid Operation Exception
I attempted to use this example to make a simple context free grammar.  However, the 3rd part always throws an exception. 

// Create the question dictation grammar.
DictationGrammar customDictationGrammar =
new DictationGrammar("grammar:dictation");
customDictationGrammar.Name = "question dictation";
customDictationGrammar.SetDictationContext("How do you", null); // This always fails with an exception.
customDictationGrammar.Enabled = true;
The exception text reads "Grammar is not loaded into this recognizer."  This error is ambiguous to me.  Can anyone explain why?