Share via


Note

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

Grammar Authoring Processes (Microsoft.Speech)

You can author grammars for applications based on Microsoft.Speech using any of the following processes:

Create Dynamic Grammars Programmatically

Use the following to create grammars within the code of your speech application.

GrammarBuilder and Choices

The GrammarBuilder and Choices classes in the Microsoft.Speech.Recognition namespace provide methods for programmatically creating grammar definitions that do not rely on or conform to the SRGS specification. This lightweight, programmatic approach to authoring grammars will efficiently accomplish many common tasks for grammar creation. Authoring with GrammarBuilder is best suited to grammars that have a single rule containing lists, or perhaps lists of lists. See Create Grammars Using GrammarBuilder (Microsoft.Speech).

To programmatically build grammars that have multiple rules, or that need to make internal rule references, use the classes of the Microsoft.Speech.Recognition.SrgsGrammar namespace.

SrgsGrammar Namespace

The classes of the Microsoft.Speech.Recognition.SrgsGrammar namespace map closely to the elements and attributes defined by the SRGS specification. Using members of the Microsoft.Speech.Recognition.SrgsGrammar namespace, you can programmatically create SRGS-compliant grammars and even write the results to an XML file. In addition, you can build a grammar in the Microsoft.Speech.Recognition.SrgsGrammar namespace from an XML-format grammar document or from a GrammarBuilder object. See Create Grammars Using SrgsGrammar (Microsoft.Speech).

Create Static Grammars as Files

Use the following to create grammars as static files, external to your speech application's code. Your speech application can load external grammar files at runtime.

SRGS XML Format

The SRGS specification defines the structure of grammars and grammar rules that use XML markup. Using a text editor, you can add XML elements, XML attributes, and plain text to define the content and sequence of the spoken input or dial tones that the grammar will recognize. A typical use for static grammars is to store lists of data that does not change frequently, for example lists of cities or grammars that process phone numbers. See Create Grammars Using SRGS XML (Microsoft.Speech) and SRGS Grammar XML Reference (Microsoft.Speech).

Building Grammar Objects

After creating a grammar using any of the processes described above, you must build the grammar into a Grammar object, using a constructor on the class. A SpeechRecognitionEngine instance will only load grammars as Grammar objects.