Question

In: Computer Science

Write a Java program that uses the file EnglishWordList.txt which contains a collection of words in...

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.

Solutions

Expert Solution

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

    }
}



Related Solutions

Write a Java program that uses the file EnglishWordList.txt which contains a collection of words in...
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...
JAVA Write a program that checks the spelling of words in a document. This program uses...
JAVA Write a program that checks the spelling of words in a document. This program uses two text files: A dictionary file containing all known correctly spelled words, and A document to be spell-checked against the dictionary. As the document to be spell checked is read, each of its words is checked against the dictionary words. The program determines whether each word is misspelled. Misspelled words are recorded. is spelled correctly. Correctly spelled words are recorded and their frequency counted....
Write a spell checking program (java) which uses a dictionary of words (input by the user...
Write a spell checking program (java) which uses a dictionary of words (input by the user as a string) to find misspelled words in a second string, the test string. Your program should prompt the user for the input string and the dictionary string. A valid dictionary string contains an alphabetized list of words. Functional requirements: For each word in the input string, your program should search the dictionary string for the given word. If the word is not in...
Write a Java program to read in words from the given file “word.txt”. a. Prompt the...
Write a Java program to read in words from the given file “word.txt”. a. Prompt the user for two words b. Print out how many words in the file fall between those words c. If one of the two words is not contained in the file, print out which word is not found in the file d. If both words are not found in the file, print out a message e. Sample output: Please type in two words: hello computer...
Write a program in java which is in main and uses no classes, methods, loops or...
Write a program in java which is in main and uses no classes, methods, loops or if statements Ask the user to enter their first name Ask the user to enter their last name Print their initials followed by periods (ie. Clark Kent = C. K.) Print the number of letters in their last name
Write a program that asks the user for a file name. The file contains a series...
Write a program that asks the user for a file name. The file contains a series of scores(integers), each written on a separate line. The program should read the contents of the file into an array and then display the following content: 1) The scores in rows of 10 scores and in sorted in descending order. 2) The lowest score in the array 3) The highest score in the array 4) The total number of scores in the array 5)...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
[In Python] Write a program that takes a .txt file as input. This .txt file contains...
[In Python] Write a program that takes a .txt file as input. This .txt file contains 10,000 points (i.e 10,000 lines) with three co-ordinates (x,y,z) each. From this input, use relevant libraries and compute the convex hull. Now, using all the points of the newly constructed convex hull, find the 50 points that are furthest away from each other, hence giving us an evenly distributed set of points.
Java question- Write a java program to process the number.txt file. Then count the numbers and...
Java question- Write a java program to process the number.txt file. Then count the numbers and calculate the total average, even numbers average, odd number average, then print the corresponding information. The result should be displayed in the following format there are XX numebers in the file there are xx even numbers there are xx odd numbers the total number average is xx the odd number average is xx the even number average is xx I am having trouble using...
A file concordance tracks the unique words in a file and their frequencies. Write a program...
A file concordance tracks the unique words in a file and their frequencies. Write a program that displays a concordance for a file. The program should output the unique words and their frequencies in alphabetical order. Variations are to track sequences of two words and their frequencies, or n words and their frequencies.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT