我正在尝试使用c#(windows窗体)制作语音识别应用程序。
以下是我正在使用的代码
//recognizing speech
SpeechRecognitionEngine r = new SpeechRecognitionEngine();
//then load the grammar
r.LoadGrammar(new Grammar(new GrammarBuilder
(new Choices("hello","how are you","nice to meet you")))
{ Name = "speechGrammar" });
r.SetInputToDefaultAudioDevice(); // set the input of the speech recognizer to the default audio device
r.RecognizeAsync(RecognizeMode.Multiple); // recognize speech asynchronous
//adding a event handler
r.SpeechRecognized += r_SpeechRecognized;
//then create a method for that
void r_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
if (e.Result.Text == "test") // e.Result.Text contains the recognized text
{
SpeechSynthesizer s = new SpeechSynthesizer();
s.Speak("hello");
MessageBox.Show("Hi User");
s.Dispose();
}
}
所以我希望程序将文本框的文本更改为所说的但不在语法中的单词/短语。
离
send a mail to xyz
句子不在语法列表中
所以当我说出来时我想要
textbox1.text = spokenword;
我应该对此结果的代码进行哪些更改。
答案 0 :(得分:0)
你无法识别语法中没有的单词。你只需要做更大的语法。如果你想识别任意文本你可以使用DictationGrammar,它应该覆盖大部分单词。
识别文本后,您可以将其分配到文本框:
textbox1.text = e.Result.Text;