Reputation: 49
I have a HashMap which certain keys like "crash" and "crashes" which return the same response. I want to create a new HashMap that maps synonyms to a unique key in the responseMap (for example, map "crash", "crashes" and "crashed" to "crash" in the synonymMap).
private void fillSynonymMap()
{
synonymMap.put("crash", "crash");
synonymMap.put("crashes", "crash");
synonymMap.put("crashed", "crash");
}
What I am stuck on is how to input these keys so that I can simplify the code below.
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();
}
Upvotes: 0
Views: 958
Reputation: 27356
After a little messing about I wrote a function that will look for a synonym, before returning a default message.
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;
}
As you can see the function first checks if the key exists. If it doesn't, it attempts a synonym. If that synonym doesn't pass the test, it will move to the default code at the bottom, which will set input to some default value and return that instead :)
Upvotes: 1
Reputation: 837
You could use a second map.
The first map translates the synonyms to the basic key, which in turn can be used for the second map with the answers.
This also allows flexible extension of synonyms without enlarging the actual response map.
Besides, in this case you can actually use another type as key for the answser map. It just has to be same as the value type of synonyms maps.
That way, you could also use
Map<String, YourEnum> and Map<YourEnum, String>
with EnumMap as implementation of the Map interface.
Upvotes: 0