edu.northwestern.at.utils.spellcheck

## Class SimpleMindedSpellingChecker

- java.lang.Object
- - edu.northwestern.at.utils.spellcheck.SimpleMindedSpellingChecker

- All Implemented Interfaces:
    SpellingChecker

      public class SimpleMindedSpellingChecker
      extends java.lang.Object
      implements SpellingChecker

  SimpleMindedSpellingChecker -- A very simple minded spelling checker.

  Implements a very simple minded spelling checker using HashMaps to hold the dictionaries. The dictionaries are read as combined word lists and metaphone codes from text files into two hashmaps. One hashmap contains each word as a key and has the metaphone code for that word as a value. The second hashmap uses the metaphone values as keys and the list of words mapping to that metaphone as values. This allows presentation of suggested spellings for misspelled words. The list of suggestions may optionally be pruned by using a measure of the Levenstein distance between the original misspelling and each suggested spelling.

  Words beginning with a digit are assumed to be numbers and therefore spelled correctly.

  When creating a list of suggested replacements for a misspelled word, the first letter of the suggestions is capitalized if the first letter of the original misspelled word is capitalized.

  This class is not intended for production use because of the time needed to load and save dictionaries and the amount of memory used to hold the dictionaries in memory. This class serves as a reference implementation for the SpellingChecker interface as well as a testbed for applications needing a spelling checker during development.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `SimpleMindedSpellingChecker()` Create spelling checked without loading any dictionaries.
    `SimpleMindedSpellingChecker(SpellingDictionary globalDictionary)` Create spelling checker specifying global dictionary.
    `SimpleMindedSpellingChecker(SpellingDictionary globalDictionary, SpellingDictionary localDictionary)` Create spelling checker specifying global and local dictionaries.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `boolean` | `addWordToGlobalDictionary(java.lang.String word)` Add a word to the global dictionary.
    `boolean` | `addWordToIgnoreList(java.lang.String word)` Add word to ignore list.
    `boolean` | `addWordToLocalDictionary(java.lang.String word)` Add a word to the local dictionary.
    `boolean` | `checkSpelling(java.lang.String word)` Check spelling of word.
    `void` | `emptyIgnoreList()` Empties the ignore list.
    `java.lang.String[]` | `suggest(java.lang.String word)` Suggest alternative words for misspelled item.
    `java.lang.String[]` | `suggest(java.lang.String word, boolean prune)` Suggest alternative words for misspelled item.
    `void` | `useGlobalDictionary(SpellingDictionary dictionary)` Select global dictionary to use to check spelling.
    `void` | `useLocalDictionary(SpellingDictionary dictionary)` Select local dictionary to use to check spelling.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### SimpleMindedSpellingChecker

          public SimpleMindedSpellingChecker(SpellingDictionary globalDictionary,
                                     SpellingDictionary localDictionary)

      Create spelling checker specifying global and local dictionaries.

    - #### SimpleMindedSpellingChecker

          public SimpleMindedSpellingChecker(SpellingDictionary globalDictionary)

      Create spelling checker specifying global dictionary.

    - #### SimpleMindedSpellingChecker

          public SimpleMindedSpellingChecker()

      Create spelling checked without loading any dictionaries.

  - ### Method Detail

    - #### useGlobalDictionary

          public void useGlobalDictionary(SpellingDictionary dictionary)

      Select global dictionary to use to check spelling.
      Specified by:
        `useGlobalDictionary` in interface `SpellingChecker`
      Parameters:
        `dictionary` - Identifies dictionary. The dictionary class must implement the SpellingDictionary interface.

        The global dictionary is usually shared among many users. Typically it is created by a system administrator as a shareable resource.

    - #### useLocalDictionary

          public void useLocalDictionary(SpellingDictionary dictionary)

      Select local dictionary to use to check spelling.
      Specified by:
        `useLocalDictionary` in interface `SpellingChecker`
      Parameters:
        `dictionary` - The dictionary to use. If the name is null, no local dictionary is used. The dictionary class must implement the SpellingDictionary interface.

        The local dictionary is usually limited to access by a single individual. The local dictionary generally contains words added by an individual while checking the spelling of one or more documents.

    - #### checkSpelling

          public boolean checkSpelling(java.lang.String word)

      Check spelling of word.
      Specified by:
        `checkSpelling` in interface `SpellingChecker`
      Parameters:
        `word` - The word whose spelling should be checked.
      Returns:
        True if the word was found in the global or local dictionaries.

    - #### suggest

          public java.lang.String[] suggest(java.lang.String word,
                                   boolean prune)

      Suggest alternative words for misspelled item.
      Parameters:
        `word` - The misspelled word for which possible alternatives are desired.
        `prune` - True to prune suggestions using Levenstein distance.
      Returns:
        Sorted array of correctly spelled words which are similar in some sense to the misspelled word.

        The suggested words are typically words which are similar in sound or spelling to the misspelled word. If the misspelled word begins with a capital letter, the suggestions are also capitalized to match.

    - #### suggest

          public java.lang.String[] suggest(java.lang.String word)

      Suggest alternative words for misspelled item.
      Specified by:
        `suggest` in interface `SpellingChecker`
      Parameters:
        `word` - The misspelled word for which possible alternatives are desired.

        The suggested words are typically words which are similar in sound or spelling to the misspelled word.

      Returns:
        String array of correctly spelled words which are similar in some sense to the misspelled word.

        The suggested words are typically words which are similar in sound or spelling to the misspelled word. For English, an implementation might return all words with the same soundex or metaphone code as the misspelled word.

    - #### addWordToLocalDictionary

          public boolean addWordToLocalDictionary(java.lang.String word)

      Add a word to the local dictionary.
      Specified by:
        `addWordToLocalDictionary` in interface `SpellingChecker`
      Parameters:
        `word` - The word to add to the local dictionary.
      Returns:
        True if word added successfully.

    - #### addWordToGlobalDictionary

          public boolean addWordToGlobalDictionary(java.lang.String word)

      Add a word to the global dictionary.
      Specified by:
        `addWordToGlobalDictionary` in interface `SpellingChecker`
      Parameters:
        `word` - The word to add to the global dictionary.
      Returns:
        True if word added successfully.

        Typically this function is retricted to system administrators.

    - #### addWordToIgnoreList

          public boolean addWordToIgnoreList(java.lang.String word)

      Add word to ignore list.
      Specified by:
        `addWordToIgnoreList` in interface `SpellingChecker`
      Parameters:
        `word` - Word to ignore.
      Returns:
        True if word successfully added to ignore list.

        The ignore list contains words which an individual marks as ignorable while checking the spelling of one of more documents. The ignore list is transient. If an ignored word is to persist across multiple spelling check sessions, the word should be added to the local dictionary.

    - #### emptyIgnoreList

          public void emptyIgnoreList()

      Empties the ignore list.

      Removes all words from the ignore list. Typically this is invoked at the start of a spelling checker session for a new document.

      Specified by:
        `emptyIgnoreList` in interface `SpellingChecker`