public class SimpleMindedSpellingChecker extends java.lang.Object implements SpellingChecker
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 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.
|
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.
|
public SimpleMindedSpellingChecker(SpellingDictionary globalDictionary, SpellingDictionary localDictionary)
public SimpleMindedSpellingChecker(SpellingDictionary globalDictionary)
public SimpleMindedSpellingChecker()
public void useGlobalDictionary(SpellingDictionary dictionary)
useGlobalDictionary
in interface SpellingChecker
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.
public void useLocalDictionary(SpellingDictionary dictionary)
useLocalDictionary
in interface SpellingChecker
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.
public boolean checkSpelling(java.lang.String word)
checkSpelling
in interface SpellingChecker
word
- The word whose spelling should be checked.public java.lang.String[] suggest(java.lang.String word, boolean prune)
word
- The misspelled word for which
possible alternatives are desired.prune
- True to prune suggestions using
Levenstein distance.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.
public java.lang.String[] suggest(java.lang.String word)
suggest
in interface SpellingChecker
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.
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.
public boolean addWordToLocalDictionary(java.lang.String word)
addWordToLocalDictionary
in interface SpellingChecker
word
- The word to add to the local dictionary.public boolean addWordToGlobalDictionary(java.lang.String word)
addWordToGlobalDictionary
in interface SpellingChecker
word
- The word to add to the global dictionary.Typically this function is retricted to system administrators.
public boolean addWordToIgnoreList(java.lang.String word)
addWordToIgnoreList
in interface SpellingChecker
word
- Word to ignore.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.
public void emptyIgnoreList()
Removes all words from the ignore list. Typically this is invoked at the start of a spelling checker session for a new document.
emptyIgnoreList
in interface SpellingChecker