Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

please answer in java only, thank you Implement a spelling checker by using a ha

ID: 3683876 • Letter: P

Question

please answer in java only, thank you

Implement a spelling checker by using a hash table. Assume thet the dictionary comes from two sources: an existing large dictionary and a second file containing a personal dictionary. Output all misspelled words and the line numbers in which they occur. Also, for each misspelled word, list any words in the large dictionary thet are obtainable by applying any of the following rules: a) Add one character. b) Remove one character. c) Exchange adjacent characters.

Explanation / Answer

public class SpellChecker {
private static String stringInput;
private static String[] checkThis;
public static HashSet dictionary;
public static void main(String[] args) {
setup();
}
public static void setup(){
int tableSIZE=59000;
dictionary = new HashSet(tableSIZE);
try {
  
BufferedReader bufferedReader = new BufferedReader(new FileReader("./dictionary.txt"));
String line = null;
while((line = bufferedReader.readLine()) != null) {
dictionary.add(line);
}
prompt();
bufferedReader.close();
}
catch(FileNotFoundException ex) {
ex.printStackTrace();   
}
catch(IOException ex) {
ex.printStackTrace();
}
}

public static void prompt(){
System.out.println("Type a number from below: ");
System.out.println("1. Auto Generate Test 2.Manual Input 3.Exit");
Scanner theLine = new Scanner(System.in);
int choice = theLine.nextInt();
if(choice==1) autoTest();
else if(choice==2) startwInput();
else if (choice==3) System.exit(0);
else System.out.println("Invalid Input. Exiting.");
}
  
public static void startwInput(){
System.out.println("Spell Checker by C. Austria Please enter text to check: ");
Scanner theLine = new Scanner(System.in);
stringInput = theLine.nextLine();
System.out.print(" You have entered this text: "+stringInput+" Initiating Check...");
WordFinder grammarNazi = new WordFinder();
splitString(removePunctuation(stringInput));
grammarNazi.initialCheck(checkThis);
}
public static void autoTest(){
System.out.println("Spell Checker by C. Austria This sentence is being tested: The dog foud my hom. And m ct hisse xdgfchv!@# ");
WordFinder grammarNazi = new WordFinder();
splitString(removePunctuation("The dog foud my hom. And m ct hisse xdgfchv!@# "));//turn String line to String[]
grammarNazi.initialCheck(checkThis);
}


public static void printDictionary(BufferedReader bufferedReader){
String line = null;
try{
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
}catch(FileNotFoundException ex) {
ex.printStackTrace();
}
catch(IOException ex) {
ex.printStackTrace();
}
}

public static void splitString(String sentence){
checkThis = sentence.split(" ");
}
public static String removePunctuation(String sentence){
String newSentence;
newSentence = sentence.toLowerCase().replaceAll("[^a-zA-Z\s]", "").replaceAll("\s+", " ");
return newSentence;
}
}

This class checks for misspellings

public class WordFinder extends SpellChecker{
private int wordsLength;
private List<String> wrongWords = new ArrayList<String>();
public void initialCheck(String[] words){
wordsLength=words.length;

System.out.println();
for(int i=0;i<wordsLength;i++){
if(!dictionary.contains(words[i])) wrongWords.add(words[i]);
} if (!wrongWords.isEmpty()) {
System.out.println("Mistakes have been made!");
printIncorrect();
}
if (wrongWords.isEmpty()) {
System.out.println(" Move along. End of Program.");
}
}
public void printIncorrect(){
   System.out.print("These words [ ");
for (String wrongWord : wrongWords) {
System.out.print(wrongWord + " ");
}
System.out.println("]seems incorrect. ");
suggest();
}
public void suggest(){
MisSpell test = new MisSpell();
while(!wrongWords.isEmpty()&&test.possibilities.size()<=5){
String wordCheck=wrongWords.remove(0);
test.generateMispellings(wordCheck);
if(test.possibilities.size()>=0) test.print(test.possibilities);
}
}
public void manualWordLookup(){
System.out.print("Enter 'ext' to exit. ");
Scanner line = new Scanner(System.in);
String look=line.nextLine();
do{
if(dictionary.contains(look)) System.out.print(look+" is valid ");
else System.out.print(look+" is invalid ");
look=line.nextLine();
}while (!look.equals("ext"));
}
   }

public class MisSpell extends SpellChecker{
public List<String> possibilities = new ArrayList<String>();
private List<String> tempHolder = new ArrayList<String>();
private int Ldistance=0;
private String wrongWord;
  
public void generateMispellings(String wordCheck){
wrongWord=wordCheck;
try{
concatFL(wordCheck);
concatLL(wordCheck);
replaceFL(wordCheck);
replaceLL(wordCheck);
deleteFL(wordCheck);
deleteLL(wordCheck);
pluralize(wordCheck);
transposition(wordCheck);
}catch(StringIndexOutOfBoundsException e){
System.out.println();
}catch(ArrayIndexOutOfBoundsException e){
System.out.println();
}


}


public void concatFL(String word){
char cur;
String tempWord="";
   for(int i=97;i<123;i++){
cur=(char)i;
tempWord+=cur;
tempWord=tempWord.concat(word);
checkDict(tempWord);
tempWord="";
}
}

  
public void concatLL(String word){
char cur;
String tempWord="";
for(int i=123;i>97;i--){
cur=(char)i;
tempWord=tempWord.concat(word);
tempWord+=cur;
checkDict(tempWord);
tempWord="";
}
}
public void replaceFL(String word){
char cur;
String tempWord="";
for(int i=97;i<123;i++){
cur=(char)i;
tempWord=cur+word.substring(1,word.length());
checkDict(tempWord);
tempWord="";
}
}
public void replaceLL(String word){
char cur;
String tempWord="";
for(int i=97;i<123;i++){
cur=(char)i;
tempWord=word.substring(0,word.length()-1)+cur;
checkDict(tempWord);
tempWord="";
}
}
public void deleteFL(String word){
String tempWord=word.substring(1,word.length()-1);
checkDict(tempWord);
}
public void deleteLL(String word){
String tempWord=word.substring(0,word.length()-1);
checkDict(tempWord);
}
public void pluralize(String word){
String tempWord=word+"s";
checkDict(tempWord);
}
public void checkDict(String word){
if(dictionary.contains(word)){//check to see if tempWord is in dictionary
if(!possibilities.contains(word)) possibilities.add(word);
}
}
public void transposition(String word){
wrongWord=word;
int wordLen=word.length();
String[] mixer = new String[wordLen];
for(int i=0;i<wordLen;i++){
mixer [i]=word.substring(i,i+1);
}
shift(mixer);
}
public void shift(String[] mixer){
System.out.println();
String wordValue="";
for(int i=0;i<=tempHolder.size();i++){
resetHelper(tempHolder);
transposeHelper(mixer);
String wordFirstValue=tempHolder.remove(i);
for(int j=0;j<tempHolder.size();j++){
int inttemp=0;
String temp;
while(inttemp<j){
temp=tempHolder.remove(inttemp);
tempHolder.add(temp);
wordValue+=wordFirstValue+printWord(tempHolder);
inttemp++;
if(dictionary.contains(wordValue)) if(!possibilities.contains(wordValue)) possibilities.add(wordValue);
wordValue="";
}
}
}
}
public void transposeHelper(String[] wordMix){
for(int i=0;i<wordMix.length;i++){
tempHolder.add(wordMix[i]);
}
}
public void resetHelper(List<String> thisList){
while(!thisList.isEmpty()) thisList.remove(0);
}


public void print(List<String> listPrint){
if (possibilities.isEmpty()) {
System.out.print("Can't seem to find any related words for "+wrongWord);
return;
}
System.out.println("Maybe you meant these for "+wrongWord+": ");
System.out.printf("%s", listPrint);
resetHelper(possibilities);
}
public String printWord(List<String> listPrint){
Object[] suggests = listPrint.toArray();
String theWord="";
for(Object word: suggests){
theWord+=word;
}
return theWord;
}
}