【问题标题】:Ask a chain/multiple questions together (Voice Recognition)一起问一个链/多个问题(语音识别)
【发布时间】:2013-09-11 03:03:40
【问题描述】:

我需要帮助向我的程序提出一系列问题。 例如:

我可能会说“嗨,电脑”,我希望我的电脑回应说“嗨,先生。你好吗?”然后我的电脑会说“你自己好吗?”我的电脑会说别的。

到目前为止,我正在使用 Case 语句。下面是我的代码示例:

//Kindness
            case "thank you":
            case "thank you jarvis":
            case "thanks":
            case "thanks jarvis":
                if (ranNum <= 3) { QEvent = ""; JARVIS.Speak("You're Welcome Sir"); }
                else if (ranNum <= 6) { QEvent = ""; JARVIS.Speak("Anytime"); }
                else if (ranNum <= 10) { QEvent = ""; JARVIS.Speak("No problem boss"); }
                break;

【问题讨论】:

  • 你忘记了问题。
  • 我的程序根据我给出的示例回答一系列问题的最佳方法是什么?... thx
  • 您是否研究过使用语音语法?语法可以指定将匹配相关短语的规则,就像您列出的那样,并返回一个指示下一步做什么的标记。这消除了 case 语句。
  • 请考虑对那些对您有帮助的帖子点赞和/或标记答案。

标签: c# switch-statement case speech-recognition


【解决方案1】:

我取得了很大成功的一种方法是创建“上下文”,它是响应和脚本的(嵌套)集合。当您找到匹配的上下文时,您将该上下文推送到堆栈中,并开始在内部上下文中查找响应。如果没有响应与当前的上下文集匹配,则弹出堆栈并重试。如果堆栈为空,则生成默认的“我不明白”响应。

一个有趣的实现可能基于对this question 的回答,尤其是this answer,它很好地映射了响应/动作对。

【讨论】:

  • @user2764826 我整理了一个示例来演示上下文(以及反射、匿名类型、Linq、XML 和一些其他技术)here。它允许静态字符串响应和编程响应。显而易见的下一步是支持字符串插值(将编程响应与字符串响应合并)。
【解决方案2】:

工厂模式就是你所需要的。

工厂只是简单地反映MySpeechMethods 中的所有方法,查找带有SpeechAttributes 的方法并返回MethodInfo 以调用。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using MyApp.SpeechMethods;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var methods = new MySpeechMethods();
            MethodInfo myMethod;
            myMethod = SpeechFactory.GetSpeechMethod("Thank you");
            myMethod.Invoke(methods, null);
            myMethod = SpeechFactory.GetSpeechMethod("Say something funny");
            myMethod.Invoke(methods, null);
            myMethod = SpeechFactory.GetSpeechMethod("I said funny dammit!");
            myMethod.Invoke(methods, null);
        }
    }

    public static class SpeechFactory
    {
        private static Dictionary<string, MethodInfo> speechMethods = new Dictionary<string, MethodInfo>();
        public static MethodInfo GetSpeechMethod(string speechText)
        {
            MethodInfo methodInfo;
            var mySpeechMethods = new MySpeechMethods();
            if (speechMethods.Count == 0)
            {
                var methodNames =
                    typeof (MySpeechMethods).GetMethods(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance);
                var speechAttributeMethods = methodNames.Where(y => y.GetCustomAttributes().OfType<SpeechAttribute>().Any());
                foreach (var speechAttributeMethod in speechAttributeMethods)
                {
                    foreach (var attribute in speechAttributeMethod.GetCustomAttributes(true))
                    {
                        speechMethods.Add(((SpeechAttribute)attribute).SpeechValue, speechAttributeMethod);
                    }
                }
                methodInfo = speechMethods[speechText];
            }
            else
            {
                methodInfo = speechMethods[speechText];
            }

            return methodInfo;
        }
    }
}

namespace MyApp.SpeechMethods
{
    public class MySpeechMethods
    {
        [Speech("Thank you")]
        [Speech("Thank you Jarvis")]
        [Speech("Thanks")]
        public void YourWelcome()
        {
            JARVIS.Speak("You're Welcome Sir"); 
        }

        [Speech("Say something funny")]
        public void SayFunny()
        {
            JARVIS.Speak("A priest, a rabbi and a cabbage walk into a bar"); 
        }

        [Speech("I said funny dammit!")]
        public void TryFunnyAgain()
        {
            JARVIS.Speak("My apologies sir."); 
        }
    }

    [System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple = true)]
    public class SpeechAttribute : System.Attribute
    {
        public string SpeechValue { get; set; }

        public SpeechAttribute(string textValue)
        {
            this.SpeechValue = textValue;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    相关资源
    最近更新 更多