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

Working code implemented in Java and appropriate comments provided for better understanding.

Here I am attaching code for these files:

  • Driver.java
  • ReadFiles.java
  • PrimitiveJavaEditor.java

Driver.java:

public class Driver
{
public static void main(String[] args) {

ReadFiles rf = new ReadFiles();
rf.readTextFile();
rf.readDictionary();
PrimitiveJavaEditor pje = new PrimitiveJavaEditor();
pje.spellCheckAll();

}
}

ReadFiles.java:

import java.util.*;
import java.io.*;
public class ReadFiles {

Scanner fileScanner;
static LinkedList<String> textFile;
static HashSet<String> wordList;
String dictionaryFile = "EnglishWordList.txt";
String textFileName ="alcott-little-261.txt";

public ReadFiles () {

fileScanner=null;
textFile = new LinkedList<>();
wordList = new HashSet<>();

}
  
  
public void readTextFile()
{

String line=null;
//String[] words;
try
{
  
fileScanner = new Scanner (new File (textFileName));

while (fileScanner.hasNext()) {

line = fileScanner.nextLine();
String[] words=line.split(" ");
for (int i=0;i<words.length;i++) {
String s = words[i].replaceAll("[\\[\\]_:\"'`?;\\�0-9—;“()-/.,*! ]", "").trim().toLowerCase();
if (!s.isEmpty())textFile.add(s);
}
  
}
}
catch (IOException e)
{
System.out.println(e);
}

}

public void readDictionary()
{

String word=null;
try
{
  
fileScanner = new Scanner (new File (dictionaryFile));

while (fileScanner.hasNext()){

word = fileScanner.nextLine();
wordList.add(word);
}

}
catch (IOException e)
{
System.out.println(e);
}

}

}

PrimitiveJavaEditor.java:

import java.util.*;
import java.io.*;

public class PrimitiveJavaEditor

{
PrintWriter printWriter=null;

public boolean find (String x)
{ String word;
ListIterator<String> itr = ReadFiles.textFile.listIterator();
while (itr.hasNext()){
word= itr.next().trim();
//word= word.replaceAll("[\\[\\]_:\"'`?;0-9;()-/.,*! ]", "").toLowerCase();
// System.out.println(word);
if (word.equals(x)) {
return true;
}
}
return false;
}
// Looks for a word "x" in the file and returns true if found or false otherwise.


public boolean findReplace (String find, String replace)

{
int index;
index = ReadFiles.textFile.indexOf(find);
if (index != -1) {
String prev = ReadFiles.textFile.set(index,replace);
return true;
}
else return false;
}

// looks for the first occurrence of word "x" in the file and replaces it with word "y" if found returning true, false otherwise.

public boolean findInsert (String find, String insert) {
// looks for the first occurrence of word "x" in the file and then insert "y" right after "x", if x is found, returning true, false otherwise.

int index;
index = ReadFiles.textFile.indexOf(find);
if (index != -1) {
ReadFiles.textFile.add(index+1,insert);
return true;
}
else return false;
}

public boolean delete (String del) {
// looks for the first occurrence of word "x" in the file and deletes it from the file, returning true if x is found, returning false otherwise.

return (ReadFiles.textFile.removeFirstOccurrence(del));
}


public String spellCheck () { // finds the first occurrence of spelling error and returns the misspelled word. If no word is misspelled returns "Spell Check Passed".

String word;
ListIterator<String> itr = ReadFiles.textFile.listIterator();
while (itr.hasNext()){
word= itr.next().trim();

// word= word.replaceAll("[\\[\\]_:\"'`?;0-9;()-/.,*! ]", "").toLowerCase();
if (!ReadFiles.wordList.contains(word)) return word;
}
return "no spelling errors";

}

public void spellCheckAll() {

// find all misspelled words and output them to the screen.
   int count = 0;
   long time1 = System.currentTimeMillis();
     
String word;
boolean spellPass=true;
ListIterator<String> itr = ReadFiles.textFile.listIterator();
while (itr.hasNext()){
word= itr.next().trim();
//word= word.replaceAll("[\\[\\]_:\"'`?;0-9;()-/.,*! ]", "").toLowerCase();
// word=word.replaceAll("[]","");
// System.out.println(word);
if (!ReadFiles.wordList.contains(word)) {count++;
spellPass=false;
}
}
if (spellPass) System.out.println("no misspelled words, spellcheck passed ");
long time2 = System.currentTimeMillis();
System.out.println(time2 - time1 + " milliseconds");
System.out.println(count + " number of misspelled words");
}

public void save() {
// saves file with the changes made
  
ListIterator itr = ReadFiles.textFile.listIterator();
int count=0;
try
{
printWriter = new PrintWriter (new FileWriter ("newTextFile.txt"));
while (itr.hasNext()){
printWriter.print(itr.next());
count++;
if (count % 10 ==0) printWriter.print ("\n");
}
  
}

catch (IOException e)
{
System.out.println(e);
}
// }

printWriter.close();
  
}


public void print()
// saves file with the changes and outputs the contents of the file to the screen.
{
this.save();
Scanner sc;


try
{
sc = new Scanner("newTextFile.txt");
while (sc.hasNext())
System.out.println(sc.nextLine());
}

catch (Exception e)
{
System.out.println(e);
}
}
  
public void quit() {
  
save();
  
}
  
public boolean findReplaceAll(String x, String y) {
  
boolean found=false;
int index=0;
ListIterator<String> itr = ReadFiles.textFile.listIterator();
while (itr.hasNext()){
String word= itr.next().trim();
if (word.equals(x)) {
ReadFiles.textFile.set(index,y);
found=true;
index++;
}
}
return found;
  
}
}

Sample Output Screenshots:


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...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
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: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
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 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)...
Write a Java program that reads words from a text file and displays all the non-duplicate...
Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... 1mango Salami apple banana boat zebra
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT