Question: I am trying to write the code for C++ for hash table functions. the .h file is as follows: ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ // WordCount.h #ifndef WORDCOUNT_H #define

I am trying to write the code for C++ for hash table functions.

the .h file is as follows:

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

// WordCount.h #ifndef WORDCOUNT_H #define WORDCOUNT_H #include  #include  #include  #include  class WordCount { public: WordCount(); int getTotalWords() const; // returns total number of word occurences (non-unique) in the // hash table. int getNumUniqueWords() const; // returns total number of unique words in the hash table. int getWordCount(std::string word) const; // Return the number of occurences of a specific word. // If the word does not exist in vocabulary, return 0 int incrWordCount(std::string word); // Update the hash table by incrementing the number of occurences for // the word. Words must be hashed and stored in all upper-case characters. // If the word not in vocabulary, add it with the appropriate // values, then increment the number of occurences. static bool isWordChar(char c); // May be useful as a helper function in your code. // Returns true if c is a valid word character defined as either // a lower-case or upper-case alpha char. static std::string stripWord(std::string word); // Words cannot contain any digits or special characters EXCEPT for // hyphens (-) and apostrophes (') that occur in the middle of a // valid word. // For example, "can't" and "good-hearted" are considered valid words. // Some examples of stripped words: // "12moNkEys" will be stripped to "moNkEys" // "$money!" will be stripped to "money" // "C++" will be stripped to "C" // "$1wo,rd-%#" will be stripped to "word" // "'nuff-said-" will be stripped to "nuff-said". void dumpWordsSortedByWord(std::ostream &out) const; // Dump each word,count pair as CSV to std::ostream, sorted by word in // ascending lexicographical order based on ASCII values. Each word count // pair will be in its own line with as: word,occurrence // For example, if the following string of words was entered into the // table: "Sentence is a sentence.", then out will contain: // A,1 // IS,1 // SENTENCE,2 void dumpWordsSortedByOccurence(std::ostream &out) const; // dump each word,count as CSV to std::ostream, sorted by occurence in // descending order. // In the event of a tie, ordering is defined by the ascending // lexicographical order of the word based on its ASCII value. // Each word count pair will be in its own line with as word,numOccurence. // For example, if the following string of words was entered into the // table: "Sentence is a sentence.", then out will contain: // SENTENCE,2 // A,1 // IS,1 void addAllWords(std::string text); // parse out the string text into individual words, and add each word // to the hash table. You may assume that words are always // separated by a space (' ') or newline (' ') within the string text. // It is possible for the text to have multiple spaces and newlines // before/after each word. You may not assume that the words are valid // words, and must strip them and convert to upper case before // inserting into the hash table. private: const static size_t CAPACITY = 100; // capacity for the hash table array std::vector > table[CAPACITY]; // hash table array of vectors. Each index in the array will contain // a vector. Each element in the vector contains a  // pair where the string value represents a unique word and the size_t // value represents the number of occurences for that word. size_t hash(std::string word) const; // hash function that will output an index for a string // in the table }; #endif // WORDCOUNT_H

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

So far I have the WordCount.cpp file as:

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

// WordCount.cpp

#include "WordCount.h"

using namespace std;

// Default constructor

WordCount::WordCount() {}

// Simple hash function. Do not modify.

size_t WordCount::hash(std::string word) const {

size_t accumulator = 0;

for (size_t i = 0; i < word.size(); i++) {

accumulator += word.at(i);

}

return accumulator % CAPACITY;

}

int WordCount::getTotalWords() const {

size_t i, j;

int count;

for(i = 0, count = 0; i < CAPACITY; i++) {

for(j = 0; j < table[i].size(); j++)

count += table[i][j].second;

}

return count;

}

int WordCount::getNumUniqueWords() const {

size_t i;

int count;

for(i = 0, count = 0; i < CAPACITY; i++) {

count += table[i].size();

}

return count;

}

int WordCount::getWordCount(std::string word) const {

word = stripWord(word);

if(word == "")

return 0;

size_t i;

for(i = 0; i < word.size(); i++) {

if(word.at(i) >= 'a' && word.at(i) <= 'z')

word[i] += ('A' - 'a');

}

size_t hashval = hash(word);

for(i = 0; i < table[hashval].size(); i++) {

std::pair entry = table[hashval][i];

if(entry.first == word) {

return entry.second;

}

}

// not found

return 0;

}

int WordCount::incrWordCount(std::string word) {

word = stripWord(word);

if(word == "")

return 0;

size_t i;

for(i = 0; i < word.size(); i++) {

if(word.at(i) >= 'a' && word.at(i) <= 'z')

word[i] += ('A' - 'a');

}

size_t hashval = hash(word);

for(i = 0; i < table[hashval].size(); i++) {

std::pair entry = table[hashval][i];

if(entry.first == word) {

entry.second ++;

table[hashval][i] = entry;

return entry.second;

}

}

// not found

std::pair entry;

entry.first = word;

entry.second = 1;

table[hashval].push_back(entry);

return 1;

}

bool WordCount::isWordChar(char c) {

if( (c >= 'a' && c <= 'z')

|| (c >= 'A' && c <= 'Z') )

return true;

return false;

}

std::string WordCount::stripWord(std::string word) {

size_t i;

for(i = 0; i < word.size(); i++) {

if(isWordChar(word.at(i)))

continue;

if(i == 0 || i == word.size() - 1) { //beginning or end

word.erase(i, 1);

i --;

}

else {

if(word.at(i) == '\'' || word.at(i) == '-')

continue;

word.erase(i, 1);

i --;

}

}

//reverse the operation starting from the end

i = word.size() - 1;

while(word.size() > 0 && !isWordChar(word.at(i))) {

word.erase(i, 1);

i = word.size() - 1;

}

return word;

}

void WordCount::dumpWordsSortedByWord(std::ostream &out) const {

// STUB

return;

}

void WordCount::dumpWordsSortedByOccurence(std::ostream &out) const {

// STUB

return;

}

void WordCount::addAllWords(std::string text) {

// STUB

return;

}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

I am having trouble with the last 3 functions using osstream and how to create ASSERT_EQUALS tests for them. Please help!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!