I'm using an XML grammar file to develop a Command and Control application. Since there are around 4000 entries in the grammar I can't have it all in one file (it gives an error when I try to load it). I have written multiple XML grammar files, but when I try to load more than one file it replaces the previously loaded file. that is the program only recognizes the phrases in the second grammar file. Can anyone tell me how to load multiple grammars in a single speech recognition application?
+2
A:
You can create multiple grammar, you have to give them different IDs. These snippets are in Delphi, but you can adapt:
// init
FGrammar1 := SpSharedRecoContext1.CreateGrammar(1); // ID 1
FGrammar1.CmdLoadFromFile('CommandMemo1.xml', SLODynamic);
FGrammar2 := SpSharedRecoContext1.CreateGrammar(2); // ID 2
FGrammar2.CmdLoadFromFile('CommandMemo2.xml', SLODynamic);
// start
FGrammar1.CmdSetRuleIdState(0, SGDSActive);
FGrammar2.CmdSetRuleIdState(0, SGDSActive);
...
// in the onRecognition event, test the grammar Id
case Result.PhraseInfo.GrammarId of
1: if SameText(Txt, 'erase memo') then
Memo1.Text := ''
else
if SameText(Txt, 'select memo') then
Memo1.SelectAll;
2: if SameText(Txt, 'copy memo') then
CopyToClipboard(Memo1.Text)
else
if SameText(Txt, 'paste memo') then
Memo1.Text := PasteFromClipboard;
else
raise Exception.Create('bad GrammarId');
end;
François
2009-09-16 05:29:09