Partilhar via


Grammar Authoring Processes

You can author grammars for applications based on System.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 System.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.

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

SrgsGrammar Namespace

The classes of the System.Speech.Recognition.SrgsGrammar namespace map closely to the elements and attributes defined by the SRGS specification. Using members of the System.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 System.Speech.Recognition.SrgsGrammar namespace from an XML-format grammar document or from a GrammarBuilder object. See Create Grammars Using SrgsGrammar.

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. You can organize the grammar XML elements into lists, strings, and numbers, some of which may be reused in other grammars. See Create Grammars Using XML and SRGS Grammar XML Reference.

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 SpeechRecognizer or SpeechRecognitionEngine instance will only load grammars as Grammar objects.