2 out of 3 rated this helpful - Rate this topic

Grammar Class

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

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

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

The Grammar type exposes the following members.

  Name Description
Protected method Grammar() Initializes a new instance of the Grammar class
Public method Grammar(GrammarBuilder) Initializes a new instance of the Grammar class from a GrammarBuilder object.
Public method Grammar(SrgsDocument) Initializes a new instance of a Grammar class from an SrgsDocument object.
Public method Grammar(Stream) Initializes a new instance of the Grammar class from a Stream.
Public method Grammar(String) Initializes a new instance of the Grammar class from a file.
Public method Grammar(SrgsDocument, String) Initializes a new instance of a Grammar class from an SrgsDocument object and specifies a root rule.
Public method Grammar(Stream, String) Initializes a new instance of the Grammar class from a Stream and specifies a root rule.
Public method Grammar(String, String) Initializes a new instance of the Grammar class from a file and specifies a root rule.
Public method Grammar(SrgsDocument, String, Object[]) Initializes a new instance of the Grammar class from an instance of SrgsDocument, and specifies the name of a rule to be the entry point to the grammar.
Public method Grammar(SrgsDocument, String, Uri) Initializes a new instance of a Grammar class from an SrgsDocument object, specifies a root rule, and defines a base Uniform Resource Identifier (URI) to resolve relative rule references.
Public method Grammar(Stream, String, Object[]) Initializes a new instance of the Grammar class from a Stream and specifies a root rule.
Public method Grammar(Stream, String, Uri) Initializes a new instance of the Grammar class from a stream, specifies a root rule, and defines a base Uniform Resource Identifier (URI) to resolve relative rule references.
Public method Grammar(String, String, Object[]) Initializes a new instance of the Grammar class from a file that contains a grammar definition, and specifies the name of a rule to be the entry point to the grammar..
Public method Grammar(SrgsDocument, String, Uri, Object[]) Initializes a new instance of the Grammar class from an instance of SrgsDocument, and specifies the name of a rule to be the entry point to the grammar and a base URI to resolve relative references.
Public method Grammar(Stream, String, Uri, Object[]) Initializes a new instance of the Grammar class a Streamand specifies a root rule and a base URI to resolve relative references.
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.
Protected property IsStg Gets whether a grammar is strongly typed.
Public property Loaded Gets whether a Grammar has been loaded by a speech recognizer.
Public property Name Gets or sets the name of a Grammar object.
Public property Priority Gets or sets the priority value of a Grammar object.
Protected property ResourceName Gets or sets a value with the name of a binary resource that was used to load the current Grammar.
Public property RuleName Gets the name of the root rule or entry point of a Grammar object.
Public property Weight Gets or sets the weight value of a Grammar object.
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.)
Public method Static member LoadLocalizedGrammarFromType The LoadLocalizedGrammarFromType method returns a localized instance of a Grammar object derived from Type.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Protected method StgInit The StgInit method initializes a strongly-typed 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.
Top

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, Create Grammars Using XML, Create Grammars Using GrammarBuilder, and Create Grammars Using SrgsGrammar.

After you author a grammar, you must build it into a Grammar object that a speech recognition engine can load and that your application can use at runtime 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 SpeechRecognizer or 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.

Note Note

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

Windows and the Speech platform provide 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 constructs a Grammar object from a speech recognition grammar defined in a XML file (cities.xml). The content of the cities.xml file appears in the following XML example.

// Load a cities grammar from a local file and return the grammar object. 
private static Grammar CreateGrammarFromFile()
{
  Grammar citiesGrammar = new Grammar(@"c:\temp\cities.xml");
  citiesGrammar.Name = "SRGS File Cities Grammar";
  return citiesGrammar;
}


<?xml version="1.0" encoding="UTF-8" ?>
<grammar version="1.0" xml:lang="en-US"
         xmlns="http://www.w3.org/2001/06/grammar"
         tag-format="semantics/1.0" root="Main">
  
  <!-- cities.xml: 
    Defines an SRGS grammar for requesting a flight. This grammar includes
    a Cities rule that lists the cities that can be used for departures
    and destinations. -->
  
  <rule id="Main">
    <item>
      I would like to fly from <ruleref uri="#Cities"/>
      to <ruleref uri="#Cities"/>
    </item>
  </rule>

  <rule id="Cities" scope="public">
    <one-of>
      <item> Seattle </item>
      <item> Los Angeles </item>
      <item> New York </item>
      <item> Miami </item>
    </one-of>
  </rule>
</grammar>

.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