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.
Write a python program that will perform text analysis on an input text using all of...
Write a python program that will perform text analysis on an input text using all of the following steps: 1. Take the name of an input file as a command line argument. For example, your program might be called using python3 freq.py example1 to direct you to process a plain text file called example. More information is given on how to do this below. 2. Read the contents of the file into your program and divide the text into a...
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....
Please write a java program Instructions Your goal is to take N integer inputs from the...
Please write a java program Instructions Your goal is to take N integer inputs from the user -- N's value will be given by the user as well. You can assume the user provides a valid value for N, i.e., >0. Store the input integers in an array of size N in the order they are provided. These tasks should be done in the main() method. Create a new method called checkArray() that will take the previously created array as...
You can complete this assignment individually or as a group of two people. In this assignment...
You can complete this assignment individually or as a group of two people. In this assignment you will create a ​​Sorted Singly-Linked List​ that performs basic list operations using C++. This linked list should not allow duplicate elements. Elements of the list should be of type ‘ItemType’. ‘ItemType’ class should have a private integer variable with the name ‘value’. Elements in the linked list should be sorted in the ascending order according to this ‘value’ variable. You should create a...
Write in C++. Define a class called Text whose objects store lists of words. The class...
Write in C++. Define a class called Text whose objects store lists of words. The class Text will be just like the class StringVar except that the class Text will use a dy- namic array with base type StringVar rather than base type char and will mark the end of the array with a StringVar object consisting of a single blank, rather than using '\0' as the end marker. Intuitively, an object of the class Text represents some text consisting...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
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...
Objective: Write a Java program that will use a JComboBox from which the user will select...
Objective: Write a Java program that will use a JComboBox from which the user will select to convert a temperature from either Celsius to Fahrenheit, or Fahrenheit to Celsius. The user will enter a temperature in a text field from which the conversion calculation will be made. The converted temperature will be displayed in an uneditable text field with an appropriate label. Specifications Structure your file name and class name on the following pattern: The first three letters of your...
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 {...