Class SpellChecker

java.lang.Object
com.swabunga.spell.event.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 Details

    • SPELLCHECK_OK

      public static final int SPELLCHECK_OK
      Flag indicating that the Spell Check completed without any errors present
      See Also:
    • SPELLCHECK_CANCEL

      public static final int SPELLCHECK_CANCEL
      Flag indicating that the Spell Check completed due to user cancellation
      See Also:
  • Constructor Details

    • 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 Details

    • 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 as Ignore All words and Replace All words.
    • 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
    • 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:
      • http://
      • ftp://
      • https://
      • ftps://
      • www.
      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.
    • 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
    • ignoreAll

      public void ignoreAll(String word)
      Adds a word to the list of ignored words
      Parameters:
      word - The text of the word to ignore
    • addToDictionary

      public void addToDictionary(String word)
      Adds a word to the user dictionary
      Parameters:
      word - The text of the word to add
    • 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
    • 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.
    • 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
    • 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.