In: Computer Science
Write a Java program that uses the file EnglishWordList.txt which contains a collection of words in English (dictionary), to find the number of misspelled words in a story file called Alcott-little-261.txt. Please note that the dictionary words are all lower-cased and there are no punctuation symbols or numbers. Hence it is important that you eliminate anything other than a-z and A-Z and then lower case story words for valid comparisons against the WordList file. When you read each line of the story file as a String, use split() method to store the words per line in a String array. Then go through each word in the array to lowercase it and to eliminate all but a-z A-Z using Java regex - something like
String word = word.replaceAll(“[^a-zA-Z]”,””).toLowerCase();
Neither file can be linked, but one can use two random text files to complete this. the Alcott text file is a story, while EnglishWordList is all the words in the English dictionary. Basically this is spell check.
import java.util.*;
import java.io.*;
public class Main {
    public static void main(String...args) throws Exception {
        //input files
        File dictionaryFile = new File("EnglishWordList.txt");
        File storyFile = new File("Alcott-little-261.txt");
        //dictionary to store words
        HashSet<String> dictionary = new HashSet<>();
        
        //scanners to scan the files
        Scanner dictionaryScanner = new Scanner(dictionaryFile);
        Scanner storyScanner = new Scanner(storyFile);
        
        //list of mispelledwords
        ArrayList<String> mispelledwords = new ArrayList<>();
        
        while (dictionaryScanner.hasNext())
            dictionary.add(dictionaryScanner.next());
            
        while (storyScanner.hasNextLine()) {
            String line = storyScanner.nextLine();
            //skips new line character
            storyScanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
            String[] words = line.split(" ");
            for (String word : words) {
                word = word.replaceAll("[^a-zA-Z]","").toLowerCase();
                if (dictionary.contains(word))
                    continue;
                mispelledwords.add(word);
            }
        }
        System.out.println("No of mispelled words: "+mispelledwords.size());
        for (String word : mispelledwords)
            System.out.println(word);
    }
}