public class FileCorrector extends Corrector
One way to get corrections for a misspelled word is to consult an external resource. This kind of Corrector uses a file that contains pairs of words on each line (a misspelled word and a correction for that misspelling) to generate corrections.
Modifier and Type | Class and Description |
---|---|
static class |
FileCorrector.FormatException
A special purpose exception class to indicate errors when reading the input for the
FileCorrector.
|
Constructor and Description |
---|
FileCorrector(java.io.Reader r)
Constructs a FileCorrector from the argued Reader.
|
Modifier and Type | Method and Description |
---|---|
java.util.Set<java.lang.String> |
getCorrections(java.lang.String wrong)
Returns a set of proposed corrections for an incorrectly spelled word.
|
static FileCorrector |
make(java.lang.String filename)
Constructs a FileCorrecotr from a file.
|
public FileCorrector(java.io.Reader r) throws java.io.IOException, FileCorrector.FormatException
Each line in the input should have a single comma that separates two parts in the form: misspelled_word,corrected_version
For example:
aligatur,alligator
baloon,balloon
inspite,in spite
who'ev,who've
ther,their
ther,there
The lines are not case-sensitive, so all of the following lines should function equivalently:
baloon,balloon
Baloon,balloon
Baloon,Balloon
BALOON,balloon
bAlOon,BALLOON
You should ignore any leading or trailing whitespace around the misspelled and corrected
parts of each line. Thus, the following lines should all be equivalent:
inspite,in spiteNote that spaces are allowed inside the corrected word. (In general, the FileCorrector is allowed to suggest Strings that are not words according to TokenScanner.)
inspite,in spite
inspite ,in spite
inspite , in spite
You should throw a FileCorrector.FormatException
if you encounter input that is
invalid. For example, the FileCorrector constructor should throw an exception if any of these
inputs are encountered:
,correct
wrong,
wrong correct
wrong,correct,
r
- The sequence of characters to parsejava.io.IOException
- If error while readingFileCorrector.FormatException
- If an invalid line is encounteredjava.lang.IllegalArgumentException
- If the provided reader is nullpublic static FileCorrector make(java.lang.String filename) throws java.io.IOException, FileCorrector.FormatException
filename
- Location of file from which to readjava.io.FileNotFoundException
- If the file does not existjava.io.IOException
- If error while readingFileCorrector.FormatException
- If an invalid line is encounteredpublic java.util.Set<java.lang.String> getCorrections(java.lang.String wrong)
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)