TMH
TMH

Reputation: 6246

Recognize multiple grammar choices

I have this code to set up a custom grammar and load it into a speech recognition engine

DictationGrammar customDictationGrammar = new DictationGrammar();
customDictationGrammar.Name = "Dictation";
customDictationGrammar.Enabled = true;
GrammarBuilder grammar = new GrammarBuilder();
grammar.Append(new Choices("turn", "on", "off",  "lamp"));
grammar.Culture = ri.Culture;
Grammar g = new Grammar(grammar);



spRecEng.LoadGrammar(g);
spRecEng.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(spRecEng_SpeechRecognized);
spRecEng.SpeechRecognitionRejected += new EventHandler<SpeechRecognitionRejectedEventArgs>(spRecEng_SpeechRecognitionRejected);

spRecEng.SetInputToAudioStream(source.Start(), new SpeechAudioFormatInfo(EncodingFormat.Pcm, 16000, 16, 1, 32000, 2, null));
spRecEng.RecognizeAsync(RecognizeMode.Multiple);

Is it possible to make it so it will recognize multiple options e.g. turn, on, and lamp or would I have to write in all variations I could say it in my Choices array?

Upvotes: 0

Views: 1182

Answers (1)

Eric Brown
Eric Brown

Reputation: 13932

I'd split your grammar into multiple parts - a state (on/off), a noun (lamp), and a verb (turn). As your grammar expands (I'm pretty sure you're going to want to turn other things on besides the lamp), you can easily update your program. (Also, this makes it easy to bind SemanticResultKeys to the various parts and SemanticResultValues to the various options in the parts, so you don't have to parse English text.)

GrammarBuilder state = new GrammarBuilder();
state.Append(new Choices("on", "off"));
state.Culture = ri.Culture;
GrammarBuilder noun = new GrammarBuilder();
noun.Append(new Choices("lamp"));
noun.Culture = ri.Culture;
GrammarBuilder verb = new GrammarBuilder();
verb.Append(new Choices("turn"));
verb.Culture = ri.Culture;   

GrammarBuilder grammar = new GrammarBuilder();
grammar.Append(verb);
grammar.Append(lamp);
grammar.Append(state);
Grammar g = new Grammar(grammar);

If you really wanted users to say "off lamp turn" or any other word order, then you can still separate the parts, but combine them using a Choices element, and use the repeat count to enforce a minimum count:

GrammarBuilder grammar = new GrammarBuilder();
Choices c = new Choices([verb, lamp, state]);
grammar.Append(c, 2, 3);
Grammar g = new Grammar(grammar);

Upvotes: 1

Related Questions