2013-02-26 57 views
0

我有一個HashMap,其中某些鍵像「崩潰」和「崩潰」,它們返回相同的響應。我想創建一個新的HashMap,它將同義詞映射到responseMap中的唯一鍵(例如,映射「崩潰」,「崩潰」和「崩潰」到synonym中的「崩潰」)。散列圖鍵將不會工作

private void fillSynonymMap() 
    { 
    synonymMap.put("crash", "crash"); 
    synonymMap.put("crashes", "crash"); 
    synonymMap.put("crashed", "crash"); 
    } 

我被困在什麼是如何輸入這些鍵,以便我可以在下面簡化代碼。

private void fillResponseMap() 
{ 
    responseMap.put("crash", 
        "Well, it never crashes on our system. It must have something\n" + 
        "to do with your system. Tell me more about your configuration."); 
    responseMap.put("crashes", 
        "Well, it never crashes on our system. It must have something\n" + 
        "to do with your system. Tell me more about your configuration.");\ 
    responseMap.put("crashed", 
        "Well, it never crashes on our system. It must have something\n" + 
        "to do with your system. Tell me more about your configuration."); 
} 



public String generateResponse(HashSet<String> words) 
{ 
    for (String word : words) { 
     String response = responseMap.get(word); 
     if(response != null) { 
      return response; 
     } 
    } 

    // If we get here, none of the words from the input line was recognized. 
    // In this case we pick one of our default responses (what we say when 
    // we cannot think of anything else to say...) 
    return pickDefaultResponse(); 
} 
+0

告訴我更多關於您的配置的信息。 – vikingsteve 2013-02-26 07:47:53

+1

我不明白這個問題。你能否更清楚地解釋一下。可能會有一些樣本輸入和輸出。這兩張地圖應該包含哪些地圖? – 2013-02-26 07:48:24

+0

請詳細說明您所需的輸入和輸出。這裏沒有上下文。 – 2013-02-26 07:48:33

回答

1

經過一番小題大做,我寫了一個函數,它會在返回默認消息之前查找同義詞。

public String getResponse() 
{ 
    HashMap<String, String> responseMap = new HashMap<String, String>(); 
    HashMap<String, String> synonymMap = new HashMap<String, String>(); 

    responseMap.put("crash", "Hello there"); 
    // Load the response value. 
    synonymMap.put("crash", "crash"); 
    synonymMap.put("crashed", "crash"); 
    synonymMap.put("crashes", "crash"); 
    // Load the synonyms. 

    String input = "crashed"; 
    // Select input value. 

     if(responseMap.containsKey(input)) 
     { 
      // Response is already mapped to the word. 
      return responseMap.get(input); 
     } 
     else 
     { 
      // Look for a synonym of the word. 
      String synonym = synonymMap.get(input); 
      if(!synonym.equals(input) && responseMap.containsKey(synonym)) 
      { 
       // If a new value has been found that is a key.. 
       return responseMap.get(synonym); 
      } 
     } 
     // If no response, set default response. 
    input = "This is a default response"; 
    return input; 
} 

正如你所看到的,函數首先檢查密鑰是否存在。如果沒有,它會嘗試一個同義詞。如果該同義詞沒有通過測試,它將移動到底部的默認代碼,將輸入設置爲某個默認值,然後返回該代碼:)

+0

我可以看到你做了什麼,但是crash和crash都是返回響應的responseMap中的鍵。我有一個HashMap,其中某些鍵像「崩潰」和「崩潰」那樣返回相同的響應。我想創建一個新的HashMap,它將同義詞映射到responseMap中的唯一鍵(例如,映射「崩潰」,「崩潰」和「崩潰」到synonym中的「崩潰」)。 – DrJonesYu 2013-02-26 08:22:10

+1

就記憶而言,你的回答看起來很長。爲什麼當你可以使用上述方法時,多次持續存儲相同的響應,並且唯一的重複是在同義詞映射中? :) – christopher 2013-02-26 08:52:03

0

您可以使用第二張地圖。

第一張地圖將同義詞翻譯爲基本關鍵詞,而後者又可用於第二張地圖的答案。

這也允許在不擴大實際響應圖的情況下靈活地擴展同義詞。

此外,在這種情況下,您實際上可以使用另一種類型作爲answser地圖的關鍵字。它必須與同義詞映射的值類型相同。

這樣的話,你也可以使用

Map<String, YourEnum> and Map<YourEnum, String> 

與EnumMap的作爲的Map接口的。

+0

實際響應圖會放大嗎?據我所知,他加了3次相同的字符串常量。 – vikingsteve 2013-02-26 07:57:55

+0

不適用於實際響應。但是,您會在地圖的keySet中獲得另外兩個鍵。 – omilke 2013-02-26 08:01:07

+0

這就是我想要做的,但我不知道如何。我有一個HashMap,其中某些鍵像「崩潰」和「崩潰」那樣返回相同的響應。我想創建一個新的HashMap,它將同義詞映射到responseMap中的唯一鍵(例如,映射「崩潰」,「崩潰」和「崩潰」到synonym中的「崩潰」)。 – DrJonesYu 2013-02-26 08:20:21