I'm trying to code a spellchecker using a hashtable of dictionary words. I'm currently trying to compare words from a text document to a value in the hash table to see if it's spelled correctly, but no matter what I do, the containsValue() method always returns false even when I know it's not.
public class SpellChecker {
private Hashtable<Integer, Wrapper> words;
private ArrayList<Wrapper> parsedFile;
SpellChecker() {
words = new Hashtable<Integer, Wrapper>();
parsedFile = new ArrayList<Wrapper>();
}
public void createDict(String inputFile) throws IOException {
FileReader input = new FileReader(inputFile);
BufferedReader bufRead = new BufferedReader(input);
String myLine = null;
int i = 0;
while ((myLine = bufRead.readLine()) != null)
{
Wrapper my_line = new Wrapper(myLine);
words.put(i,my_line);
i++;
}
bufRead.close();
}
public ArrayList<Wrapper> readFile(String inputFile) throws IOException {
FileReader input = new FileReader(inputFile);
BufferedReader bufRead = new BufferedReader(input);
String myLine = null;
Wrapper[] array_file;
while ((myLine = bufRead.readLine()) != null)
{
String[] arrayFile = myLine.split(" ");
for (int i = 0; i < arrayFile.length; i++) {
array_file = new Wrapper[arrayFile.length];
arrayFile[i] = arrayFile[i].toLowerCase();
Wrapper my_line = new Wrapper(arrayFile[i]);
array_file[i] = my_line;
parsedFile.add(array_file[i]);
}
}
bufRead.close();
return parsedFile;
}
public ArrayList<Wrapper> getParsedFile(){
return parsedFile;
}
public ArrayList<String> checkMisspellings() {
ArrayList<String> misspelled = new ArrayList<String>();
for (int i = 0; i<parsedFile.size(); i++) {
if (!words.containsValue(parsedFile.get(i))) {
misspelled.add(parsedFile.get(i).backToString());
}
}
return misspelled;
}
}
I looked at some answers online that said it might be because containsValue() compares addresses, so I made a wrapper class for the String values but it still isn't working.
public class Wrapper {
public String x;
public Wrapper(String x){
this.x=x;
}
public String backToString() {
return x;
}
public boolean equals(Object o) {
if(o == null) return false;
if(!(o instanceof Wrapper)) return false;
final Wrapper p = (Wrapper) o;
if(p.x.equals(this.x)) return true;
return false;
}
@Override
public int hashCode() {
int hash = 3;
hash = 53 * hash + x.length();
return hash;
}
}
the misspelled arrayList is supposed to only contain words that aren't found in the hashtable, but it always just returns all the original words. What am I doing wrong?? Here's some sample input/output:
input: hellos my name Ann corral mlks please spell correct
output: [��hellos, my, name, ann, corral, mlks, please, spell, correct, ]
the text file I'm using for the dictionary looks something like this:
corralled
corralling
corrals
correct
correctable
corrected
correcter
correctest
correcting