我有一个 HashMap,其中某些键(例如“崩溃”和“崩溃”)返回相同的响应。我想创建一个新的 HashMap,将同义词映射到 responseMap 中的唯一键(例如,将 synonymMap 中的“crash”、“crashes”和“crashed”映射到“crash”)。
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();
}