Question

Your program, called should take two command line arguments, the dictionary, and the text that you...

Your program, called should take two command line arguments, the dictionary, and the text that you wish to spellcheck. Provide some sample text. Your program should be case insensitive (so you can toLower everything). Numbers and contractions are considered valid words. You may use the java HashTable or HashMap to implement this program; but it must use some kind of hash table.

Homework Answers

Answer #1


Program 1:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Scanner;
public class Dictionary {
   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("C:\\data\\words.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() {
       startwInput();
   }
  
   public static void startwInput() {
       System.out.println("Please enter text to check: ");
       Scanner theLine = new Scanner(System.in);
       stringInput = theLine.nextLine();
       System.out.print("\n This word is found: " + stringInput + "");
       WordFinder grammarNazi = new WordFinder();
       splitString(removePunctuation(stringInput));
       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;
   }
}
Program 2:
import java.util.ArrayList;
import java.util.List;
public class MisSpell extends Dictionary {
   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); // stores temp
                                                               // made up word
       checkDict(tempWord);
   }
   public void deleteLL(String word) {
       String tempWord = word.substring(0, word.length() - 1); // stores temp
                                                               // made up word
       checkDict(tempWord);
   }
   public void pluralize(String word) {
       String tempWord = word + "s";
       checkDict(tempWord);
   }
   public void checkDict(String word) {
       if (dictionary.contains(word)) {
           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 = "";
               } // end of while
           } // end of for
       } // end for
   }// end of shift
   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);
   }// end of resetHelper
   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);
   }// end of print
   public String printWord(List<String> listPrint) {
       Object[] suggests = listPrint.toArray();
       String theWord = "";
       for (Object word : suggests) {// form listPrint elements into a word
           theWord += word;
       }
       return theWord;
   }// end of printWord
}

Program 3:
import java.util.ArrayList;
import java.util.List;
public class WordFinder extends Dictionary {
   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();
       }
   }
   public void printIncorrect() {
       System.out.print("These words [ ");
       for (String wrongWord : wrongWords) {
           System.out.print(wrongWord + " ");
       }
       System.out.println("]seems incorrect.\n");
       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);
       }
   }
}

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In Java, Write a small program that gets some numbers as command line arguments and finds...
In Java, Write a small program that gets some numbers as command line arguments and finds the minimum of those numbers. You can assume that the user will provide some command line arguments when running the program (so you don’t have to worry about what to do if the user doesn’t provide any arguments -- that won’t happen). Also, you can assume that all the command line arguments will be floating-point numbers, i.e., numbers with a decimal point, like 8.25.
C LANGUAGE CODE WITH COMMAND-LINE ARGUMENTS (NO SCANF TO BE USED ) Q]. Write a program...
C LANGUAGE CODE WITH COMMAND-LINE ARGUMENTS (NO SCANF TO BE USED ) Q]. Write a program that displays all the prime numbers in the given array with the following constraint. Constraint: Only those prime numbers should be displayed whose location is a composite number. Although you may have several prime numbers in the array, only those prime numbers should be displayed which are stored at non-prime locations. Remember that the first position in an array corresponds to the location/index 0....
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in Lab 2 to obtain a diameter value from the user and compute the volume of a sphere (we assumed that to be the shape of a balloon) in a new program, and implement the following restriction on the user’s input: the user should enter a value for the diameter which is at least 8 inches but not larger than 60 inches. Using an if-else...
Take the Java program Pretty.java and convert it to the equivalent C program. You can use...
Take the Java program Pretty.java and convert it to the equivalent C program. You can use the file in.txt as sample input for your program. import java.io.*; import java.util.*; public class Pretty { public static final int LINE_SIZE = 50; public static void main(String[] parms) { String inputLine; int position = 1; Scanner fileIn = new Scanner(System.in); while (fileIn.hasNextLine()) { inputLine = fileIn.nextLine(); if (inputLine.equals("")) { if (position > 1) { System.out.println(); } System.out.println(); position = 1; } else {...
** Language Used : Python ** PART 2 : Create a list of unique words This...
** Language Used : Python ** PART 2 : Create a list of unique words This part of the project involves creating a function that will manage a List of unique strings. The function is passed a string and a list as arguments. It passes a list back. The function to add a word to a List if word does not exist in the List. If the word does exist in the List, the function does nothing. Create a test...
1. Vim commands: a. How do you auto indent your program? b. Explain what the following...
1. Vim commands: a. How do you auto indent your program? b. Explain what the following commands do: dd, y3, p, :set cindent (1 pt) VIM exercises These exercises on the computer need to be repeated by each student in the pair. This is to ensure that both students understand how to get around in Linux!!! For this part of the lab, you will create a .vimrc file that will help you develop your C++ programs using VIM. First, we...
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question....
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question. Thank you! Here’s the contents of a file called example.cpp: // example.cpp #include "LinkedList.h" #include <iostream> #include <string> using namespace std; int main() { cout << "Please enter some words (ctrl-d to stop):\n"; LinkedList lst; int count = 0; string s; while (cin >> s) { count++; lst.add(remove_non_letters(s)); } // while cout << "\n" << count << " total words read in\n"; cout <<...
This laboratory assignment involves implementing a data structure called a map. A map associates objects called...
This laboratory assignment involves implementing a data structure called a map. A map associates objects called keys with other objects called values. It is implemented as a Java class that uses arrays internally. 1. Theory. A map is a set of key-value pairs. Each key is said to be associated with its corresponding value, so there is at most one pair in the set with a given key. You can perform the following operations on maps. You can test if...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Write a C++ program to demonstrate thread synchronization. Your main function should first create a file...
Write a C++ program to demonstrate thread synchronization. Your main function should first create a file called synch.txt. Then it will create two separate threads, Thread-A and Thread-B. Both threads will open synch.txt and write to it. Thread-A will write the numbers 1 - 26 twenty times in nested for loops then exit. In other words, print 1 - 26 over and over again on separate lines for at least 20 times. Thread-B will write the letters A - Z...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT