gpt4 book ai didi

c# - 从语音中获取用户输入?

转载 作者:太空狗 更新时间:2023-10-29 20:10:25 24 4
gpt4 key购买 nike

我刚刚开始在 C# .Net 中试用 Windows Speech to Text 功能。我目前有基础工作(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;

显然,没有这样的方法,但这是我希望实现的总体思路。有没有一种方法可以搜索特定的短语(即:Call me)并获取以下单词?

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

最佳答案

在您的情况下,它看起来不像 e.Result.Text 代表您可以枚举的内容:您正在检查文本开头的单词,而不是整个文本。在这种情况下,您不应该使用 switch,而应该使用 if-then-else 链>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
...

关于c# - 从语音中获取用户输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17878565/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com