public class SwapCorrector extends Corrector
A common misspelling is accidentally swapping two adjacent letters, e.g. "with" -> "wiht". This Corrector is given a Dictionary of valid words and proposes corrections that are precisely one "swap" away from the incorrect word.
For example, if the incorrect word is "haet", then this Corrector might suggest "heat" and "hate", provided that both of these words occur in the dictionary.
Only swaps between adjacent letters are considered by this corrector.
Constructor and Description |
---|
SwapCorrector(Dictionary dict)
Constructs a SwapCorrector using the argued Dictionary.
|
Modifier and Type | Method and Description |
---|---|
java.util.Set<java.lang.String> |
getCorrections(java.lang.String wrong)
Suggests as corrections any words in the Dictionary (provided when this object is constructed)
that are one swap away from the input word.
|
public SwapCorrector(Dictionary dict)
dict
- The reference dictionary to use to look for corrections arising from letter swapsjava.lang.IllegalArgumentException
- If the argued Dictionary is nullpublic java.util.Set<java.lang.String> getCorrections(java.lang.String wrong)
For example, if the Dictionary contains the words "heat" and "hate", then both should be returned if the input wrong word is "haet".
If the provided word is already spelled correctly, then the empty set should be returned. The corrections should match the case of the input; the matchCase method is helpful here. For any input that is *not* a valid word, throw an IllegalArgumentException. A valid word is any sequence of letters (as determined by Character.isLetter) or apostrophes characters.
getCorrections
in class Corrector
wrong
- The misspelled wordjava.lang.IllegalArgumentException
- If the input is not a valid word (i.e. not composed of only
letters and/or apostrophes)