I am trying to implement google’s “did you mean” feature in java.
I found some code on internet saying it works properly, it gets me an error though when trying to run it. I think it has to do with the directory creation, which is the only part of the code I do not exactly understand.
Here is the code, could you give me some help on what’s wrong?
Thanks in advance!
public static void main(String[] args) throws Exception {
File dir = new File("C:/Users/Lala");
Directory directory = FSDirectory.open(dir);
SpellChecker spellChecker = new SpellChecker(directory);
spellChecker.indexDictionary(
new PlainTextDictionary(new File("fulldictionary00.txt")));
String wordForSuggestions = "hwllo";
int suggestionsNumber = 5;
String[] suggestions = spellChecker.
suggestSimilar(wordForSuggestions, suggestionsNumber);
if (suggestions!=null && suggestions.length>0) {
for (String word : suggestions) {
System.out.println("Did you mean:" + word);
}
}
else {
System.out.println("No suggestions found for word:"+wordForSuggestions);
}
}
The file fulldictionary00.txt is a plain text file in the right format.
The error I get though is at line 18:
SpellChecker spellChecker = new SpellChecker(directory);
so it has to do with the directory creation.. I am pasting the error I get just in case any idea accures when you see it.
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/lucene/document/Fieldable at did_you_mean.main(did_you_mean.java:18) Caused by:
java.lang.ClassNotFoundException: org.apache.lucene.document.Fieldable
Just in case that someone else has the same problem, I found a way to solve it!
First of all, the problem seemed to be at version 4.0.0 of lucene, the one that I downloaded, because a class of a jar file was calling a class in another jar file that had been renamed in this version.
To fix the problem I just downloaded an older version (3.6.1), which demanded some changes to the existing code. In this version, spellChecker.IndexDictionary() function needs 3 arguments:
spellChecker.indexDictionary(new PlainTextDictionary(new File("fulldictionary00.txt")),config,false);config is an IndexWriterConfig object.
I hope this’ll help someone with the same problem!
@ppeterka thanks for the help anyway!