Note

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

GrammarInfo Class

Represents an object that contains static information about a speech recognition grammar. In contrast, a Grammar object contains information about a speech recognition grammar that is loaded into a speech recognizer at run time.

Inheritance Hierarchy

System.Object
  Microsoft.Speech.Recognition.GrammarInfo

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

Syntax

'Declaration
Public Class GrammarInfo _
    Implements IDisposable
'Usage
Dim instance As GrammarInfo
public class GrammarInfo : IDisposable

Remarks

You will typically use the GrammarInfo and GrammarInfoPartsCollection classes to optimize compiled grammars for the speech recognition engine that your application will use in production. Optimizing a compiled grammar for a specific speech recognition engine is called "preparing" the grammar file. Usually reserved for large grammars, preparing a grammar can reduce latency when an application loads the grammar.

You can compile grammars in the Microsoft Speech Platform SDK 11 from source files in XML format or from SrgsDocument objects, using one of the Compile() methods. The result is a grammar file in binary format with the ".cfg" extension. You can also use the Compile Grammar tool (see Compile Grammar Reference Manual) to create CFG files from source grammars in XML and ARPA formats. Grammar files in CFG format can be loaded by any speech recognition engine in the Speech Platform SDK 11.

When you prepare a compiled grammar file, you add information (see AddEnginePart(SpeechRecognitionEngine)) to the file about a specific speech recognition engine. This information allows the specified speech recognition engine to most efficiently load the prepared grammar file.

A prepared grammar file carries the ".cfgpp" extension, to indicate that it has been prepared for a specific speech recognition engine. For example, if your source grammar file is MyGrammar.grxml, it will become MyGrammar.cfg after being compiled, and then MyGrammar.cfgpp after being prepared.

An EnginePart is information about a specific speech recognition engine which, when added to a grammar in CFG format, allows the engine to load the grammar in the most efficient manner possible.

Note

If a grammar is being fetched across the internet by a hosted speech recognition engine (for example, a hosted IVR service such as Tellme), the grammar should never be prepared by the developer. Instead, the grammar’s optimization should remain at the CFG format. This allows the hosted speech recognizer to do the preparation of the grammar dynamically. For hosted IVR applications, grammars outside of the hosted platform should always be in source format (for example, GRXML) or optimized to CFG format.

Examples

The following gives an example of how to write a method that prepares a grammar.

private System.IO.Stream Prepare(System.IO.Stream cfgStream, String engineTokenName)
        {
            GrammarInfo cfg = null;
            SpeechRecognitionEngine srEngine = null;

            try
            {
                srEngine = new SpeechRecognitionEngine(engineTokenName);
            }
            catch 
            {
                throw new ArgumentException("Invalid Engine Token Name: " + engineTokenName);
            }

            try
            {
              cfg  = new GrammarInfo(cfgStream);
            }
            catch (Exception ex)
            {
                throw new ArgumentException("Invalid CFG stream: " + ex.Message);
            }

            if ((srEngine != null) && (cfgStream != null))
            {
                try
                {
                    cfg.ExtraParts.AddEnginePart(srEngine);
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("Failed to Added Engine Part from " + engineTokenName + ": " + ex.Message);
                }

                try
                {
                    cfg.Save(cfgStream);
                }
                catch (Exception ex)
               {
                    throw new InvalidOperationException("Failed to serialize cfg stream: " + ex.Message);
                }
                return cfgStream;
            }
            else
            {
                throw new InvalidOperationException("SR Engine or CFG Stream are not valid");
            }
            
        }

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

GrammarInfo Members

Microsoft.Speech.Recognition Namespace