【问题标题】:Get user input from Speech?从语音获取用户输入?
【发布时间】:2013-07-26 10:06:37
【问题描述】:

我刚刚开始在 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 只返回它可以与字典中的 Text 匹配的单词。

【问题讨论】:

  • +1 用于智力上有趣的编程问题,但只是我的 2 美分:如果计算机问我的名字,我的第一反应是回答 just my name ,响亮而清晰。不是“叫我伊斯梅尔”或“我的名字是泥”或类似的东西……如果你走“叫我”的路,你可能想测试一下:)
  • @MiklosAubert 感谢您的建议。但是,我计划实施它,以便用户首先说“Call me blah”而不是被提示

标签: c# .net wpf speech-recognition speech-to-text


【解决方案1】:

在您的情况下,e.Result.Text 看起来并不代表您可以列举的内容:您正在检查以文本开头的单词,而不是整个文本。在这种情况下,您不应该使用switch,而是使用if-then-elses 的链:

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 
    ...

【讨论】:

    【解决方案2】:

    它不能在e.Result.Text 上的switch 中使用,因为它将测试整个值: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();
    

    【讨论】:

      【解决方案3】:

      我会考虑更新您的语法以使用SemanticValues,以便您可以直接提取结果,而不必解析识别结果。有一个简单的示例here 演示了SemanticValuesSemanticResultKeysSemanticResultValues

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-23
        • 1970-01-01
        相关资源
        最近更新 更多