Grammar Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
A runtime object that references a speech recognition grammar, which an application can use to define the constraints for speech recognition.
public ref class Grammar
public class Grammar
type Grammar = class
Public Class Grammar
- Inheritance
-
Grammar
- Derived
Examples
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>
Remarks
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, 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:
XML-format files that conform to the W3C Speech Recognition Grammar Specification (SRGS) Version 1.0
Grammars that have been compiled to a binary file with a .cfg file extension
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
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.
Constructors
Grammar() |
Initializes a new instance of the Grammar class. |
Grammar(GrammarBuilder) |
Initializes a new instance of the Grammar class from a GrammarBuilder object. |
Grammar(SrgsDocument) |
Initializes a new instance of the Grammar class from an SrgsDocument object. |
Grammar(SrgsDocument, String) |
Initializes a new instance of the Grammar class from an SrgsDocument object and specifies a root rule. |
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. |
Grammar(SrgsDocument, String, Uri) |
Initializes a new instance of the Grammar class from an SrgsDocument object, specifies a root rule, and defines a base Uniform Resource Identifier (URI) to resolve relative rule references. |
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. |
Grammar(Stream) |
Initializes a new instance of the Grammar class from a Stream. |
Grammar(Stream, String) |
Initializes a new instance of the Grammar class from a Stream and specifies a root rule. |
Grammar(Stream, String, Object[]) |
Initializes a new instance of the Grammar class from a Stream and specifies a root rule. |
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. |
Grammar(Stream, String, Uri, Object[]) |
Initializes a new instance of the Grammar class a Stream and specifies a root rule and a base URI to resolve relative references. |
Grammar(String) |
Initializes a new instance of the Grammar class from a file. |
Grammar(String, String) |
Initializes a new instance of the Grammar class from a file and specifies a root rule. |
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. |
Properties
Enabled |
Gets or sets a value that controls whether a Grammar can be used by a speech recognizer to perform recognition. |
IsStg |
Gets whether a grammar is strongly typed. |
Loaded |
Gets whether a Grammar has been loaded by a speech recognizer. |
Name |
Gets or sets the name of a Grammar object. |
Priority |
Gets or sets the priority value of a Grammar object. |
ResourceName |
Gets or sets a value with the name of a binary resource that was used to load the current Grammar. |
RuleName |
Gets the name of the root rule or entry point of a Grammar object. |
Weight |
Gets or sets the weight value of a Grammar object. |
Methods
Equals(Object) |
Determines whether the specified object is equal to the current object. (Inherited from Object) |
GetHashCode() |
Serves as the default hash function. (Inherited from Object) |
GetType() |
Gets the Type of the current instance. (Inherited from Object) |
LoadLocalizedGrammarFromType(Type, Object[]) |
The |
MemberwiseClone() |
Creates a shallow copy of the current Object. (Inherited from Object) |
StgInit(Object[]) |
The |
ToString() |
Returns a string that represents the current object. (Inherited from Object) |
Events
SpeechRecognized |
Raised when a speech recognizer performs recognition using the Grammar object. |