java - Understand two or more keys with Hashmaps -
i having issue hashmap. in hashmap method want have 2 or more keywords key, oppose having one. example want user input sentence containing 2 or more keywords assuming "professor name" keyword. example
string[] temp3 = { "instructor","teacher","mentor" }; responses.put("professor name", temp3);
and user enters "what professor name" code hangs. on other hand, if
string[] temp3 = { "instructor","teacher","mentor" }; responses.put("professor", temp3);
the code works. want able enter sentence containing 2 or more keywords oppose one. appreciate sort of assistance.
here 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 main method
public static void main(string args[]) throws parseexception, ioexception { /* initialization */ hashmap<string, string[]> synonymmap = new hashmap<string, string[]>(); synonymmap = populatesynonymmap(); // populate map scanner scanner = new scanner(system.in); string input = null; /*end initialization*/ system.out.println("welcome database "); system.out.println("what know?"); system.out.print("> "); input = scanner.nextline().tolowercase(); string[] inputs = input.split(" "); (string ing : inputs) { // iterate on each word of sentence. boolean found = false; (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 parsefile method works
i use string contains method check multiple words. solve problem of checking single word 'professor' or multiple words 'what professor name' problem.
system.out.print("> "); input = scanner.nextline().tolowercase(); (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 make work single word or multiple word. works when words entered in same order. can make elaborate like.
for example may extend separate words of input , output sentence sets , find how many words matches etc.and recommend use different data structure makes task natural looking. may think having database organized words , synonyms relation don't have worry cases value 1 synonym list has other synonym etc. , lookup can based on words.
Comments
Post a Comment