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.
Read the dictionary into a Set<String>
(for example, HashSet<String>
), and then use set.contains(word)
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With