基于不同策略的英文单词的词频统计和检索系统。在这个系统中,我们使用了两种不同的数据结构,分别是哈希表和二叉搜索树。
1. 类设计
class WordFrequencySystem { private: // 哈希表策略 std::unordered_map<std::string, int> hashTable; // 二叉搜索树策略 std::map<std::string, int> binarySearchTree; public: // 插入单词 void insertWord(const std::string& word); // 获取单词的词频 int getWordFrequency(const std::string& word, const std::string& strategy); // 获取所有单词及其词频 void getAllWordsAndFrequency(const std::string& strategy); };
2. 插入单词操作
void WordFrequencySystem::insertWord(const std::string& word) { // 在哈希表中插入 hashTable[word]++; // 在二叉搜索树中插入 binarySearchTree[word]++; }
这个操作用于将给定的单词插入到哈希表和二叉搜索树中,并更新词频。
3. 获取单词词频操作
int WordFrequencySystem::getWordFrequency(const std::string& word, const std::string& strategy) { // 根据策略选择相应的数据结构 std::map<std::string, int>* selectedStructure; if (strategy == "hash") { selectedStructure = &hashTable; } else if (strategy == "bst") { selectedStructure = &binarySearchTree; } else { std::cerr << "Invalid strategy!" << std::endl; return -1; } // 查找词频 auto it = selectedStructure->find(word); if (it != selectedStructure->end()) { return it->second; } else { return 0; } }
这个操作允许用户根据选择的策略从哈希表或二叉搜索树中检索指定单词的词频。
4. 获取所有单词及其词频操作
void WordFrequencySystem::getAllWordsAndFrequency(const std::string& strategy) { // 根据策略选择相应的数据结构 std::map<std::string, int>* selectedStructure; if (strategy == "hash") { selectedStructure = &hashTable; } else if (strategy == "bst") { selectedStructure = &binarySearchTree; } else { std::cerr << "Invalid strategy!" << std::endl; return; } // 打印所有单词及其词频 for (const auto& pair : *selectedStructure) { std::cout << pair.first << ": " << pair.second << std::endl; } }
这个操作用于打印所有单词及其词频,根据选择的策略使用哈希表或二叉搜索树。
5. 主函数
int main() { WordFrequencySystem wordSystem; // 示例文本 std::string text = "This is a sample text. This text is used for testing the word frequency system. "; // 将文本分割成单词并插入系统 std::vector<std::string> words; std::string word; for (char c : text) { if (std::isalnum(c)) { word += std::tolower(c); } else if (!word.empty()) { wordSystem.insertWord(word); word.clear(); } } // 统计使用哈希表策略的单词词频 std::cout << "Word frequencies using hash table strategy:" << std::endl; wordSystem.getAllWordsAndFrequency("hash"); // 统计使用二叉搜索树策略的单词词频 std::cout << "\nWord frequencies using binary search tree strategy:" << std::endl; wordSystem.getAllWordsAndFrequency("bst"); // 检索单词词频 std::string searchWord; std::cout << "\nEnter a word to retrieve its frequency: "; std::cin >> searchWord; int hashFrequency = wordSystem.getWordFrequency(searchWord, "hash"); int bstFrequency = wordSystem.getWordFrequency(searchWord, "bst"); std::cout << "\nFrequency of '" << searchWord << "' using hash table strategy: " << hashFrequency << std::endl; std::cout << "Frequency of '" << searchWord << "' using binary search tree strategy: " << bstFrequency << std::endl; return 0; }
这个主函数演示了如何使用系统,将示例文本分割成单词并插入系统,然后统计并输出使用不同策略的单词词频,最后根据用户输入检索指定单词的词频。
这个例子是一个基本框架,你可以根据实际需求扩展系统的功能,优化性能,并增加错误处理等功能。
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/468345/
转载文章受原作者版权保护。转载请注明原作者出处!