Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match a string (phrase) to a HashMap values quickly?

I have a HashMap whose key is id and whose value is text:

HashMap<String,String> textMap = new HashMap<String,String>();
textMap.put("id_1","She");
textMap.put("id_2","has");
textMap.put("id_3","a"); 
textMap.put("id_4","neck");
textMap.put("id_5","pain");
//.. more elements in textMap

I would like to find the related ids given a phrase, like 'neck pain', so in this case the result should be id_4 and id_5.

Can anybody suggest an efficient algorithm to match the phrase with ids?

I'm sorry I forgot to include an important condition. I have both the phrase and the offset of word, like "start_5_end_14", which means that 5 should be the start of "neck" given that the start of "She" is 0. However, what complicates the case is that the offset might not be correct.

like image 428
D.Q. Avatar asked Dec 06 '25 19:12

D.Q.


1 Answers

Maintain a reversed map in which you map the words to the id's. Then, traverse over your phrase and call reversedTextMap.get(word) for each word in the phrase.

like image 155
arshajii Avatar answered Dec 08 '25 09:12

arshajii



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!