I am programming with the SAPI 5.1 COM SDK. I need to construct a
grammar in which sentences can come in either two or three parts:
<partA><partB>
or
<partA><partB><partC>
For example, partA could be any one of: "I like", "I love",
"I
enjoy", "I hate"
partB could be any one of: "books", "candy",
"dogs", "television"
and partC could be any on of: "and cats", "and cookies",
"and
sailing", "and puns"
So for example, the recognizer would recognize "I like dogs." or
"I
enjoy books and sailing."
A method I found that works is as follows:
-----------------------------------------
ISpRecoGrammar grammar;
SPSTATEHANDLE hState1, hState2, hState3;
grammar.GetRule(NULL, 1, SPRAF_TopLevel | SPRAF_Active, TRUE,
&hState1);
grammar.CreateNewState(hState1, &hState2);
for each phrase in partA:
grammar.AddWordTransition(hState1, hState2, phrase, L" ",
SPWT_LEXICAL, 1, NULL);
for each phrase in partB:
grammar.AddWordTransition(hState2, 0, phrase, L" ", SPWT_LEXICAL, 1,
NULL);
// This takes care of <partA><partB> sentences. To recognize
<partA><partB><partC>
// sentences, I have to do:
grammar.CreateNewState(hState2, &hState3);
for each phrase in partB:
grammar.AddWordTransition(hState2, hState3, phrase, L" ",
SPWT_LEXICAL, 1, NULL);
for each phrase in partC:
grammar.AddWordTransition(hState3, 0, phrase, L" ", SPWT_LEXICAL, 1,
NULL);
-----------------------------------------
Note that I had to add to the grammar all of the phrases in partB
twice.
Is there a way to do this by adding the phrases partB just once?
Jim
|