Export (0) Print
Expand All

Grammar Class

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

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

[DebuggerDisplayAttribute("Grammar: {(_uri != null ? "uri=" + _uri.ToString () + " " : "") + "rule=" + _ruleName }")]
public class Grammar

The Grammar type exposes the following members.

  NameDescription
Protected methodGrammar()Initializes a new instance of the Grammar class
Public methodGrammar(GrammarBuilder)Initializes a new instance of the Grammar class from a GrammarBuilder object.
Public methodGrammar(SrgsDocument)Initializes a new instance of a Grammar class from an SrgsDocument object.
Public methodGrammar(Stream)Initializes a new instance of the Grammar class from a Stream.
Public methodGrammar(String)Initializes a new instance of the Grammar class from a file.
Public methodGrammar(SrgsDocument, String)Initializes a new instance of a Grammar class from an SrgsDocument object and specifies a root rule.
Public methodGrammar(Stream, String)Initializes a new instance of the Grammar class from a Stream and specifies a root rule.
Public methodGrammar(String, String)Initializes a new instance of the Grammar class from a file and specifies a root rule.
Public methodGrammar(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 methodGrammar(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 methodGrammar(Stream, String, Object[])Initializes a new instance of the Grammar class from a Stream and specifies a root rule.
Public methodGrammar(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 methodGrammar(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 methodGrammar(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 methodGrammar(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

  NameDescription
Public propertyEnabledGets or sets a value that controls whether a Grammar can be used by a speech recognizer to perform recognition.
Protected propertyIsStgGets whether a grammar is strongly typed.
Public propertyLoadedGets whether a Grammar has been loaded by a speech recognizer.
Public propertyNameGets or sets the name of a Grammar object.
Public propertyPriorityGets or sets the priority value of a Grammar object.
Protected propertyResourceNameGets or sets a value with the name of a binary resource that was used to load the current Grammar.
Public propertyRuleNameGets the name of the root rule or entry point of a Grammar object.
Public propertyWeightGets or sets the weight value of a Grammar object.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodStatic memberLoadLocalizedGrammarFromTypeThe LoadLocalizedGrammarFromType method returns a localized instance of a Grammar object derived from Type.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodStgInitThe StgInit method initializes a strongly-typed grammar.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public eventSpeechRecognizedRaised 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.

NoteNote

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.6, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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.
Show:
© 2014 Microsoft