Compartir a través de


Note

Please see Azure Cognitive Services for Speech documentation for the latest supported speech solutions.

Grammar Class

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

Inheritance Hierarchy

System.Object
  Microsoft.Speech.Recognition.Grammar

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

Syntax

'Declaration
Public Class Grammar
'Usage
Dim instance As Grammar
public class 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 (Microsoft.Speech), Create Grammars Using GrammarBuilder (Microsoft.Speech), Create Grammars Using SrgsGrammar (Microsoft.Speech), and Create Grammars Using SRGS XML (Microsoft.Speech).

After you author a grammar, you must build it into a Grammar object that a SpeechRecognitionEngine instance can load and that your application can use at run time 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 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.

The Microsoft Speech Platform SDK 11 provides 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.

Examples

The following example generates a file dialog to open a grammar as a stream, selecting a specific grammar rule, and then loading that grammar into an instance of a recognition engine.

private void _grammarAddButton_Click(object sender, EventArgs eventArgs) 
{
  // Create the Open Grammar dialog box.
  OpenFileDialog dialog = new OpenFileDialog();
  dialog.Filter = "SRGS files {*.xml;*.srgs}|*.xml;*.srgs|*.cfg|All files|*.*";
  dialog.Title = "Open Grammar";
  if (dialog.ShowDialog() == DialogResult.OK) 
  {
    try 
    {
      Uri baseURI = null;
      baseURI = new Uri("http://treyresearch.net/SRGS/");
      Stream stream = dialog.OpenFile();
      Uri uriTarget = new Uri(dialog.FileName);
      Grammar grammar = new Grammar(stream, "SetColorRule", baseURI, null);
      //;
      grammar.Name = String.Format("{0} ({1})",
                   grammar.RuleName, dialog.FileName);
      grammar.Enabled = true;
      _recognizer.LoadGrammar(grammar);
      
    } 
    catch (Exception exp) 
    {
      // Catch all exceptions while loading the grammar file.
      MessageBox.Show(String.Format("{0} failed to load.\nError Message:\n{1}",
                    dialog.FileName, exp.Message));
      return;
    }
  }
}

Thread Safety

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

See Also

Reference

Grammar Members

Microsoft.Speech.Recognition Namespace

Microsoft.Speech.Recognition.SrgsGrammar

SrgsDocument

SrgsRule

GrammarBuilder

Other Resources

Speech Recognition Grammar Specification