0

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

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

截至目前,我正在使用 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;
4

2 回答 2

1

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

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

于 2013-09-12T04:17:32.127 回答
0

工厂模式是您所需要的。

工厂只是简单地反映 中的所有方法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;
        }
    }
}
于 2013-09-11T03:23:32.647 回答