How can I use SAPI 4 CFG grammars in my application?

Last reviewed: 7/15/2011

HOW Article ID: H071142

The information in this article applies to:

  • Developer Workbench 2011
  • GrammarKit 4
  • SpeechKit 7

Summary

Chant Developer Workbench 2011, GrammarKit 4, and SpeechKit 7 provide support for SAPI 4 grammars.

More Information

The SAPI 4 grammar text file format is similar to .inf or .ini files and is adapted for speech recognition by Microsoft.

Creating, Editing, and Testing SAPI 4 Grammars

Chant Developer Workbench 2011 continues support for creating, editing, and testing SAPI 4 grammars. The editor provides intelliprompt assistance for fast syntax input and editing.

The steps to create a SAPI 4 grammar with Chant Developer Workbench and GrammarKit are as follows:

  1. Select the File->New->Speech Recognition Grammar->SAPI 4 Grammar menu item or click the New Standard toolbar button and select SAPI 4 Grammar to create one in a document window. Note if you do not see any grammar types listed in the menu, then you need to run the Chant Software Unlock utility to begin your evaluation, your GrammarKit evaluation has expired, or you need to register and unlock your licensed copy with a serial number.
  2. Save your new grammar to a file by selecting the File->Save menu item or click the Save Standard toolbar button. You are prompted to confirm the file name.

Dynamically Generating SAPI 4 Grammars

GrammarKit 4 introduces support for dynamically generating and compiling SAPI 4 grammars.

The following example illustrates generating a SAPI 4 grammar:


private Chant.GrammarKit.NChantGM NChantGM1;
private NChantGrammar _NChantGrammar = null;
// Instantiate NChantGM object
NChantGM1 = new NChantGM(this);
// Build Colors grammar
_NChantGrammar = NChantGM1.CreateGrammar("Colors");
_NChantGrammar.AddComment("This is the list of colors");
NChantGrammarRule nChantGrammarRule = _NChantGrammar.AddRule("Color");
NChantGrammarList nChantGrammarList = nChantGrammarRule.AddList("colorlist");
string[] colors = { "red", "blue", "green", "yellow", "pink", "purple", "brown", "orange" };
for (int i = 0; i < colors.Length; i++)
{
    nChantGrammarList.AddPhrase(colors[i]);
}
string grammarSource = _NChantGrammar.GenerateGrammar(ChantGrammarSyntax.CGSSAPI4CFG);

CChantGM* m_pChantGM;  // ChantGM object
CChantGrammar* m_pChantGrammar;  // ChantGrammar object
// Instantiate ChantGM object
m_pChantGM = new CChantGM();
// Build Colors grammar
m_pChantGrammar = m_pChantGM->CreateGrammar(L"Colors");
m_pChantGrammar->AddComment(L"This is the list of colors");
CChantGrammarRule* pChantGrammarRule = m_pChantGrammar->AddRule(L"Color");
CChantGrammarList* pChantGrammarList = pChantGrammarRule->AddList(L"colorlist");
wchar_t* colors[] = { L"red", L"blue", L"green", L"yellow", L"pink", L"purple", L"brown", L"orange" };
for (int i = 0; i < 8; i++)
{
    pChantGrammarList->AddPhrase(colors[i]);
}
wchar_t* pszGrammar = m_pChantGrammar->GenerateGrammar(CGSSAPI4CFG);
    

CChantGM* ChantGM1;  // ChantGM object
CChantGrammar* ChantGrammar1;  // ChantGrammar object
// Instantiate ChantGM object
ChantGM1 = new CChantGM();
// Build Colors grammar
ChantGrammar1 = ChantGM1->CreateGrammar("Colors");
ChantGrammar1->AddComment("This is the list of colors");
CChantGrammarRule* pChantGrammarRule = ChantGrammar1->AddRule("Color");
CChantGrammarList* pChantGrammarList = pChantGrammarRule->AddList("colorlist");
pChantGrammarList->AddPhrase("red");
pChantGrammarList->AddPhrase("blue");
pChantGrammarList->AddPhrase("green");
pChantGrammarList->AddPhrase("yellow");
pChantGrammarList->AddPhrase("pink");
pChantGrammarList->AddPhrase("purple");
pChantGrammarList->AddPhrase("brown");
pChantGrammarList->AddPhrase("orange");
String sGrammar = ChantGrammar1->GenerateGrammar(CGSSAPI4CFG);

var
ChantGM1: TChantGM;
aTChantGrammar: TChantGrammar;
aTChantGrammarRule: TChantGrammarRule;
aTChantGrammarList: TChantGrammarList;
sGrammar: string;
begin
    // Instantiate ChantGM object
    ChantGM1 := TChantGM.Create();
    // Build Colors grammar
    aTChantGrammar := ChantGM1.CreateGrammar('Colors');
    aTChantGrammar.AddComment('This is the list of colors');
    aTChantGrammarRule := aTChantGrammar.AddRule('Color');
    aTChantGrammarList := aTChantGrammarRule.AddList('colorlist');
    aTChantGrammarList.AddPhrase('red');
    aTChantGrammarList.AddPhrase('blue');
    aTChantGrammarList.AddPhrase('green');
    aTChantGrammarList.AddPhrase('yellow');
    aTChantGrammarList.AddPhrase('pink');
    aTChantGrammarList.AddPhrase('purple');
    aTChantGrammarList.AddPhrase('brown');
    aTChantGrammarList.AddPhrase('orange');
    sGrammar := ChantGrammar.GenerateGrammar(CGSSAPI4CFG);
end;
    

private JChantGM JChantGM1;
private JChantGrammar _JChantGrammar;
// Instantiate ChantGM object
JChantGM1 = new JChantGM();
// Build Colors grammar
_JChantGrammar = JChantGM1.createGrammar("Colors");
_JChantGrammar.addComment("This is the list of colors");
JChantGrammarRule jChantGrammarRule = _JChantGrammar.addRule("Color");
JChantGrammarList jChantGrammarList = jChantGrammarRule.addList("colorlist");
String[] colors = { "red", "blue", "green", "yellow", "pink", "purple", "brown", "orange" };
for (int i = 0; i < 8; i++)
{
    jChantGrammarList.addPhrase(colors[i]);
}
String grammarSource = _JChantGrammar.generateGrammar(ChantGrammarSyntax.CGSSAPI4CFG);
    

// Build Colors grammar
var wChantGrammar = WChantGM1.CreateGrammar("Colors");
wChantGrammar.AddComment("This is the list of colors");
var wChantGrammarRule = wChantGrammar.AddRule("Color");
var wChantGrammarList = wChantGrammarRule.AddList("colorlist");
wTChantGrammarList.AddPhrase("red");
wTChantGrammarList.AddPhrase("blue");
wTChantGrammarList.AddPhrase("green");
wTChantGrammarList.AddPhrase("yellow");
wTChantGrammarList.AddPhrase("pink");
wTChantGrammarList.AddPhrase("purple");
wTChantGrammarList.AddPhrase("brown");
wTChantGrammarList.AddPhrase("orange");
var sGrammar = wChantGrammar.GenerateGrammar(CGSSAPI4CFG);

Dim WithEvents XChantGM1 As XChantGM
Dim aXChantGrammar As XChantGrammar
Dim aXChantGrammarRule As XChantGrammarRule
Dim aXChantGrammarList As XChantGrammarList
Dim sGrammar As String
' Build Colors grammar
Set aXChantGrammar = XChantGM1.CreateGrammar("Colors")
aXChantGrammar.AddComment "This is the list of colors"
Set aXChantGrammarRule = aXChantGrammar.AddRule("Color")
Set aXChantGrammarList = aXChantGrammarRule.AddList("colorlist")
aXChantGrammarList.AddPhrase "red"
aXChantGrammarList.AddPhrase "blue"
aXChantGrammarList.AddPhrase "green"
aXChantGrammarList.AddPhrase "yellow"
aXChantGrammarList.AddPhrase "pink"
aXChantGrammarList.AddPhrase "purple"
aXChantGrammarList.AddPhrase "brown"
aXChantGrammarList.AddPhrase "orange"
sGrammar = aXChantGrammar.GenerateGrammar(CGSSAPI4CFG)

Dim WithEvents NChantGM1 As NChantGM
Dim _NChantGrammar As NChantGrammar
Dim nChantGrammarRule As NChantGrammarRule
Dim nChantGrammarList As NChantGrammarList
Dim grammarSource As String
' Instantiate ChantGM object
NChantGM1 = New NChantGM(Me)
' Build Colors grammar
_NChantGrammar = NChantGM1.CreateGrammar("Colors")
_NChantGrammar.AddComment("This is the list of colors")
nChantGrammarRule = _NChantGrammar.AddRule("Color")
nChantGrammarList = nChantGrammarRule.AddList("colorlist")
nChantGrammarList.AddPhrase("red")
nChantGrammarList.AddPhrase("blue")
nChantGrammarList.AddPhrase("green")
nChantGrammarList.AddPhrase("yellow")
nChantGrammarList.AddPhrase("pink")
nChantGrammarList.AddPhrase("purple")
nChantGrammarList.AddPhrase("brown")
nChantGrammarList.AddPhrase("orange")
grammarSource = _NChantGrammar.GenerateGrammar(ChantGrammarSyntax.CGSSAPI4CFG)

Recognizing with SAPI 4 Grammars

SpeechKit 7 introduces support for recognizing with SAPI 4 grammars through Microsoft SAPI 5.3 and higher.

The following example illustrates defining, enabling, and disabling a SAPI 4 grammar resource for speech recognition.


private Chant.SpeechKit.NChantSR NChantSR1;
// Instantiate NChantSR object
NChantSR1 = new NChantSR(this);
// Define a grammar vocabulary
int grammarVocabID = NChantSR1.DefineResource(ChantSpeechResource.CSRGrammarVocab, "mygrammar.cfg");
// Load and enable grammar vocabulary
NChantSR1.EnableResource(ChantSpeechResource.CSRGrammarVocab, grammarVocabID);
// Disable it
NChantSR1.DisableResource(ChantSpeechResource.CSRGrammarVocab, grammarVocabID);

CChantSR* m_pChantSR;  // ChantSR object
// Instantiate ChantSR object
m_pChantSR = new CChantSR();
// Define a grammar vocabulary
long grammarVocabID = pChantSR->DefineResource(CSRGrammarVocab, L"mygrammar.cfg");
// Load and enable grammar vocabulary
pChantSR->EnableResource(CSRGrammarVocab, grammarVocabID);
// Disable it
pChantSR->DisableResource(CSRGrammarVocab, grammarVocabID);

CChantSR* ChantSR1;  // ChantSR object
// Instantiate ChantSR object
ChantSR1 = new CChantSR();
// Define a grammar vocabulary
long grammarVocabID = ChantSR1->DefineResource(CSRGrammarVocab, "mygrammar.cfg");
// Load and enable grammar vocabulary
ChantSR1->EnableResource(CSRGrammarVocab, grammarVocabID);
// Disable it
ChantSR1->DisableResource(CSRGrammarVocab, grammarVocabID);

var
ChantSR1: TChantSR;
grammarVocabID: Integer;
begin
// Instantiate ChantSR object
ChantSR1 := TChantSR.Create();
// Define a grammar vocabulary
grammarVocabID := ChantSR1.DefineResource(CSRGrammarVocab, 'mygrammar.cfg');
// Load and enable grammar vocabulary
ChantSR1.EnableResource(CSRGrammarVocab, grammarVocabID);
// Disable it
ChantSR1.DisableResource(CSRGrammarVocab, grammarVocabID);

private JChantSR JChantSR1;
// Instantiate ChantSR object
JChantSR1 = new JChantSR();
// Define a grammar vocabulary
int grammarVocabID = JChantSR1.defineResource(ChantSpeechResource.CSRGrammarVocab, "mygrammar.cfg");
// Load and enable grammar vocabulary
JChantSR1.enableResource(ChantSpeechResource.CSRGrammarVocab, grammarVocabID);
// Disable it
JChantSR1.disableResource(ChantSpeechResource.CSRGrammarVocab, grammarVocabID);

 // Define a grammar vocabulary
var grammarVocabID = WChantSR1.DefineResource(CSRGrammarVocab, "file:///c:/mygrammar.cfg");
// Load and enable grammar vocabulary
WChantSR1.EnableResource(CSRGrammarVocab, grammarVocabID);
// Disable it
WChantSR1.DisableResource(CSRGrammarVocab, grammarVocabID);
   

Dim WithEvents XChantSR1 As XChantSR
Dim grammarVocabID as Integer
' Define a grammar vocabulary
grammarVocabID = XChantSR1.DefineResource(CSRGrammarVocab, App.Path & "\mygrammar.cfg")
' Load and enable grammar vocabulary
XChantSR1.EnableResource CSRGrammarVocab, grammarVocabID
' Disable it
XChantSR1.DisableResource CSRGrammarVocab, grammarVocabID
    

Dim WithEvents NChantSR1 As NChantSR
Dim grammarVocabID As Integer
' Instantiate ChantSR object
NChantSR1 = New NChantSR(Me)
' Define a grammar vocabulary
grammarVocabID = NChantSR1.DefineResource(ChantSpeechResource.CSRGrammarVocab, "mygrammar.cfg")
' Load and enable grammar vocabulary
NChantSR1.EnableResource(ChantSpeechResource.CSRGrammarVocab, grammarVocabID)
' Disable it
NChantSR1.DisableResource(ChantSpeechResource.CSRGrammarVocab, grammarVocabID)