Index: contrib/spellchecker/src/java/org/apache/lucene/search/spell/SpellChecker.java =================================================================== --- contrib/spellchecker/src/java/org/apache/lucene/search/spell/SpellChecker.java (revision 634765) +++ contrib/spellchecker/src/java/org/apache/lucene/search/spell/SpellChecker.java Fri Mar 07 19:00:56 CET 2008 @@ -139,7 +139,7 @@ /** * Suggest similar words (optionally restricted to a field of an index). - * + * *

As the Lucene similarity that is used to fetch the most relevant n-grammed terms * is not the same as the edit distance strategy used to calculate the best * matching spell-checked word from the hits that Lucene found, one usually has @@ -162,7 +162,36 @@ */ public String[] suggestSimilar(String word, int numSug, IndexReader ir, String field, boolean morePopular) throws IOException { + return suggestSimilar(word, numSug, ir, field, morePopular, false); + } + /** + * Suggest similar words (optionally restricted to a field of an index). + * + *

As the Lucene similarity that is used to fetch the most relevant n-grammed terms + * is not the same as the edit distance strategy used to calculate the best + * matching spell-checked word from the hits that Lucene found, one usually has + * to retrieve a couple of numSug's in order to get the true best match. + * + *

I.e. if numSug == 1, don't count on that suggestion being the best one. + * Thus, you should set this value to at least 5 for a good suggestion. + * + * @param word the word you want a spell check done on. see parameter canSuggestSelf + * @param numSug the number of suggested words + * @param ir the indexReader of the user index (can be null see field param) + * @param field the field of the user index: if field is not null, the suggested + * words are restricted to the words present in this field. + * @param morePopular return only the suggest words that are more frequent than the searched word + * (only if restricted mode = (indexReader!=null and field!=null) + * @param canSuggestSelf parameter word can be a part of the results of this method if this is true. + * @throws IOException + * @return String[] the sorted list of the suggest words with these 2 criteria: + * first criteria: the edit distance, second criteria (only if restricted mode): the popularity + * of the suggest words in the field of the user index + */ + public String[] suggestSimilar(String word, int numSug, IndexReader ir, + String field, boolean morePopular, boolean canSuggestSelf) throws IOException { + float min = this.minScore; final TRStringDistance sd = new TRStringDistance(word); final int lengthWord = word.length(); @@ -212,8 +241,7 @@ sugWord.string = hits.doc(i).get(F_WORD); // get orig word - // don't suggest a word for itself, that would be silly - if (sugWord.string.equals(word)) { + if (!canSuggestSelf && sugWord.string.equals(word)) { continue; } Index: contrib/spellchecker/src/test/org/apache/lucene/search/spell/TestSpellChecker.java =================================================================== --- contrib/spellchecker/src/test/org/apache/lucene/search/spell/TestSpellChecker.java (revision 634765) +++ contrib/spellchecker/src/test/org/apache/lucene/search/spell/TestSpellChecker.java Fri Mar 07 19:15:28 CET 2008 @@ -18,6 +18,7 @@ */ import java.io.IOException; +import java.util.Arrays; import junit.framework.TestCase; @@ -103,6 +104,15 @@ similar = spellChecker.suggestSimilar("fi", 2); assertEquals(0, similar.length); + // canSuggestSelf + similar = spellChecker.suggestSimilar("five", 2, null, null, false, false); + assertFalse("The suggestions is not supposed to contain the query word", Arrays.asList(similar).contains("five")); + + // !canSuggestSelf + similar = spellChecker.suggestSimilar("five", 2, null, null, false, true); + assertTrue("The suggestions is supposed to contain the query word", Arrays.asList(similar).contains("five")); + + // test restraint to a field similar = spellChecker.suggestSimilar("tousand", 10, r, "field1", false); assertEquals(0, similar.length); // there isn't the term thousand in the field field1 @@ -126,5 +136,5 @@ rs.close(); return num; } - + }