com.jalios.jcms.search.spell
Class SpellChecker

java.lang.Object
  extended by com.jalios.jcms.search.spell.SpellChecker

public class SpellChecker
extends java.lang.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 - $Revision: 27753 $
Author:
Nicolas Maisonneuve, Olivier Jaquemet

Nested Class Summary
 class SpellChecker.IndexSearcherWrapper
          For optimized used of the searcher, we keep it open as much as possible and delay its close only when it is replaced by a new one when modifying index.
 
Field Summary
static java.lang.String MAX_BUFFERED_DOCS
          Optionnal integer property name to define the maxBufferedDocs when writing in spellchecker index
static java.lang.String MAX_MERGE_DOCS
          Optionnal integer property name to define the maxMergeDocs when writing in spellchecker index
static java.lang.String MERGE_FACTOR
          Optionnal integer property name to define the mergeFactor when writing in spellchecker index
static java.lang.String REVISION
           
 
Constructor Summary
SpellChecker(org.apache.lucene.store.Directory gramIndex)
          Build a new SpellChecker.
 
Method Summary
 void clearIndex()
          Completely clear index from its content
 void closeAll()
           
 boolean exist(java.lang.String word)
          Check whether the word exists in the index.
 void indexDictionary(Dictionary dict)
          Index a Dictionary
 void renewReaderAndSeacher()
          Renew internal reader and searcher, call this method after index change.
 java.lang.String[] suggestSimilar(java.lang.String word, int num_sug)
          Suggest similar words
 java.lang.String[] suggestSimilar(java.lang.String word, int num_sug, org.apache.lucene.index.IndexReader ir, java.lang.String field, boolean morePopular)
          Suggest similar words (restricted or not to a field of a user index)
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REVISION

public static final java.lang.String REVISION
See Also:
Constant Field Values

MERGE_FACTOR

public static final java.lang.String MERGE_FACTOR
Optionnal integer property name to define the mergeFactor when writing in spellchecker index


MAX_MERGE_DOCS

public static final java.lang.String MAX_MERGE_DOCS
Optionnal integer property name to define the maxMergeDocs when writing in spellchecker index


MAX_BUFFERED_DOCS

public static final java.lang.String MAX_BUFFERED_DOCS
Optionnal integer property name to define the maxBufferedDocs when writing in spellchecker index

Constructor Detail

SpellChecker

public SpellChecker(org.apache.lucene.store.Directory gramIndex)
             throws java.io.IOException
Build a new SpellChecker.

Parameters:
gramIndex - the lucene directory in which to look and put indexed words (grams)
Throws:
java.io.IOException
Method Detail

closeAll

public void closeAll()

suggestSimilar

public java.lang.String[] suggestSimilar(java.lang.String word,
                                         int num_sug)
                                  throws java.io.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[]
Throws:
java.io.IOException

suggestSimilar

public java.lang.String[] suggestSimilar(java.lang.String word,
                                         int num_sug,
                                         org.apache.lucene.index.IndexReader ir,
                                         java.lang.String field,
                                         boolean morePopular)
                                  throws java.io.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
Throws:
java.io.IOException

exist

public boolean exist(java.lang.String word)
              throws java.io.IOException
Check whether the word exists in the index.

Parameters:
word - String
Returns:
true if the word exists in the index
Throws:
java.io.IOException

indexDictionary

public void indexDictionary(Dictionary dict)
                     throws java.io.IOException
Index a Dictionary

Parameters:
dict - the dictionary to index
Throws:
java.io.IOException

clearIndex

public void clearIndex()
                throws java.io.IOException
Completely clear index from its content

Throws:
java.io.IOException

renewReaderAndSeacher

public void renewReaderAndSeacher()
                           throws java.io.IOException
Renew internal reader and searcher, call this method after index change.

Throws:
java.io.IOException


Copyright © 2001-2010 Jalios SA. All Rights Reserved.