Homework Help Question & Answers

create a hash table to implement spell checker in java 1. Create a file of 100...

create a hash table to implement spell checker in java

1. Create a file of 100 words of varying length (max 8 characters).

2. All the words are in lower case.

3. design a hash table.

4. enter each word in the hash table.

Once the hash table is created, run it as a spell checker.enter a word (interactive), find the word in the hash table. If not found enter an error message. Then prompt for next word. End the session by some control character ctrl-c.

1. use the linear probing first. Count the number of collisions and print it.

2. Then use quadratic probing. Count the number of collisions and print it.

3. Start with a table size that is about 53. So 100 words would still have a load factor of less than .5 Now add 10 more words. The program should automatically determine that table size needs to increase.

Print out - increasing table size to <size>

rehash all the old words also and the new words to the new hash table.

Print the collisions in each case for the new table size.

0 0
Next > < Previous
ReportAnswer #1

Linear Probing:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Y2018.April;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
*
* @author sambh
*/
public class HashTable {
private String[] hashTable;
private int collisions;
private int load;

public HashTable() {
hashTable = new String[50];
for (int i = 0; i < 50; i++)
hashTable[i] = null;
collisions = 0;
load = 0;
}

public HashTable(int n ) {
hashTable = new String[n];
for (int i = 0; i < n; i++)
hashTable[i] = null;
collisions = 0;
load = 0;
}
  
public void load(File file) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(file));
String word;
while ((word = br.readLine()) != null)
add(word);
br.close();
}
  
public void add(String word) {
if (load + 1 >= hashTable.length)
expandHashTable();
int hashKey = word.hashCode() % hashTable.length;
while(true) {
if (hashTable[hashKey] == null) {
hashTable[hashKey] = word;
load++;
break;
}
collisions++;
hashKey ++; //linear probing
if (hashKey == hashTable.length)
hashKey = 0;
}
}

private void expandHashTable() {
System.out.println("Increasing table size to " + hashTable.length * 2);
String[] backUpTable = hashTable;
collisions = 0;
load = 0;
hashTable = new String[hashTable.length * 2];
for(int i = 0; i < backUpTable.length; i++)
if (backUpTable[i] != null)
add(backUpTable[i]);
}
  
boolean search(String word) {
int hashKey = word.hashCode() % hashTable.length;
while(true) {
if (hashTable[hashKey] == null) {
return false;
}
else if (hashTable[hashKey].equalsIgnoreCase(word)) {
return true;
}
hashKey ++; //linear probing
if (hashKey == hashTable.length)
hashKey = 0;
if (hashKey == word.hashCode() % hashTable.length)
return false;
}
}
  
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
HashTable hashOb1 = new HashTable(53);
hashOb1.load (new File("words.txt"));
String word;
while(true) {
System.out.println("Enter a word:");
word = br.readLine();
if (hashOb1.search(word))
System.out.println("Found");
else
System.out.println("Not Found");
}
}
}

Quadratic probing:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Y2018.April;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
*
* @author sambh
*/
public class HashTable {
private String[] hashTable;
private int collisions;
private int load;

public HashTable() {
hashTable = new String[50];
for (int i = 0; i < 50; i++)
hashTable[i] = null;
collisions = 0;
load = 0;
}

public HashTable(int n ) {
hashTable = new String[n];
for (int i = 0; i < n; i++)
hashTable[i] = null;
collisions = 0;
load = 0;
}
  
public void load(File file) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(file));
String word;
while ((word = br.readLine()) != null)
add(word);
br.close();
}
  
public void add(String word) {
if (load + 1 >= hashTable.length)
expandHashTable();
int hashKey = word.hashCode() % hashTable.length;
int probing = 1;
while(true) {
if (hashTable[hashKey] == null) {
hashTable[hashKey] = word;
load++;
break;
}
collisions++;
hashKey += (probing * probing); //quadratic probing
probing ++;
hashKey = hashKey % hashTable.length;
}
}

private void expandHashTable() {
System.out.println("Increasing table size to " + hashTable.length * 2);
String[] backUpTable = hashTable;
collisions = 0;
load = 0;
hashTable = new String[hashTable.length * 2];
for(int i = 0; i < backUpTable.length; i++)
if (backUpTable[i] != null)
add(backUpTable[i]);
}
  
boolean search(String word) {
int hashKey = word.hashCode() % hashTable.length;
int probing = 1;
while(true) {
if (hashTable[hashKey] == null) {
return false;
}
else if (hashTable[hashKey].equalsIgnoreCase(word)) {
return true;
}
hashKey += (probing * probing); //linear probing
probing ++;
hashKey = hashKey % hashTable.length;
if (hashKey == word.hashCode() % hashTable.length || probing == hashTable.length)
return false;
}
}
  
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
HashTable hashOb1 = new HashTable(53);
hashOb1.load (new File("words.txt"));
String word;
while(true) {
System.out.println("Enter a word:");
word = br.readLine();
if (hashOb1.search(word))
System.out.println("Found");
else
System.out.println("Not Found");
}
}
}

words.txt
a
about
all
also
and
as
at
be
because
but
by
can
come
could
day
do
even
find
first
for
from
get
give
go
have
he
her
here
him
his
how
I
if
in
into
it
its
just
know
like
look
make
man
many
me
more
my
new
no
not
now
of
on
one
only
or
other
our
out
people
say
see
she
so
some
take
tell
than
that
the
their
them
then
there
these
they
thing
think
this
those
time
to
two
up
use
very
want
way
we
well
what
when
which
who
will
with
would
year
you
your

Know the answer?
Add Answer to:
create a hash table to implement spell checker in java 1. Create a file of 100...
Your Answer: Your Name: What's your source?
Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++ and not Java: Implement a spelling checker by using a hash table. Assume that...

    In C++ and not Java: Implement a spelling checker by using a hash table. Assume that the dictionary comes from two sources: an existing large dictionary and a second file containing a personal dictionary. Output all misspelled words and the line numbers on which they occur. Also, for each misspelled word, list any words in the dictionary that are obtainable by applying any of the following rules: 1. Add one character. 2. Remove one character. 3. Exchange adjacent characters The...

  • Please complete the following task: Create a C++ hash table program for generating a hash from a string. Create a hash f...

    Please complete the following task: Create a C++ hash table program for generating a hash from a string. Create a hash function then test and evaluate the results. The set of strings are from a "words.txt" file (there are 45,402 words in the file, no repeating words. Map the words to 45,402 buckets using a hash function). Requirements: Create an array of 45,402 integers and initialize each to zero. These will be used to store how many times each index...

  • Hash table plllz Help

    so the main overview of the assignment - is to read an input textfile from the user and to put it in a hashtable the text should bestripped from all symbols [email protected]!#$%^&*() except for - ' and numbers. Then we are to see how many times the word has beenused and record all the collisions at the end we are to writethefile to a .cvs file.My problem is i am not sure how to write the code for the hashtable...

  • In this assignment you will implement the second version of your spell checker. Using the randomi...

    In this assignment you will implement the second version of your spell checker. Using the randomized dictionary (random_dictionary.txt) given, read in the dictionary into an array of 26 Binary Search Trees (BST) , one for each letter of the alphabet. Thus the first BST would contain only those words starting with letter 'a', while the last would contain only those words starting with letter 'z'. Then, when you read in the book (oliver.txt), you examine the first character of each...

  • I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash funct...

    I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash function then test and evaluate the results. The set of strings are from a "words.txt" file (there are 45,402 words in the file, no repeating words. Map the words to 45,402 buckets using a hash function). Requirements: Create an array of 45,402 integers and initialize each to zero. These will be used to store how many times each index...

  • I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash funct...

    I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash function then test and evaluate the results. The set of strings are from a "words.txt" file (there are 45,402 words in the file, no repeating words. Map the words to 45,402 buckets using a hash function). Requirements: Create an array of 45,402 integers and initialize each to zero. These will be used to store how many times each index...

  • vas Х Question 1 5 Secondary clustering in a hash table occurs when using Separate chaining...

    vas Х Question 1 5 Secondary clustering in a hash table occurs when using Separate chaining Double hashing Linear probing Quadratic probing Question 2 5 pt Rehashing occurs when a hash table becomes too full and we must migrate to a larger table. If we have N elements, and our new table size is M. what is the Big O time of rehashing? O(M) ON+M) ON) O(Mlog N) Question 3 5 pts When sorting n records. Merge Sort has worst-case...

  • C++ assignment - Hash table with linear probing --- implement in single file - main.cpp You...

    C++ assignment - Hash table with linear probing --- implement in single file - main.cpp You are asked to implement a very specific hash table. The keys are lower-case English words (e.g., apple, pear). The length of a key is at most 10. The hash function is “simply using the last character”. That is, the hash value of apple should be e, and the hash value of pear should be r. Your hash table contains exactly 26 slots (hash value...

  • ****YOU CAN DOWNLOAD THOSE FILES FROM HERE*** https://drive.google.com/open?id=0B2gb5h869g2aaVNYMjAzeDRMRjQ Create a spell checker that uses dictionary that...

    ****YOU CAN DOWNLOAD THOSE FILES FROM HERE*** https://drive.google.com/open?id=0B2gb5h869g2aaVNYMjAzeDRMRjQ Create a spell checker that uses dictionary that stores its words in a balanced binary tree. Tasks and Requirements NOTE: Naming is critical in the tasks and requirements described below. If the names don't match those described below exactly, your project will not be graded. Create a copy of the Java SE Project Template. The project name must follow this pattern: {FLname}_SpellChecker_{TERM}, where {FLname} is replaced by the first letter of your...

  • Java hash table

    Simulate the external hashing structure for a company to store the clients’ information by using Java RandomAccessFile class.Assumptions:• The data structure is implemented by an in-memory hash table and an external file. Assume the file consists of n disc blocks and each block contains r recordsmaximally.• The number of total records is not more than (n * r ) / 2.• each record is defined as follows:It consists of one string name and one string id. The id is a...

Free Homework App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.