7

我刚刚开始在 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;

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

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

4

3 回答 3

4

在您的情况下,它看起来并不e.Result.Text代表您可以列举的东西:您正在检查以文本开头的单词,而不是整个文本。在这种情况下,您不应该使用 a ,而是使用- - sswitch链:ifthenelse

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 
    ...
于 2013-07-26T10:14:41.067 回答
4

好吧,它不能在switchon中使用e.Result.Text,因为它将测试整个值 : 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();
于 2013-07-26T10:14:49.830 回答
2

I would look at updating your grammar to use SemanticValues so that you can extract the results directly rather than having to parse through recognition results. There's a quick example here that demonstrates SemanticValues, SemanticResultKeys, and SemanticResultValues.

于 2013-07-28T05:40:49.680 回答