Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Check if a string is in a dictionary

I have a language dictionary (i.e. english,italian,etc...), that essentially is a file with one word on every line.

Now i want to create a class with a method that given a string in input check if that string exists into that dictionary.

My idea is that the method return a boolean value. In pseudocode:

boolean checkWord(String s){
    if(StringIsInDictionary) return true;
    return false
}

What should be the best way to implement that feature?

Consider that the file will contain ~65000 words.

like image 512
Ivan Avatar asked Dec 20 '22 09:12

Ivan


2 Answers

Read the dictionary into a Set<String> (for example, HashSet<String>), and then use set.contains(word).

like image 89
NPE Avatar answered Jan 08 '23 13:01

NPE


For a space and time efficent solution (like you might use on a smartphone), consider a bloom filter. Then you won't need to store the dictionary on the phone, and checking that a string is in a dictionary will be very fast. Note that a bloom filter may return a false positive, but you can tune it to reduce that risk.

There are several open-source Java implementations of bloom filters out there. One is here https://github.com/magnuss/java-bloomfilter.

like image 22
lreeder Avatar answered Jan 08 '23 14:01

lreeder