一起问一个链/多个问题(语音识别)

本文关键字:问题 语音识别 一起 一个 | 更新日期: 2023-09-27 18:31:01

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

我可能会说"嗨,计算机",我希望我的计算机回答说"嗨,先生。你好吗?然后我的电脑会说"好吗,你自己呢?"我的电脑会说别的。

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

//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;

一起问一个链/多个问题(语音识别)

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

一个有趣的实现可以基于这个问题的答案,特别是这个答案,它很好地映射了响应/操作对。

您需要工厂模式。

工厂只是简单地反映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;
        }
    }
}