com.swabunga.spell.event

Class SpellChecker

public class SpellChecker extends Object

This is the main class for spell checking (using the new event based spell checking).

By default, the class makes a user dictionary to accumulate added words. Since this user directory has no file assign to persist added words, they will be retained for the duration of the spell checker instance. If you set a user dictionary like SpellDictionaryHashMap to persist the added word, the user dictionary will have the possibility to grow and be available across differents invocations of the spell checker.

Author: Jason Height (jheight@chariot.net.au) 19 June 2002

Field Summary
static intSPELLCHECK_CANCEL
Flag indicating that the Spell Check completed due to user cancellation
static intSPELLCHECK_OK
Flag indicating that the Spell Check completed without any errors present
Constructor Summary
SpellChecker()
Constructs the SpellChecker.
SpellChecker(SpellDictionary dictionary)
Constructs the SpellChecker.
SpellChecker(SpellDictionary dictionary, int threshold)
Constructs the SpellChecker with a threshold
Method Summary
voidaddDictionary(SpellDictionary dictionary)
Accumulates a dictionary at the end of the dictionaries list used for looking up words.
voidaddSpellCheckListener(SpellCheckListener listener)
Adds a SpellCheckListener to the listeners list.
voidaddToDictionary(String word)
Adds a word to the user dictionary
intcheckSpelling(WordTokenizer tokenizer)
This method is called to check the spelling of the words that are returned by the WordTokenizer.
StringcheckString(String text)
Checks the text string.
protected booleanfireAndHandleEvent(WordTokenizer tokenizer, SpellCheckEvent event)
This method will fire the spell check event and then handle the event action that has been selected by the user.
protected voidfireSpellCheckEvent(SpellCheckEvent event)
Fires off a spell check event to the listeners.
ConfigurationgetConfiguration()
Supply the instance of the configuration holding the spell checking engine parameters.
ListgetSuggestions(String word, int threshold)
Produces a list of suggested word after looking for suggestions in various dictionaries.
voidignoreAll(String word)
Adds a word to the list of ignored words
booleanisCorrect(String word)
Verifies if the word to analyze is contained in dictionaries.
booleanisIgnored(String word)
Indicates if a word is in the list of ignored words
static booleanisINETWord(String word)
Verifies if the word that is being spell checked contains an Internet address.
voidremoveSpellCheckListener(SpellCheckListener listener)
Removes a SpellCheckListener from the listeners list.
voidreset()
This method clears the words that are currently being remembered as Ignore All words and Replace All words.
voidsetCache()
Activates a cache with the maximum number of entries set to 300
voidsetCache(int size)
Activates a cache with specified size
voidsetUserDictionary(SpellDictionary dictionary)
Registers the user dictionary to which words are added.

Field Detail

SPELLCHECK_CANCEL

public static final int SPELLCHECK_CANCEL
Flag indicating that the Spell Check completed due to user cancellation

SPELLCHECK_OK

public static final int SPELLCHECK_OK
Flag indicating that the Spell Check completed without any errors present

Constructor Detail

SpellChecker

public SpellChecker()
Constructs the SpellChecker.

SpellChecker

public SpellChecker(SpellDictionary dictionary)
Constructs the SpellChecker. The default threshold is used

Parameters: dictionary The dictionary used for looking up words.

SpellChecker

public SpellChecker(SpellDictionary dictionary, int threshold)
Constructs the SpellChecker with a threshold

Parameters: dictionary the dictionary used for looking up words. threshold the cost value above which any suggestions are thrown away

Method Detail

addDictionary

public void addDictionary(SpellDictionary dictionary)
Accumulates a dictionary at the end of the dictionaries list used for looking up words. Adding a dictionary give the flexibility to assign the base language dictionary, then a more technical, then...

Parameters: dictionary the dictionary to add at the end of the dictionary list.

addSpellCheckListener

public void addSpellCheckListener(SpellCheckListener listener)
Adds a SpellCheckListener to the listeners list.

Parameters: listener The feature to be added to the SpellCheckListener attribute

addToDictionary

public void addToDictionary(String word)
Adds a word to the user dictionary

Parameters: word The text of the word to add

checkSpelling

public final int checkSpelling(WordTokenizer tokenizer)
This method is called to check the spelling of the words that are returned by the WordTokenizer.

For each invalid word the action listeners will be informed with a new SpellCheckEvent.

Parameters: tokenizer The media containing the text to analyze.

Returns: Either SPELLCHECK_OK, SPELLCHECK_CANCEL or the number of errors found. The number of errors are those that are found BEFORE any corrections are made.

checkString

public String checkString(String text)

Deprecated: use checkSpelling(WordTokenizer)

Checks the text string.

Returns the corrected string.

Parameters: text The text that need to be spelled checked

Returns: The text after spell checking

fireAndHandleEvent

protected boolean fireAndHandleEvent(WordTokenizer tokenizer, SpellCheckEvent event)
This method will fire the spell check event and then handle the event action that has been selected by the user.

Parameters: tokenizer Description of the Parameter event The event to handle

Returns: Returns true if the event action is to cancel the current spell checking, false if the spell checking should continue

fireSpellCheckEvent

protected void fireSpellCheckEvent(SpellCheckEvent event)
Fires off a spell check event to the listeners.

Parameters: event The event that need to be processed by the spell checking system.

getConfiguration

public Configuration getConfiguration()
Supply the instance of the configuration holding the spell checking engine parameters.

Returns: Current Configuration

getSuggestions

public List getSuggestions(String word, int threshold)
Produces a list of suggested word after looking for suggestions in various dictionaries. The order of dictionary lookup is:

Parameters: word The word for which we want to gather suggestions threshold the cost value above which any suggestions are thrown away

Returns: the list of words suggested

ignoreAll

public void ignoreAll(String word)
Adds a word to the list of ignored words

Parameters: word The text of the word to ignore

isCorrect

public boolean isCorrect(String word)
Verifies if the word to analyze is contained in dictionaries. The order of dictionary lookup is:

Parameters: word The word to verify that it's spelling is known.

Returns: true if the word is in a dictionary.

isIgnored

public boolean isIgnored(String word)
Indicates if a word is in the list of ignored words

Parameters: word The text of the word check

isINETWord

public static final boolean isINETWord(String word)
Verifies if the word that is being spell checked contains an Internet address. The method look for typical protocol or the habitual string in the word: One limitation is that this method cannot currently recognize email addresses. Since the 'word' that is passed in, may in fact contain the rest of the document to be checked, it is not (yet!) a good idea to scan for the @ character.

Parameters: word The word to analyze for an Internet address.

Returns: true if this word looks like an Internet address.

removeSpellCheckListener

public void removeSpellCheckListener(SpellCheckListener listener)
Removes a SpellCheckListener from the listeners list.

Parameters: listener The listener to be removed from the listeners list.

reset

public void reset()
This method clears the words that are currently being remembered as Ignore All words and Replace All words.

setCache

public void setCache()
Activates a cache with the maximum number of entries set to 300

setCache

public void setCache(int size)
Activates a cache with specified size

Parameters: size - max. number of cache entries (0 to disable chache)

setUserDictionary

public void setUserDictionary(SpellDictionary dictionary)
Registers the user dictionary to which words are added.

Parameters: dictionary the dictionary to use when the user specify a new word to add.