In: Computer Science
JAVA
Write a program that checks the spelling of words in a document. This program uses two text files:
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
The program outputs
Objectives
At the end of this program the student will demonstrate the ability to write a program that:
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]
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