×
Reviews 4.9/5 Order Now

Implementing Word Frequency Analysis with Hash Tables in C++: A Sample Assignment Solution

August 29, 2024
Neven Bell
Neven Bell
🇺🇸 United States
C++
Neven Bell is an experienced C++ developer with a strong background in data structures and algorithm design. He specializes in text processing, efficient data storage, and software optimization. With over 10 years of industry experience, Bell has a proven track record of solving complex programming challenges and helping students excel in their C++ assignments.
Key Topics
  • Question:
  • Solution:
Tip of the day
When working on NetLogo assignments, Test each part individually to identify errors early. Use NetLogo’s built-in documentation for guidance on commands.
News
In 2024, new programming libraries like PyWebTools and AIxGen were released, offering advanced web development and AI model generation features.

Welcome to our comprehensive sample solution for a C++ programming assignment, designed to offer valuable C++ assignment help. In this project, we guide you through the process of implementing a word frequency analyzer using C++. By leveraging the power of hash tables, this assignment demonstrates how to efficiently count and store the occurrences of words in a given text. Through this example, you'll explore essential concepts such as text sanitization, input stream processing, and hash table manipulation, all while adhering to best practices in C++ programming. This sample solution is a great resource for students looking to enhance their understanding of text analysis and data structuring in C++. If you're seeking additional assistance, our team is ready to provide expert help with programming assignments to ensure your success.

Question:

Frequency-Analysis

Frequency-Analysis-1

Solution:

Word frequency cpp file

///////////////////////// TO-DO (1) ////////////////////////////// /// Include necessary header files /// Hint: Include what you use, use what you include /// /// Do not put anything else in this section, i.e. comments, classes, functions, etc. Only #include directives #include "WordFrequency.hpp" /////////////////////// END-TO-DO (1) //////////////////////////// // unnamed, anonymous namespace providing non-member private working area namespace { std::string sanitize( const std::string & word ) { constexpr char bad_char[] = " \t\n\b\v_-\"'(){}+/*,=.!?:;"; // leading and trailing characters to be removed static std::locale locality; auto startIndex = word.find_first_not_of( bad_char ); // start with the first non-bad character if( startIndex == std::string::npos ) startIndex = word.size(); // if the word contains only bad characters ... std::size_t count = 0; // assume the word contains only bad characters ... auto endIndex = word.find_last_not_of( bad_char ); // end with the first non-bad character if( endIndex != std::string::npos ) count = endIndex - startIndex + 1; // number of characters to use in results auto result = word.substr( startIndex, count ); // strip the leading and trailing bad characters for( auto & c : result ) c = std::tolower( c, locality ); // convert to lower case return result; } } // unnamed, anonymous namespace // Implement WordFrequency::WordFrequency( std::istream ) - See requirements ///////////////////////// TO-DO (2) ////////////////////////////// WordFrequency::WordFrequency(std::istream &iStream) { std::string word; while (iStream >> word) { word = sanitize(word); if (!word.empty()) { ++wordFrequencyMap[word]; } } } /////////////////////// END-TO-DO (2) //////////////////////////// // Implement WordFrequency::numberOfWords() const - See requirements ///////////////////////// TO-DO (3) ////////////////////////////// std::size_t WordFrequency::numberOfWords() const { return wordFrequencyMap.size(); } /////////////////////// END-TO-DO (3) //////////////////////////// // Implement WordFrequency::wordCount( const std::string & ) const - See requirements ///////////////////////// TO-DO (4) ////////////////////////////// std::size_t WordFrequency::wordCount(const std::string &word) const { auto it = wordFrequencyMap.find(sanitize(word)); return it != wordFrequencyMap.end() ? it->second : 0; } /////////////////////// END-TO-DO (4) //////////////////////////// // Implement WordFrequency::mostFrequentWord() const - See requirements ///////////////////////// TO-DO (5) ////////////////////////////// std::string WordFrequency::mostFrequentWord() const { std::string mostFrequent; std::size_t highestFrequency = 0; for (const auto &pair : wordFrequencyMap) { if (pair.second > highestFrequency) { highestFrequency = pair.second; mostFrequent = pair.first; } } return mostFrequent; } /////////////////////// END-TO-DO (5) //////////////////////////// // Implement WordFrequency::maxBucketSize() const - See requirements ///////////////////////// TO-DO (6) ////////////////////////////// /// Hint: see the unordered_map's bucket interface at https://en.cppreference.com/w/cpp/container/unordered_map std::size_t WordFrequency::maxBucketSize() const { std::size_t maxSize = 0; for (std::size_t i = 0; i < wordFrequencyMap.bucket_count(); ++i) { maxSize = std::max(maxSize, wordFrequencyMap.bucket_size(i)); } return maxSize; } /////////////////////// END-TO-DO (6) ////////////////////////////

Word frequency hpp file

#pragma once ///////////////////////// TO-DO (1) ////////////////////////////// /// Include necessary header files /// Hint: Include what you use, use what you include /// /// Do not put anything else in this section, i.e. comments, classes, functions, etc. Only #include directives #include <unordered_map> #include <string> #include <istream> #include <iostream> /////////////////////// END-TO-DO (1) //////////////////////////// class WordFrequency { public: WordFrequency( std::istream & iStream = std::cin); // (default) constructor, add words from file to hash table std::size_t numberOfWords ( ) const; // return the number of unique words std::size_t wordCount ( const std::string & word ) const; // return the number of occurrences of the given word std::string mostFrequentWord( ) const; // return the most frequent word std::size_t maxBucketSize ( ) const; // return the size of the largest bucket in the hash table private: ///////////////////////// TO-DO (2) ////////////////////////////// /// The class should have a single instance attribute of type std::unordered_map, which is the C++ Standard Library's /// implementation of a hash table, to store the association of words (key) to the number of times a word occurs (value). std::unordered_map<std::string, std::size_t> wordFrequencyMap; /////////////////////// END-TO-DO (2) //////////////////////////// };

Similar Samples

Discover our comprehensive collection of expertly solved programming samples at Programming Homework Help. Each example is meticulously crafted to showcase top-quality solutions. Explore our work and feel confident in choosing our services to tackle your assignments with ease.