Question

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.

1 0
Add a comment Improve this question Transcribed image text
Answer #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

Add a comment
Know the answer?
Add Answer to:
create a hash table to implement spell checker in java 1. Create a file of 100...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

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
  • Write a spell checker that stores a set of words, W, in a hash table and...

    Write a spell checker that stores a set of words, W, in a hash table and implements a function, spellCheck(s), which performs a spell check on the string s with respect to the set of words, W. If s is in W, then the call to spellCheck(s) returns an iterable collection that contains only s, because it is assumed to be spelled correctly in this case. Otherwise, if s is not in W, then the call to spellCheck(s) returns a...

  • Overview: The goal of this assignment is to implement a simple spell checker using a hash...

    Overview: The goal of this assignment is to implement a simple spell checker using a hash table. You will be given the basic guidelines for your implementation, but other than that you are free to determine and implement the exact classes and methods that you might need. Your spell-checker will be reading from two input files. The first file is a dictionary containing one word per line. The program should read the dictionary and insert the words into a hash...

  • IN JAVA: Write a spell checker class that stores a set of words, W, in a...

    IN JAVA: Write a spell checker class that stores a set of words, W, in a hash table and implements a function, spellCheck(s), which performs a Spell Check on the string s with respect to the set of words, W. If s is in W, then the call to spellCheck(s) returns an iterable collection that contains only s, since it is assumed to be spelled correctly in this case. Otherwise, if s is not in W, then the call to...

  • Write a spell checker class that stores a set of words, W, in a hash table...

    Write a spell checker class that stores a set of words, W, in a hash table and implements a function, spellCheck(s), which performs a Spell Check on the string s with respect to the set of words, W. If s is in W, then the call to spellCheck(s) returns an iterable collection that contains only s, since it is assumed to be spelled correctly in this case. Otherwise, if s is not in W, then the call to spellCheck(s) returns...

  • 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...

  • Python: Write a program that inserts numbers from a file into a quadratic probing hash table...

    Python: Write a program that inserts numbers from a file into a quadratic probing hash table and records the amount of collisions that occur after each insertion. Hash Table size is 191, so 100 random numbers are to be inserted to the table to collect number of collisions.

  • IN JAVA LANGUAGE: For this lab you are required for implement the following methods: 1. public...

    IN JAVA LANGUAGE: For this lab you are required for implement the following methods: 1. public QuadraticProbingHashTable( int size ): As the signature of this method suggests, this is a constructor for this class. This constructor will initialize status of an object from this class based on the input parameter (i.e., size). So, this function will create the array HashTable with the specified size and initialize all of its elements to be null. 2. public int hash(int value, int tableSize...

  • IN C++ Create a student hash table that contains information, studentID (int), name (string), marks_oop345 (float)....

    IN C++ Create a student hash table that contains information, studentID (int), name (string), marks_oop345 (float). The size of hash table is equal to the number of students in the class. Use linear probing in case of collisions. Perform insertion, deletion, display and search operations.

  • 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...

  • 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...

ADVERTISEMENT
Free Homework Help 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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT