org.apache.lucene.search.spell

Class SpellChecker


public class SpellChecker
extends Object

Spell Checker class (Main class)
(initially inspired by the David Spencer code).

Example Usage:

  SpellChecker spellchecker = new SpellChecker(spellIndexDirectory);
  // To index a field of a user index:
  spellchecker.indexDictionary(new LuceneDictionary(my_lucene_reader, a_field));
  // To index a file containing words:
  spellchecker.indexDictionary(new PlainTextDictionary(new File("myfile.txt")));
  String[] suggestions = spellchecker.suggestSimilar("misspelt", 5);
 
Version:
1.0
Author:
Nicolas Maisonneuve

Field Summary

static String
F_WORD
Field name for each word in the ngram index.

Constructor Summary

SpellChecker(Directory gramIndex)

Method Summary

void
clearIndex()
boolean
exist(String word)
Check whether the word exists in the index.
protected void
finalize()
void
indexDictionary(Dictionary dict)
Index a Dictionary
void
setAccuraty(float min)
Set the accuracy 0 < min < 1; default 0.5
void
setSpellIndex(Directory spellindex)
String[]
suggestSimilar(String word, int num_sug)
Suggest similar words
String[]
suggestSimilar(String word, int num_sug, IndexReader ir, String field, boolean morePopular)
Suggest similar words (restricted or not to a field of a user index)

Field Details

F_WORD

public static final String F_WORD
Field name for each word in the ngram index.

Constructor Details

SpellChecker

public SpellChecker(Directory gramIndex)

Method Details

clearIndex

public void clearIndex()
            throws IOException

exist

public boolean exist(String word)
            throws IOException
Check whether the word exists in the index.
Parameters:
word - String
Returns:
true iff the word exists in the index

finalize

protected void finalize()
            throws Throwable

indexDictionary

public void indexDictionary(Dictionary dict)
            throws IOException
Index a Dictionary
Parameters:
dict - the dictionary to index

setAccuraty

public void setAccuraty(float min)
Set the accuracy 0 < min < 1; default 0.5

setSpellIndex

public void setSpellIndex(Directory spellindex)

suggestSimilar

public String[] suggestSimilar(String word,
                               int num_sug)
            throws IOException
Suggest similar words
Parameters:
word - String the word you want a spell check done on
num_sug - int the number of suggest words
Returns:
String[]

suggestSimilar

public String[] suggestSimilar(String word,
                               int num_sug,
                               IndexReader ir,
                               String field,
                               boolean morePopular)
            throws IOException
Suggest similar words (restricted or not to a field of a user index)
Parameters:
word - String the word you want a spell check done on
num_sug - int the number of suggest words
ir - the indexReader of the user index (can be null see field param)
field - String the field of the user index: if field is not null, the suggested words are restricted to the words present in this field.
morePopular - boolean return only the suggest words that are more frequent than the searched word (only if restricted mode = (indexReader!=null and field!=null)
Returns:
String[] the sorted list of the suggest words with this 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

Copyright © 2000-2007 Apache Software Foundation. All Rights Reserved.