2013-09-11 28 views
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; 
+1

你忘了問題。 –

+0

根據我給出的例子,我的程序回答一系列問題的最佳方法是什麼?thx – user2764826

+0

您是否研究過使用語法語法?語法可以指定與相關短語相匹配的規則(如您已列出),並返回一個指示該指示指示下一步該操作的標記。這消除了案件陳述。 –

回答

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; 
     } 
    } 
} 
1

的一種方法,我有很好的成功與是創建「語境」,這是應答和腳本(嵌套)集合。當你找到一個匹配的上下文時,你將該上下文壓入堆棧,並開始在內部上下文中尋找響應。如果沒有響應與當前上下文集匹配,則彈出堆棧並重試。如果堆棧爲空,則生成默認的「我不明白」響應。

這個有趣的實現可以基於對this question的回答,特別是this answer,它很好地映射了響應/動作對。

+0

有沒有可能讓我看一個例子? – user2764826

+0

@ user2764826我放在一起展示上下文(以及反射,匿名類型,Linq,XML和其他一些技術)的示例[這裏](http://sdrv.ms/1982IFa)。它允許靜態字符串響應和編程響應。明顯的下一步將是支持字符串插值(將字符串響應的程序化響應合併)。 –