从Speech获取用户输入?

我刚刚开始在C#.Net中尝试Windows Speech to Textfunction。 我目前有基础工作(IE – 说些什么,它会根据你说的提供输出)。 但是,我正在努力弄清楚如何实际接收用户输入作为变量。

我的意思是,例如。 如果用户说:

"Call me John" 

然后我希望能够将John这个词作为变量,然后将其存储为人员用户名。

我目前的SpeechRecognized事件如下:

 void zeusSpeechRecognised(object sender, SpeechRecognizedEventArgs e) { writeConsolas(e.Result.Text, username); switch (e.Result.Grammar.RuleName) { case "settingsRules": switch (e.Result.Text) { case "test": writeConsolas("What do you want me to test?", me); break; case "change username": writeConsolas("What do you want to be called?", me); break; case "exit": writeConsolas("Do you wish me to exit?", me); break; } break; } } 

注意: writeConsolas只是RichTextBox一个美化的附加行。

我想添加另一个case ,它执行以下操作:

 case "call me" username = e.Result.GetWordFollowingCallMe() //Obv not a method, but thats the general idea. break; 

显然,没有这样的方法,但这是我希望实现的一般想法。 有没有办法搜索特定的短语(IE: Call me ),并采取以下措辞?

编辑:我应该注意,e.Result.Text只返回它可以匹配字典中的文本的单词。

它看起来不像你的情况e.Result.Text表示你可以枚举的东西:你正在检查启动文本的单词,而不是整个文本。 在这种情况下,你不应该使用一个switch ,而是去一个ifthenelse s链:

 var text = e.Result.Text; if (text.StartsWith("test")) { writeConsolas("What do you want me to test?", me); } else if (text.StartsWith("change username")) { writeConsolas("What do you want to be called?", me); } else if (text.StartsWith("exit")) { writeConsolas("Do you wish me to exit?", me); } else if (text.StartsWith("call me")) { // Here you have the whole text. Chop off the "call me" part, // using Substring(), and do whatever you need to do with the rest of it } else ... 

好吧,它不能用于e.Result.Textswitch ,因为它将测试整个值: Call Me John

您应该在default情况下或在switch外部有条件

但我真的会重构所有这些,试图避免switch或大量if..else if...else

 const string Callme = "call me"; var text = e.Result.Text; switch (text) { case "test": writeConsolas("What do you want me to test?", me); break; case "change username": writeConsolas("What do you want to be called?", me); break; case "exit": writeConsolas("Do you wish me to exit?", me); break; } if (text.StartsWith(CallMe) userName = text.Replace(CallMe, string.Empty).Trim(); 

我会考虑更新你的语法以使用SemanticValues,这样你就可以直接提取结果而不必解析识别结果。 这里有一个快速示例,演示了SemanticValuesSemanticResultKeysSemanticResultValues