我有这个代码来设置自定义语法并将其加载到语音识别引擎
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);
是否可以制作它以便识别多个选项,例如turn
,on
和lamp
或者我必须在我的Choices数组中写出所有变体吗?
答案 0 :(得分:1)
我将你的语法分成多个部分 - 状态(开/关),名词(灯)和动词(转)。随着你的语法扩展(我很确定你想要除了灯之外还要打开其他东西),你可以轻松更新你的程序。 (此外,这样可以轻松地将SemanticResultKeys绑定到各个部分,将SemanticResultValues绑定到部件中的各种选项,这样您就不必解析英文文本了。)
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);
如果确实希望用户说“关闭灯”或任何其他字序,那么您仍然可以分开这些部分,但使用Choices
元素组合它们,并使用repeat count强制执行最低计数:
GrammarBuilder grammar = new GrammarBuilder();
Choices c = new Choices([verb, lamp, state]);
grammar.Append(c, 2, 3);
Grammar g = new Grammar(grammar);