2014-02-09 64 views
0

我有這樣的代碼來設置自定義的語法並將其加載到一個語音識別引擎識別多個語法選擇

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,onlamp或者我是否必須寫出所有變體,我可以在我的Choices數組中進行說明?

+1

您的代碼不用'customDictationGrammar'做任何事情,只是創建一個對象並拋出它離開? –

+0

此外,此示例顯示如何將字詞順序放在一起:http://msdn.microsoft.com/en-us/library/system.speech.recognition.grammarbuilder(v=vs.110).aspx –

回答

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);