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 misspelled and corrected words to generate suggestions.
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 an instance from the supplied 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 incorrect word.
|
static FileCorrector |
make(java.lang.String filename)
Construct a FileCorrector 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
- for an io error while readingFileCorrector.FormatException
- for an invalid linejava.lang.IllegalArgumentException
- if the provided reader is nullpublic static FileCorrector make(java.lang.String filename) throws java.io.IOException, FileCorrector.FormatException
filename
- of file to read fromjava.io.IOException
- if error while readingFileCorrector.FormatException
- for an invalid linejava.io.FileNotFoundException
- if file cannot be openedpublic java.util.Set<java.lang.String> getCorrections(java.lang.String wrong)
See the super class for more information.
getCorrections
in class Corrector
wrong
- the misspelled word.java.lang.IllegalArgumentException
- if the input is not a valid word
(i.e. only composed of letters and/or apostrophes)