Question

In: Computer Science

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.

The program outputs

  • the most commonly used, correctly-spelled words. These words are listed in alphabetical order, ignoring case.
  • all correctly spelled words, by frequency. If more than one word occurs with a given frequency, the words are displayed in alphabetical order, ignoring case.
  • incorrectly spelled words in alphabetical order, ignoring case.

Objectives

At the end of this program the student will demonstrate the ability to write a program that:

  • Reads a file of dictionary words
  • Reads a document whose words will be checked against the dictionary
  • Identifies correctly and incorrectly spelled words
  • Maintains misspelled words
  • Maintains correctly spelled words, with a frequency count
  • Identifies the most common word

Sample Execution

Running a correct program using the SIL dictionary and the Bill of Rights, the following is output:

most frequent words

[the]

frequency 40

[the]

frequency 24

[of]

frequency 22

[to]

frequency 20

[be]

frequency 18

[in]

frequency 17

[shall]

frequency 15

[or]

frequency 14

[and]

frequency 10

[a, amendment]

frequency 9

[by]

frequency 8

[nor]

frequency 6

[for, law]

frequency 5

[no, not, people, right]

frequency 4

[any, have, jury, states]

frequency 3

[against, public, time, without]

frequency 2

[an, been, but, cause, common, constitution, crime, criminal, district, excessive, free, his, life, militia, otherwise, person, persons, process, property, state, trial, united, war, witnesses]

frequency 1

[abridging, according, accusation, accused, actual, affirmation, all, answer, are, arising, arms, ascertained, assemble, assistance, at, bail, bear, being, capital, case, cases, certain, committed, compelled, compensation, compulsory, confronted, congress, consent, construed, controversy, counsel, court, cruel, danger, defence, delegated, deny, deprived, describing, disparage, dollars, due, effects, enjoy, enumeration, establishment, exceed, except, exercise, fact, favor, fines, forces, freedom, government, grand, grievances, held, him, himself, house, houses, ii, iii, impartial, imposed, indictment, infamous, inflicted, informed, infringed, issue, it, iv, jeopardy, just, keep, land, liberty, limb, make, manner, nature, naval, necessary, oath, obtaining, offense, on, others, owner, papers, particularly, peace, peaceably, petition, place, powers, prescribed, presentment, preserved, press, previously, private, probable, prohibited, prohibiting, prosecutions, punishments, put, quartered, redress, reexamined, regulated, religion, required, reserved, respecting, respectively, retained, rights, rules, same, searched, searches, secure, security, seized, seizures, service, soldier, speech, speedy, subject, suits, supported, taken, than, their, thereof, things, tried, twenty, twice, unless, unreasonable, unusual, upon, use, value, violated, warrants, well, when, where, wherein, which, with, witness]

misspelled words

[I, IX, V, VI, VII, VIII, X]

Solutions

Expert Solution

Solution code:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Dictionary {
private int M = 1319; //prime number
final private Bucket[] array;
public Dictionary() {
this.M = M;

array = new Bucket[M];
for (int i = 0; i < M; i++) {
array[i] = new Bucket();
}
}

private int hash(String key) {
return (key.hashCode() & 0x7fffffff) % M;
}

//call hash() to decide which bucket to put it in, do it.
public void add(String key) {
array[hash(key)].put(key);
}

//call hash() to find what bucket it's in, get it from that bucket.
public boolean contains(String input) {
input = input.toLowerCase();
return array[hash(input)].get(input);
}

public void build(String filePath) {
try {
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line;
while ((line = reader.readLine()) != null) {
add(line);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}

}
//this method is used in my unit tests
public String[] getRandomEntries(int num){
String[] toRet = new String[num];
for (int i = 0; i < num; i++){
//pick a random bucket, go out a random number
Node n = array[(int)Math.random()*M].first;
int rand = (int)Math.random()*(int)Math.sqrt(num);

for(int j = 0; j<rand && n.next!= null; j++) n = n.next;
toRet[i]=n.word;


}
return toRet;
}

class Bucket {

private Node first;

public boolean get(String in) { //return key true if key exists
Node next = first;
while (next != null) {
if (next.word.equals(in)) {
return true;
}
next = next.next;
}
return false;
}

public void put(String key) {
for (Node curr = first; curr != null; curr = curr.next) {
if (key.equals(curr.word)) {
return; //search hit: return
}
}
first = new Node(key, first); //search miss: add new node
}

class Node {

String word;
Node next;

public Node(String key, Node next) {
this.word = key;
this.next = next;
}

}

}
}

If you have any Queries please comment below.
Please Like It.
Thank you


Related Solutions

(JAVA) We will write a program to check the spelling of the word entered by user,...
(JAVA) We will write a program to check the spelling of the word entered by user, using data from a dictionary, which is text file. The program will ask user to enter a word for spell check, and then check in the text file (dictionary.txt) if the word exists. (The dictionary.txt file is provided) If the word exists in text file, the output will be “Word is correctly spelled”. If the word doesn’t exist in the text file, program will...
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 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 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 program in JAVA that takes in two words, and then it recursively determines if...
Write a program in JAVA that takes in two words, and then it recursively determines if the letters of the first word are contained, in any order, in the second word. If the size of the first word is larger than the second then it should automatically return false. Also if both strings are empty then return true. YOU MAY NOT(!!!!!!!!!!): Use any iterative loops. In other words, no for-loops, no while-loops, no do-while-loops, no for-each loops. Use the string...
In Java: Write a program that will count the number of characters, words, and lines in...
In Java: Write a program that will count the number of characters, words, and lines in a file. Words are separated by whitespace characters. The file name should be passed as a command-line argument, as shown below. c:\exercise>java Exercise12_13 Loan.java File loan.java has 1919 characters 210 words 71 lines c:\exercise> Class Name: Exercise12_13
Write a java simple document retrieval program that first asks the user to enter a single...
Write a java simple document retrieval program that first asks the user to enter a single term query, then goes through two docuements named doc1.txt and doc2.txt (provided with assignment7) to find which document is more relevant by calculating the frequency of the query term in each document. Output the conclusion to a file named asmnt7output.txt in the following format (as an example for query “java” and “problem”). The percentages in parenthese are respective supporting frequencies. java: doc1(6.37%) is more...
3) Create a Java program that uses NO methods, but use scanner: Write a program where...
3) Create a Java program that uses NO methods, but use scanner: Write a program where you will enter the flying distance from one continent to another, you will take the plane in one country, then you will enter miles per gallon and price of gallon and in the end it will calculate how much gas was spend for that distance in miles. Steps: 1) Prompt user to enter the name of country that you are 2) Declare variable to...
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 java program that uses a stack to reverse an integer of three digits. The...
Write a java program that uses a stack to reverse an integer of three digits. The program must prompt the user to input an integer of 3 digits and display it in reverse. - Your program must include the Stack class, the Reverse class, and the Test class. - In the Test class, the program must prompt the user to input a 3-digit number and display it in reverse. - Class Reverse must use the Stack class to reverse the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT