Skip to content

Instantly share code, notes, and snippets.

@quinnzipse
Created December 13, 2019 01:07
Show Gist options
  • Save quinnzipse/14f976a607eb79959050f0851851cd71 to your computer and use it in GitHub Desktop.
Save quinnzipse/14f976a607eb79959050f0851851cd71 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class SpellCheck {
private String[] dictionary;
public SpellCheck(String[] dictionary) {
this.dictionary = dictionary;
}
public List<Misspelling> check(String fileName){
// Create a file object from the fileName.
File file = new File(fileName);
// Check to see if fileName exists
if(!file.exists()) return null;
// If it exists, open it.
try {
FileReader reader = new FileReader(file);
BufferedReader buffer = new BufferedReader(reader);
// Initialize a list of Misspelling
List<Misspelling> list = new ArrayList<Misspelling>();
// While there are words in the file,
while(buffer.ready()) {
// check to see if the word is spelled correctly
boolean misspelled = true;
String[] line = buffer.readLine().split(" ");
for(int x=0; x<line.length; x++) {
// Check to see if this word is in the established dictionary
for(int i=0; i<dictionary.length; i++) {
if(dictionary[i] == line[x]) misspelled = false;
}
if(misspelled) {
// If the word isn't in the dictionary, create or update a Misspelling object for it.
boolean exists = false;
for(int i=0; i<list.size(); i++) {
// Check if the misspelling exists.
if(list.get(i).getWord() == line[x]) {
exists = true;
list.get(i).addLineNum(x);
list.get(i).incrementFrequency();
break;
}
}
if(!exists) {
ArrayList<Integer> intList = new ArrayList<>();
intList.add(x);
// If the misspelling is new, create a new object for it
list.add(new Misspelling(line[x], 1, intList));
}
}
}
}
}
catch(FileNotFoundException e) {
}
catch(IOException e) {
}
// Return the list of Misspelling objects (dummy value for now)
return null;
}
// Extend the interface given on 6.1.1
private class Misspelling {
String word;
int frequency;
List<Integer> lines;
public Misspelling(String word, int frequency, List<Integer> lines) {
this.word = word;
this.frequency = frequency;
this.lines = lines;
}
public String getWord() {
return word;
}
public int getFrequency() {
return frequency;
}
public List<Integer> getLines(){
return lines;
}
public void addLineNum(int lineNum) {
lines.add(lineNum);
}
public void incrementFrequency() {
frequency++;
}
}
public static void main(String[] args) {
String[] dictionary = new String[] { "Hello", "Quinn", "It's" };
SpellCheck checker = new SpellCheck(dictionary);
// This needs a file name, since I don't have a file yet, it is set to null.
Misspelling[] flaggedWords = checker.check(null).toArray(new Misspelling[] {});
System.out.println("These are the words that were flagged as misspelled.");
System.out.println(flaggedWords);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment