Grammar Class

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)

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

public class Grammar

NameDescription
System_CAPS_protmethodGrammar()

Initializes a new instance of the Grammar class

System_CAPS_pubmethodGrammar(GrammarBuilder)

Initializes a new instance of the Grammar class from a GrammarBuilder object.

System_CAPS_pubmethodGrammar(SrgsDocument)

Initializes a new instance of a Grammar class from an SrgsDocument object.

System_CAPS_pubmethodGrammar(SrgsDocument, String)

Initializes a new instance of a Grammar class from an SrgsDocument object and specifies a root rule.

System_CAPS_pubmethodGrammar(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.

System_CAPS_pubmethodGrammar(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.

System_CAPS_pubmethodGrammar(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.

System_CAPS_pubmethodGrammar(Stream)

Initializes a new instance of the Grammar class from a Stream.

System_CAPS_pubmethodGrammar(Stream, String)

Initializes a new instance of the Grammar class from a Stream and specifies a root rule.

System_CAPS_pubmethodGrammar(Stream, String, Object[])

Initializes a new instance of the Grammar class from a Stream and specifies a root rule.

System_CAPS_pubmethodGrammar(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.

System_CAPS_pubmethodGrammar(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.

System_CAPS_pubmethodGrammar(String)

Initializes a new instance of the Grammar class from a file.

System_CAPS_pubmethodGrammar(String, String)

Initializes a new instance of the Grammar class from a file and specifies a root rule.

System_CAPS_pubmethodGrammar(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..

NameDescription
System_CAPS_pubpropertyEnabled

Gets or sets a value that controls whether a Grammar can be used by a speech recognizer to perform recognition.

System_CAPS_protpropertyIsStg

Gets whether a grammar is strongly typed.

System_CAPS_pubpropertyLoaded

Gets whether a Grammar has been loaded by a speech recognizer.

System_CAPS_pubpropertyName

Gets or sets the name of a Grammar object.

System_CAPS_pubpropertyPriority

Gets or sets the priority value of a Grammar object.

System_CAPS_protpropertyResourceName

Gets or sets a value with the name of a binary resource that was used to load the current Grammar.

System_CAPS_pubpropertyRuleName

Gets the name of the root rule or entry point of a Grammar object.

System_CAPS_pubpropertyWeight

Gets or sets the weight value of a Grammar object.

NameDescription
System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodSystem_CAPS_staticLoadLocalizedGrammarFromType(Type, Object[])

The LoadLocalizedGrammarFromType method returns a localized instance of a Grammar object derived from Type.

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethodStgInit(Object[])

The StgInit method initializes a strongly-typed grammar.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubeventSpeechRecognized

Raised when a speech recognizer performs recognition using the Grammar object.

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.

System_CAPS_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
Available since 3.0

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

Return to top
Show:
© 2016 Microsoft