Spell checker (C++) Using a hash table/function, print out a list of words that
ID: 3690859 • Letter: S
Question
Spell checker (C++)
Using a hash table/function, print out a list of words that appear to be misspelled.
A hash table contains buckets into which an object (data item) can be placed. When a hash function is applied to an object, a hash value is generated. The hash value is used to determine which bucket the object is assigned to. Hashed associated containers in STL have a constructor that takes the number of buckets and guarantees the bucket count will be at least that number.
A bucket is a cluster (or a sub container) that holds a set of data items that hash to the same table location. Obviously, you can not store 25K words in 1373 slots and you need to use some kind of chaining schemes such as linear probing or the second hashing. The size of a bucket is independent from the number of data items you put into the hash. So if you have too many buckets, the hash will not have many collisions but you may waste the storage and you may have to deal with a rather complex hash function and longer keys. If you have too small number of buckets, then you have to deal with frequent collisions. Finding a good bucket number would play an important role in reducing collisions. That's why we usually pick a prime number for the number of bucket. We picked 1373 for the bucket number.
You can use an STL for this project.
and http://www.cplusplus.com/reference/unordered_map/unordered_map/ (Links to an external site.) To set the hash you might use .hash_function() and to set the hash table size, you would use .rehash(). Like a vector note that the [ ] operators are assigned. Note that you still have to deal with collisions (either with linked lists or vectors)
OR you take the easier way (less learning, but more practical) using
http://www.cplusplus.com/reference/unordered_set/unordered_multiset/ (Links to an external site.) (Make sure to compile with std=C++11). This auto increases the size of the buckets so you won't have collisions.
OR You can implement your own hash, based on the textbook implementation but you need to come up with a scheme to include buckets (clusters) and collision resolution. Hash set from STL makes this very easy by using a constructor with a bucket value, although the exact bucket count may be reevaluated and altered by the library (it's OK for the homework. you don't have to constrain the program to use 1373 buckets)
If you're implementing your own hash, here's a sample function to create a key from a string. Basically it uses a prime number and the character ascii values for the % function.
class HFstring
{
public:
unsigned int operator()(const string& item) const
{
unsigned int prime = 2049982463;
int n = 0, i;
for (i = 0; i < item.length(); i++)
n = n*8 + item[i];
return n > 0 ? (n % prime) : (-n % prime);
}
};
You don't have to interpret verb tense, plurals, conjugations etc. All you have to do is to check with each word with the dictionary.
Explanation / Answer
main.cpp
hashdocs.h
hashfuncs.h
hashtypes.h
testdocs.h
utility.cpp
utility.h
d1.txt
hashmap_output.txt
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.