2013-10-19 40 views
1

我會盡我所能將這些詞彙表達出來。我想要有多個因素,並使它們等於一個可重用的因子。作爲一個例子,我正在做一個語音識別項目,我想用sup來表示一個單詞列表。c#將因素合併爲一個

EX:(甚至不知道如果一個聲明是正確的工作。)

var lower = speech.ToLower(); 
{ 
    if (lower.Contains("hello") || lower.Contains("hi") || lower.Contains("hey"))      
    {       
     object == sup;      
    } 
} 

這樣燮現在代表你好,你好了,嘿嘿,使事情變得更加簡單。再次,我甚至不知道if語句是否適合這項工作,或者如果有一個對象適合這種類型的場景。我希望這是有道理的,謝謝你!

+0

如果你使用'if',讓我先向任何正在處理你的代碼的人表示我的哀悼。 –

回答

1

當然,創建一個詞典,其中包含一側的值列表和另一側的響應。

private IDictionary<List<String>, List<String>> _responses = new Dictionary<List<String>, List<String>>(); 

_responses.Add(
      new List<String> { "Hello there!", "Hey mate", "Hi!" }}, 
      new List<String> { "sup"); 

_responses.Add(
      new List<String> { "Buddy", "Mate", "Hombre" }}, 
      new List<String> { "sup"); 

現在爲了找回的東西:

foreach(var keyword in _responses.Keys){ 
if(keywords.Contains("sup"){ 
    return _responses[keyword]; 
} 
} 

搜索 「SUP」 將返回的適當的應對措施清單。我也使用List<String>作爲查找值,以便您可以將多個關鍵字鏈接到相同的搜索結果。

,如果你要進入由多個值的字符串,只需添加外環它:

整個重寫:

這個例子假設你有一個輸入字符串。您的要求是檢查此輸入字符串是否包含任何一組字詞。應該能夠通過一個單詞來引用每組單詞。

void Main() 
{ 
    var babel = new Babel("hi homies, this is for my mate out there."); 
    if(babel.HasAnswer("sup") && babel.HasAnswer("friend")){ 
     Console.WriteLine ("It has both!"); 
    } else { 
     Console.WriteLine ("Boo Jeroen you suck"); 
    } 
} 

public class Babel { 
    private IDictionary<List<String>, List<String>> _responses = new Dictionary<List<String>, List<String>>(); 
    private String query; 

    public Babel(string query){ 
     this.query = query; 
     _responses.Add(
       new List<String> { "sup" }, 
       new List<String> { "hello", "hey", "hi"}); 

     _responses.Add(
       new List<String> {"friend" }, 
       new List<String> { "buddy", "mate", "hombre" }); 
    } 

    public bool HasAnswer(string input){ 

    foreach(var token in input.Split(' ')) { 
     foreach(var keyword in _responses.Keys){ 
     if(keyword.Contains(token)){ 
     return ContainsAny(_responses[keyword]); 
     } 
     } 
    } 
    return false; 
    } 

    private bool ContainsAny(List<String> toCompare){ 
     foreach(string item in toCompare){ 
      foreach(string token in query.Split(' ')){ 
       if(token == item) return true; 
      } 
     } 
     return false;    
    } 


} 

輸出:

它既有!

這種方法的好處是:添加一組新單詞就像向詞典添加條目一樣簡單。非常高的可擴展性!你也可以讓多個值引用同一個列表(如果你想創建應該引用與「sup」相同的值的「howdie」,你可以將它添加到列表中並且完成)。

+0

如果這不是你要找的(這會讓我感到驚訝),讓我知道。 –

+0

我正在理解字典;然而,它重用了代表字典中所有我沒有掌握的單詞的「sup」。我想在後面的if語句中重新使用「sup」。可以說,「朋友」一詞與「sup」的情況相同,因爲它通過字典代表許多其他詞彙。我想以這種方式使用sup和friend:if(sup && friend)== true {程序其餘部分}。我希望這是有道理的。這真的幫助了很多。 – Holden

+0

@霍爾登:我相信我第一次誤解了你的問題。檢查我編輯的答案,我相信它現在做你想做的。 –

1

如果你想多個輸入指向一個輸出,那麼Dictionary看起來是一個不錯的選擇。在constructor中注意我使用StringComparer.OrdinalIgnoreCase作爲IEqualityComparer<string>,它獲得一個執行不區分大小寫的字符串比較的對象,因此在Dictionary上使用密鑰時不需要混淆Sting.ToLower()

Dictionary<string,string> simple = new Dictionary<string,string>(StringComparer.OrdinalIgnoreCase) 
{ 
    {"hello", "sup"}, 
    {"hi", "sup"}, 
    {"hey", "sup"} 
} 

要找到你可以循環適當的反應在KeysCollection如下。既然你沒有在字典中傳遞密鑰,我想你需要在這裏使用ToLower()。

string response = string.Empty; 
foreach (string s in simple.Keys) 
{ 
    if (speech.ToLower().Contains(s.ToLower())) 
    { 
     response = simple[s]; 
     break; 
    } 
} 
0

基本上哈里森說,但我會使用TryGetValue如下。

using System; 
using System.Collections.Generic; 

namespace Test 
{ 
    class Program 
    { 
     interface Word 
     { 
      String GetResponse(); 
     } 

     public class Sup : Word 
     { 
      public String GetResponse() 
      { 
       return "Hello back to you!"; 
      } 
     } 

     public class Lates : Word 
     { 
      public String GetResponse() 
      { 
       return "Sorry to see you go."; 
      } 
     } 

     static void Main(string[] args) 
     { 
      var sup = new Sup(); 
      var lates = new Lates(); 
      var mapping = new Dictionary<String, Word>(StringComparer.OrdinalIgnoreCase) 
      { 
       {"hi", sup}, 
       {"hello", sup}, 
       {"hey", sup}, 
       {"bye", lates}, 
       {"later", lates}, 
       {"astalavista", lates}, 
      }; 

      Word word; 
      if (mapping.TryGetValue("HELLO", out word)) 
       Console.WriteLine(word.GetResponse()); 

      if (mapping.TryGetValue("astalavista", out word)) 
       Console.WriteLine(word.GetResponse()); 

      if (mapping.TryGetValue("blaglarg", out word)) 
       Console.WriteLine(word.GetResponse()); 
     } 
    } 
}