SpeechRecoContext CreateGrammar Method

Microsoft Speech SDK

Intelligent Interface Technologies Home Page Microsoft Speech SDK

Speech Automation 5.1

Interface: ISpeechRecoContext

CreateGrammar Method


The CreateGrammar method creates an object based on ISpeechRecoGrammar.

Before speech recognition takes place, a speech recognition (SR) engine requires two things: grammar creation and grammar activation. A recognizer may have more than one grammar associated with it although they are usually limited to one each of two types: dictation and context free grammar (CFG). CFG is used for command and control. The recognizer can have more than one active grammar of the same type open at one time. In this case, the recognition is assigned to the grammar in which a unique match is made. If more than one grammar can match the recognition, the earliest opened grammar will receive the recognition.

A grammar must be activated prior to use. Call ISpeechRecoGrammar.DictationSetState to activate or deactivate a dictation grammar. ISpeechRecoGrammar.CmdSetRuleState is used to activate or deactivate a command and control rule which also controls the associated grammar. By controlling the state of grammars, different grammars may be used at different times.


SpeechRecoContext.CreateGrammar(
     [GrammarId As Variant = 0]
) As ISpeechRecoGrammar

Parameters

GrammarId
[Optional] Specifies the GrammarId. The GrammarId identifies each grammar. The values do not have to be unique although each grammar instance can only have one identifier. The default value is zero.

Return Value

The CreateGrammar method returns an ISpeechRecoGrammar variable.


Example

The following snippet demonstrates creating and activating a dictation grammar with a GrammarId of zero. The zero for the GrammarId is not required as it is the default. Activate the grammar by calling ISpeechRecoGrammar.DictationSetState.

Public WithEvents RC As SpSharedRecoContext
Public myGrammar As ISpeechRecoGrammar

Set RC = New SpSharedRecoContext
Set myGrammar = RC.CreateGrammar(0)
myGrammar.DictationLoad
myGrammar.DictationSetState SGDSActive

The next example demonstrates creating and activating a command and control grammar. The grammar still has a GrammarId of zero (the default value), as the example above does. In addition to creating and activating the grammar, the grammar file must also be described by calling one of the command load methods. See the ISpeechRecoGrammar interface for more loading options. In this case, ISpeechRecoGrammar.CmdLoadFromFile is used to load the command file sol.xml.

Public WithEvents RC As SpSharedRecoContext
Public myGrammar As ISpeechRecoGrammar

Set RC = New SpSharedRecoContext
Set myGrammar = RC.CreateGrammar

myGrammar.CmdLoadFromFile "sol.xml", SLODynamic
myGrammar.CmdSetRuleIdState 0, SGDSActive