Understanding Two or More Keys with Hashmaps

I have a problem with a hashmap. In my hashmap method, I want to have two or more keywords as a key, to oppose having one. For example, I want the user to enter a sentence containing two or more keywords, assuming that the “professor’s name” is a keyword. for instance

String[] temp3 = { "instructor","teacher","mentor" }; responses.put("professor name", temp3); 

And the user enters "what is the professor’s name", the code just hangs. But, on the other hand, if

  String[] temp3 = { "instructor","teacher","mentor" }; responses.put("professor", temp3); 

the code works. Therefore, I want to be able to enter some kind of sentence containing two or more keywords that are opposite to one. I will be grateful for any help.

Here is my hashmap method

  private static HashMap<String, String[]> populateSynonymMap() { String[] temp1 = { "instructor","teacher","mentor" }; responses.put("professor name", temp1); String[] temp2 = { "amount of test","test load","quantity of test" }; return responses; } 

and here is my main method

  public static void main(String args[]) throws ParseException, IOException { /* Initialization */ HashMap<String, String[]> synonymMap = new HashMap<String, String[]>(); synonymMap = populateSynonymMap(); // populate the map Scanner scanner = new Scanner(System.in); String input = null; /*End Initialization*/ System.out.println("Welcome To DataBase "); System.out.println("What would you like to know?"); System.out.print("> "); input = scanner.nextLine().toLowerCase(); String[] inputs = input.split(" "); for (String ing : inputs) { // iterate over each word of the sentence. boolean found = false; for (Map.Entry<String, String[]> entry : synonymMap.entrySet()) { String key = entry.getKey(); String[] value = entry.getValue(); if (key.equals(ing) || Arrays.asList(value).contains(ing)) { found = true; parseFile(entry.getKey());`` } break; } if (found) { break; } } } 

Assuming the ParseFile method works

+5
source share
2 answers

I would use a String containing a method for checking multiple words. This will solve your problem of checking a single word like "professor" or several words like "What is your professor name."

  System.out.print("> "); input = scanner.nextLine().toLowerCase(); for (Map.Entry<String, String[]> entry : synonymMap.entrySet()) { String key = entry.getKey(); String[] value = entry.getValue(); if (input.contains(key) || key.contains(input)) { System.out.println("Input found in database -> " +Arrays.asList(value)); break; } } 

Now it will make him work with one word or several words. But it works when words are entered in the same order. You can make it as complicated as you like.

For example, you can expand this to separate words from input and output sentences from sets, and then find how many words match. And I recommend you use some other data structure that makes this task natural. You may think that your database is organized by the relationship of words and synonyms, so you do not need to worry about cases where a value from one list of synonyms has another synonym, etc. And the search can be based only on words.

0
source

Use the type HashMap<List<String>,String[]> or HashMap<Set<String>,String[]> for cases when the order is executed and does not matter, respectively, instead of HashMap<String,String[]> .

In the first case, your keys are of type List<String> , and in the second case, they are of type Set<String> .

The best practice would be to wrap all keys using the Collections.unmodifiable * methods before using them as keys - if they are changed while in HashMap, your program behavior will be undefined.

0
source

All Articles