missrg
missrg

Reputation: 585

Trying to implement Google's "did you mean" feature in java

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 

Upvotes: 0

Views: 2968

Answers (3)

Bily
Bily

Reputation: 801

well,In lucene 4.0.0, spellchecker is put in a package named lucene-suggest-4.0.0.jar rather than lucene-spellchecker-XXX.jar

Upvotes: 1

missrg
missrg

Reputation: 585

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!

Upvotes: 0

ppeterka
ppeterka

Reputation: 20726

EDIT

As per OPs comment though, the error is that the JAR file of Lucene does not seem to be on the class path...

Original answer, without knowing about the error (might be useful to leave it here as it is)

You have to add content into the file specified... It doesn't work without that. Just do a bit of thinking: how should the program know which words are correct and which are not?

For your case of plain text dictionary file, you should use PlainTextDictionary

Dictionary represented by a text file.

Format allowed: 1 word per line:
word1
word2
word3

This page explains it a bit in context of a Lucene index:

Import: Adding Words to the Dictionary We can add the words coming from a Lucene Index (more precisely from a set of Lucene fields), and from a text file with a list of words.

Example: we can add all the keywords of a given Lucene field of my index.

SpellChecker spell= new SpellChecker(dictionaryDirectory);
spell.indexDictionary(new LuceneDictionary(my_luceneReader,my_fieldname));

Upvotes: 0

Related Questions