SpeechRecoContext CreateGrammar Method (Microsoft Speech Platform)

Microsoft Speech Platform SDK 11

Microsoft Speech Platform

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 Visual Basic form code demonstrates two ways of creating and activating a grammar: a dictation grammar or a command and control grammar. Both example grammars have a GrammarId of zero. The zero for the GrammarId is not required as it is the default. In the case of the dictation grammar, it is set up and activated by calling ISpeechRecoGrammar.DictationLoad and ISpeechRecoGrammar.DictationSetState.

The command and control grammar is handled differently. You first need to specify a grammar file by calling one of the command load methods (in this example, ISpeechRecoGrammar.CmdLoadFromFile. See the ISpeechRecoGrammar interface for more loading options. Then a call to ISpeechRecoGrammar.CmdSetRuleIdState activates the grammar.

To run this code, paste it into the Declarations section of a form that contains two command button controls named Command1 and Command2.


Option Explicit

Dim RC As SpSharedRecoContext
Dim Grammar As ISpeechRecoGrammar

Private Sub Form_Load()
    On Error GoTo EH

    Set RC = New SpSharedRecoContext
    Set Grammar = RC.CreateGrammar(0)

EH:
    If Err.Number Then ShowErrMsg
End Sub

Private Sub Command1_Click()
    On Error GoTo EH

    With Grammar
        .DictationLoad
        .DictationSetState SGDSActive
        MsgBox .State, vbInformation
        End
    End With

EH:
    If Err.Number Then ShowErrMsg
End Sub

Private Sub Command2_Click()
    On Error GoTo EH

    With Grammar
        .CmdLoadFromFile "C:\sol.xml", SLODynamic
        .CmdSetRuleIdState 0, SGDSActive
        MsgBox .State, vbInformation
    End With

EH:
    If Err.Number Then ShowErrMsg
End Sub

Private Sub ShowErrMsg()

    ' Declare identifiers:
    Dim T As String

    T = "Desc: " & Err.Description & vbNewLine
    T = T & "Err #: " & Err.Number
    MsgBox T, vbExclamation, "Run-Time Error"
    End

End Sub