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

A spelling checker program reads an input file and prints all the words not foun

ID: 3818951 • Letter: A

Question

A spelling checker program reads an input file and prints all the words not found in some on-line dictionary. Suppose the dictionary contains 30,000 words and the input file is large, so that the algorithm can make only one pass through the input file. A simple strategy is to read the dictionary into a hash table and look for each input word as it is read. Assuming that an average word is 7 characters long and that it is possible to store words of L characters in L + 1 bytes (so space wastage is not a consideration), and assuming a quadratic probing hash table, how much space does the table require?

Explanation / Answer

public class SpellChecker {

    private static String stringInput; // input to check;

    private static String[] checkThis; // the stringInput turned array of words to check.

    public static HashSet dictionary; // the dictionary used

    /**

     * Main method.

     * @param args Argh!

     */

    public static void main(String[] args) {

        setup();

    }//end of main

    /**

     * This method loads the dictionary and initiates the checks for errors in a scanned input.

     */

    public static void setup(){

        int tableSIZE=59000;

        dictionary = new HashSet(tableSIZE);

        try {

            //System.out.print(System.getProperty("user.dir"));//just to find user's working directory;

            // I combined FileReader into the BufferReader statement

            //the file is located in edu.frostburg.cosc310

            BufferedReader bufferedReader = new BufferedReader(new FileReader("./dictionary.txt"));

            String line = null; // notes one line at a time

            while((line = bufferedReader.readLine()) != null) {

                dictionary.add(line);//add dictinary word in

            }

            prompt();

            bufferedReader.close(); //close file       

        }

        catch(FileNotFoundException ex) {

            ex.printStackTrace();//print error            

        }

        catch(IOException ex) {

            ex.printStackTrace();//print error

        }

    }//end of setUp

    /**

     * Just a prompt for auto generated tests or manual input test.

     */

    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(); // for manual input

        if(choice==1) autoTest();

        else if(choice==2) startwInput();

        else if (choice==3) System.exit(0);

        else System.out.println("Invalid Input. Exiting.");

    }

    /**

     * Manual input of sentence or words.

     */

    public static void startwInput(){

        //printDictionary(bufferedReader); // print dictionary

        System.out.println("Spell Checker by C. Austria Please enter text to check: ");

        Scanner theLine = new Scanner(System.in);

        stringInput = theLine.nextLine(); // for manual input

        System.out.print(" You have entered this text: "+stringInput+" Initiating Check...");

        /*------------------------------------------------------------------------------------------------------------*/

        //final long startTime = System.currentTimeMillis(); //speed test

        WordFinder grammarNazi = new WordFinder(); //instance of MisSpell

        splitString(removePunctuation(stringInput));//turn String line to String[]

        grammarNazi.initialCheck(checkThis);

        //final long endTime = System.currentTimeMillis();

        //System.out.println("Total execution time: " + (endTime - startTime) );

    }//end of startwInput

    /**

     * Generates a testing case.

     */

    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(); //instance of MisSpell

        splitString(removePunctuation("The dog foud my hom. And m ct hisse xdgfchv!@# "));//turn String line to String[]

        grammarNazi.initialCheck(checkThis);

    }//end of autoTest

    /**

     * This method prints the entire dictionary.

     * Was used in testing.

     * @param bufferedReader the dictionary file

     */

    public static void printDictionary(BufferedReader bufferedReader){

        String line = null; // notes one line at a time

        try{

            while((line = bufferedReader.readLine()) != null) {

                System.out.println(line);

            }

        }catch(FileNotFoundException ex) {

            ex.printStackTrace();//print error            

        }

        catch(IOException ex) {

            ex.printStackTrace();//print error

        }

    }//end of printDictionary

    /**

     * This methods splits the passed String and puts them into a String[]

     * @param sentence The sentence that needs editing.

     */

    public static void splitString(String sentence){

        // split the sentence in between " " aka spaces

        checkThis = sentence.split(" ");

    }//end of splitString

    /**

     * This method removes the punctuation and capitalization from a string.

     * @param sentence The sentence that needs editing.

     * @return the edited sentence.

     */

    public static String removePunctuation(String sentence){

        String newSentence; // the new sentence

        //remove evil punctuation and convert the whole line to lowercase

        newSentence = sentence.toLowerCase().replaceAll("[^a-zA-Z\s]", "").replaceAll("\s+", " ");

        return newSentence;

    }//end of removePunctuation

}

This class checks for misspellings

public class WordFinder extends SpellChecker{

    private int wordsLength;//length of String[] to check

    private List<String> wrongWords = new ArrayList<String>();//stores incorrect words

    /**

     * This methods checks the String[] for spelling errors.

     * Hashes each index in the String[] to see if it is in the dictionary HashSet

     * @param words String list of misspelled words to check

     */

    public void initialCheck(String[] words){

        wordsLength=words.length;

        System.out.println();

        for(int i=0;i<wordsLength;i++){

            //System.out.println("What I'm checking: "+words[i]); //test only

            if(!dictionary.contains(words[i])) wrongWords.add(words[i]);

        } //end for

        //manualWordLookup(); //for testing dictionary only

        if (!wrongWords.isEmpty()) {

            System.out.println("Mistakes have been made!");

            printIncorrect();

        } //end if

        if (wrongWords.isEmpty()) {

            System.out.println(" Move along. End of Program.");

        } //end if

    }//end of initialCheck

    /**

     * This method that prints the incorrect words in a String[] being checked and generates suggestions.

     */

    public void printIncorrect(){//delete this guy

        System.out.print("These words [ ");

        for (String wrongWord : wrongWords) {

            System.out.print(wrongWord + " ");

        }//end of for

        System.out.println("]seems incorrect. ");

        suggest();

    }//end of printIncorrect

    /**

     * This method gives suggestions to the user based on the wrong words she/he misspelled.

     */

    public void suggest(){

        MisSpell test = new MisSpell();

        while(!wrongWords.isEmpty()&&test.capabilities.size()<=5){

            String wordCheck=wrongWords.remove(0);

            test.generateMispellings(wordCheck);

            //if the capabilities size is greater than 0 then print suggestions

            if(test.capabilities.size()>=0) test.print(test.capabilities);

        }//end of while

    }//end of suggest

    /*ENTERING TEST ZONE*/

    /**

     * This allows a tester to look thorough the dictionary for words if they are valid; and for testing only.

     */

    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"));

    }//end of manualWordLookup

}

/**

* This is the main class responsible for generating misspellings.

* @author Catherine Austria

*/

public class MisSpell extends SpellChecker{

    public List<String> capabilities = new ArrayList<String>();//stores possible suggestions

    private List<String> tempHolder = new ArrayList<String>(); //telps for the transposition method

    private int Ldistance=0; // the distance related to the two words

    private String wrongWord;// the original wrong word.

    /**

     * Execute methods that make misspellings.

     * @param wordCheck the word being checked.

     */

    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();

        }

    }

    /**

     * This This technique concats the word behind each of the letter set letters and checks on the off chance that it is in the dictionary.

     * FL for first letter

     * @param word the word being manipulated.

     */

    public void concatFL(String word){

        char cur; // current character

        String tempWord=""; // stores temp made up word

        for(int i=97;i<123;i++){

            cur=(char)i;//assign ASCII from index i value

            tempWord+=cur;

            //if the word is in the dictionary then add it to the capabilities list

            tempWord=tempWord.concat(word); //add passed String to end of tempWord

            checkDict(tempWord); //check to see if in dictionary

            tempWord="";//reset temp word to contain nothing

        }//end of for

    }//end of concatFL

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote