Package com.swabunga.spell.event
Class SpellChecker
- java.lang.Object
-
- com.swabunga.spell.event.SpellChecker
-
public class SpellChecker extends java.lang.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 likeSpellDictionaryHashMap
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
Fields Modifier and Type Field Description static int
SPELLCHECK_CANCEL
Flag indicating that the Spell Check completed due to user cancellationstatic int
SPELLCHECK_OK
Flag indicating that the Spell Check completed without any errors present
-
Constructor Summary
Constructors Constructor Description SpellChecker()
Constructs the SpellChecker.SpellChecker(SpellDictionary dictionary)
Constructs the SpellChecker.SpellChecker(SpellDictionary dictionary, int threshold)
Constructs the SpellChecker with a threshold
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addDictionary(SpellDictionary dictionary)
Accumulates a dictionary at the end of the dictionaries list used for looking up words.void
addSpellCheckListener(SpellCheckListener listener)
Adds a SpellCheckListener to the listeners list.void
addToDictionary(java.lang.String word)
Adds a word to the user dictionaryint
checkSpelling(WordTokenizer tokenizer)
This method is called to check the spelling of the words that are returned by the WordTokenizer.java.lang.String
checkString(java.lang.String text)
Deprecated.use checkSpelling(WordTokenizer)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.protected void
fireSpellCheckEvent(SpellCheckEvent event)
Fires off a spell check event to the listeners.Configuration
getConfiguration()
Supply the instance of the configuration holding the spell checking engine parameters.java.util.List
getSuggestions(java.lang.String word, int threshold)
Produces a list of suggested word after looking for suggestions in various dictionaries.void
ignoreAll(java.lang.String word)
Adds a word to the list of ignored wordsboolean
isCorrect(java.lang.String word)
Verifies if the word to analyze is contained in dictionaries.boolean
isIgnored(java.lang.String word)
Indicates if a word is in the list of ignored wordsstatic boolean
isINETWord(java.lang.String word)
Verifies if the word that is being spell checked contains an Internet address.void
removeSpellCheckListener(SpellCheckListener listener)
Removes a SpellCheckListener from the listeners list.void
reset()
This method clears the words that are currently being remembered asIgnore All
words andReplace All
words.void
setCache()
Activates a cache with the maximum number of entries set to 300void
setCache(int size)
Activates a cache with specified sizevoid
setUserDictionary(SpellDictionary dictionary)
Registers the user dictionary to which words are added.
-
-
-
Field Detail
-
SPELLCHECK_OK
public static final int SPELLCHECK_OK
Flag indicating that the Spell Check completed without any errors present- See Also:
- Constant Field Values
-
SPELLCHECK_CANCEL
public static final int SPELLCHECK_CANCEL
Flag indicating that the Spell Check completed due to user cancellation- See Also:
- Constant Field Values
-
-
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.
-
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.
-
getConfiguration
public Configuration getConfiguration()
Supply the instance of the configuration holding the spell checking engine parameters.- Returns:
- Current Configuration
-
addSpellCheckListener
public void addSpellCheckListener(SpellCheckListener listener)
Adds a SpellCheckListener to the listeners list.- Parameters:
listener
- The feature to be added to the SpellCheckListener attribute
-
removeSpellCheckListener
public void removeSpellCheckListener(SpellCheckListener listener)
Removes a SpellCheckListener from the listeners list.- Parameters:
listener
- The listener to be removed from the listeners list.
-
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.
-
reset
public void reset()
This method clears the words that are currently being remembered asIgnore All
words andReplace All
words.
-
checkString
public java.lang.String checkString(java.lang.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
-
isINETWord
public static final boolean isINETWord(java.lang.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:- http://
- ftp://
- https://
- ftps://
- www.
- Parameters:
word
- The word to analyze for an Internet address.- Returns:
- true if this word looks like an Internet address.
-
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 Parameterevent
- 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
-
ignoreAll
public void ignoreAll(java.lang.String word)
Adds a word to the list of ignored words- Parameters:
word
- The text of the word to ignore
-
addToDictionary
public void addToDictionary(java.lang.String word)
Adds a word to the user dictionary- Parameters:
word
- The text of the word to add
-
isIgnored
public boolean isIgnored(java.lang.String word)
Indicates if a word is in the list of ignored words- Parameters:
word
- The text of the word check
-
isCorrect
public boolean isCorrect(java.lang.String word)
Verifies if the word to analyze is contained in dictionaries. The order of dictionary lookup is:- The default user dictionary or the one set through
setUserDictionary(com.swabunga.spell.engine.SpellDictionary)
- The dictionary specified at construction time, if any.
- Any dictionary in the order they were added through
addDictionary(com.swabunga.spell.engine.SpellDictionary)
- Parameters:
word
- The word to verify that it's spelling is known.- Returns:
- true if the word is in a dictionary.
- The default user dictionary or the one set through
-
getSuggestions
public java.util.List getSuggestions(java.lang.String word, int threshold)
Produces a list of suggested word after looking for suggestions in various dictionaries. The order of dictionary lookup is:- The default user dictionary or the one set through
setUserDictionary(com.swabunga.spell.engine.SpellDictionary)
- The dictionary specified at construction time, if any.
- Any dictionary in the order they were added through
addDictionary(com.swabunga.spell.engine.SpellDictionary)
- Parameters:
word
- The word for which we want to gather suggestionsthreshold
- the cost value above which any suggestions are thrown away- Returns:
- the list of words suggested
- The default user dictionary or the one set through
-
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)
-
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.
-
-