Question Details

[solution] »

Hey! check the attachments, let me know if you need more information. Thank you


Approach


Description

Answer Download


The Question

Hey! check the attachments, let me know if you need more information. Thank you


Approach 1: Binary Search Tree

 

One easy way of implementing a spell checker is to use a binary search tree. Given a list of valid

 

words, you can insert them into a BST. Checking whether a word is spelled correctly simply involves

 

searching the tree for that word. If the word is found, great ? if not, it must be misspelled! Remember

 

that finding items in a BST takes O(log n) time on average, where n is the number of elements in the

 

tree. Thus, the expected time cost of using this spell checker depends on the number of words that it?s

 

storing.

 

1. (14 pts) Write a BSTSpellChecker class. This class should have an instance variable of type

 

BinarySearchTree<String> to store its word list. You can use the BinarySearchTree<E>

 

code that we wrote in lecture. BSTSpellChecker should contain the following methods:

 

a. add(String s)

 

Adds the specified word to the tree.

 

b. contains(String s)

 

Returns whether the specified word is in the tree.

 

c. A method that reads a list of words from a file and adds them all into the tree. Add

 

the words to the tree in the same order that they appear in the file (i.e.,

 

alphabetically). (Note ? you will probably have to use non-recursive versions of the

 

BST add and find methods to prevent stack overflows here!)

 

d. Remember that adding things in order into a BST is a bad idea ? this will result in a

 

linked list with O(n) performance. Write another version of the above method that adds

 

the words in a balanced manner. You can do this by first adding the word in the middle

 

of the file, then recursively adding the words in the middle of the left and right halves,

 

and so on.

 

A good spell checker should also give some suggested corrections for words that are misspelled.

 

Some common techniques of doing this are to take the misspelled word and see if any valid words

 

can be formed by: Swapping any pair of adjacent characters in the word.

 

Inserting a new character at any position in the word. (For a word containing n characters,

 

there should be n + 1 insertions tested.) Deleting a character at any position in the word. Replacing the character at each position in the word with a new character.

 

Inserting a space between any pair of adjacent characters in the word, to see if two smaller

 

valid words can be formed. Of course, not all results from these transformations should be included as suggested corrections ?

 

only those that form valid words! Each result must be tested to see if it?s contained within the spell

 

checker?s collection of words.

 

2. (12 pts) Within your BSTSpellChecker class, add a method closeMatches(String s) that

 

returns a java.util.Set<String> of valid words that are obtained by applying these transformations to

 

the string s. Approach 2: Hash Table

 

As we discussed in class, hash tables allow (in theory) the ?holy grail? of O(1) performance for

 

adding and retrieving items from the table. Since every add and get operation requires hashing, the

 

hash function plays a critical role in the performance of a hash table. A hash function is a poor choice

 

if it takes too long to compute, or if it results in frequent collisions. In this part you?ll explore the

 

relative performance of a few different string hash functions.

 

3. (8 pts) Write a HashTable class that supports the following operations:

 

a. add(String newItem)

 

Adds the specified newItem to the hash table.

 

b. get(String someItem)

 

Finds and returns the specified someItem from the table if it exists. If someItem

 

doesn?t exist in the table, the method should return null.

 

This HashTable class should resolve collisions via chaining. You can use either

 

java.util.ArrayList<E> or java.util.LinkedList<E> for the lists at each table index. Use an

 

initial table length of 5, and rehash the table as soon as the load factor reaches 1.0. The load

 

factor is computed the same way as in a hash table with open addressing: load factor = (number

 

of elements in the table) / (length of the data array). Rehashing should change the table length to

 

2*(old length) + 1.

 

HashTable should also include a constructor that allows you to specify a StringHasher

 

object indicating what type of hash function to use (more on that in the next part).

 

(Note: You can use the HashMapChained class that we wrote in lecture as a starting point.

 

However, note that HashTable is a little simpler ? it?s designed to store only strings, and there is

 

no separate value associated with each string key.)

 

4. (8 pts) Write a StringHasher interface that contains a single method, int hash(String s).

 

This interface should be implemented by three different classes, each of which defines a

 

different string hash function:

 

a. The first hash function is very simple ? it returns 0 all the time. This makes it a good

 

candidate for the Worst Hash Function EverTM, since collisions are guaranteed!

 

b. The second hash function sums the ASCII values of all characters in the string to

 

determine the hash. This gives a decent chance that different strings will hash to

 

different values, but it still does not consider the placement of characters within the

 

string. Different strings that are permutations of each other ? for example, ?mean? vs. ?name? ? will hash to the same value.

 

c. The third hash function improves the second by also taking the location of each

 

character into consideration. One popular way of doing this is the ?djb2? hash

 

function, which you can find C code for here: http://www.cse.yorku.ca/~oz/hash.html .

 

C syntax is very close to Java, but here?s the algorithm in pseudocode: function hash(string s):hash = 5381

 

for each character c in s:

 

hash = hash*33 + c

 

return hash

 

5. (8 pts) Write a HashTableSpellChecker class. This class should have an instance of

 

HashTable to store its word list. It should have the same functionality as your

 

BSTSpellChecker class from previously. Specifically, HashTableSpellChecker should

 

support these operations:

 

6. (8 pts) Write a HashTableSpellChecker class. This class should have an instance of

 

HashTable to store its word list. It should have the same functionality as your

 

BSTSpellChecker class from previously. Specifically, HashTableSpellChecker should

 

support these operations:

 

a. add(String s)

 

Adds the specified word to the spell checker.

 

b. contains(String s)

 

Returns whether the specified word is in the spell checker.

 

c. A method that reads a list of words from a file and adds them all to the spell checker.

 

d. closeMatches(String s)

 

Returns a java.util.Set<String> of valid words that are obtained by applying the

 

previously mentioned transformations to the string s.

 

I recommend creating an abstract superclass for both BSTSpellChecker and

 

HashTableSpellChecker to save yourself some coding! Both the file reading and

 

closeMatches methods can be placed in this superclass. Putting It All Together

 

7. (10 pts) Write a client program that allows the user to pick one of your five spell checkers (the

 

unbalanced BST, the balanced BST, or any of the three hash table versions). The program

 

should create a spell checker object of that type and then allow the user to enter any number of

 

sentences to check. For each sentence, show the misspelled words and a list of suggested

 

corrections for each one. Also have your program display some statistics when it creates the spell checker object:

 

a. The time required to read the word list

 

b. (For hash tables) The number of hash collisions that occurred during spell checker

 

creation

 

c. (For hash tables) The percentage of indices in the final table that are occupied (15 pts) are set aside for coding style and documentation.

 


Solution details

Solution #000122190

[solution] »

Hey! check the attachments, let me know if you need more information. Thank you


Approach.zip

Uploaded by: Tutor

Answer rating:

This paper was answered on 21-Sep-2019

Pay using PayPal (No PayPal account Required) or your credit card . All your purchases are securely protected by .

About this Question

STATUS

Answered

QUALITY

Approved

DATE ANSWERED

Sep 21, 2019

EXPERT

Tutor

ANSWER RATING

BEST TUTORS

We have top-notch tutors who can do your essay/homework for you at a reasonable cost and then you can simply use that essay as a template to build your own arguments.

You can also use these solutions:

  • As a reference for in-depth understanding of the subject.
  • As a source of ideas / reasoning for your own research (if properly referenced)
  • For editing and paraphrasing (check your institution's definition of plagiarism and recommended paraphrase).
This we believe is a better way of understanding a problem and makes use of the efficiency of time of the student.

STUCK WITH YOUR PAPER?

Order New Solution. Quick Turnaround

Click on the button below in order to Order for a New, Original and High-Quality Essay Solutions. New orders are original solutions and precise to your writing instruction requirements. Place a New Order using the button below.

WE GUARANTEE, THAT YOUR PAPER WILL BE WRITTEN FROM SCRATCH AND WITHIN A DEADLINE.

Order Now