本文主要是介绍使用map和set实现简单的词频统计,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、运行效果图
二、代码示例
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;class TextQuery {
public:void readFile(const string &filename);void query(const string &word);
private:vector<string> _lines; // 用于保存每一行的内容map<string, set<int>> _wordsNumbers; // 用于保存每个词所在的行号map<string, int> _dict; // 用于保存每个词的词频
};void TextQuery::readFile(const string &filename) {// 打开文件流ifstream ifs(filename);if (!ifs.good()) {ifs.close();cerr << "open " << filename << " is fail" << endl;return;} // 操作数据string line;size_t lineNumber = 0; // 行号while (getline(ifs, line)) {++lineNumber;_lines.push_back(line); // 记录每一行istringstream iss(line);string word;// 一行一行地处理while (getline(iss, word, ' ')) {word.erase(remove_if(word.begin(), word.end(), [](const char c) {return !isalpha(c); // 处理掉非字母部分}),word.end());if (word.size() == 0) { // 跳过空字符continue;}++_dict[word]; // 词频+1// 存入word的行号auto it = _wordsNumbers.find(word);if (it == _wordsNumbers.end()) { // 还没有word对应的keyset<int> numbers;numbers.insert(lineNumber);_wordsNumbers[word] = numbers;} else { // 有对应key,直接插入it->second.insert(lineNumber);}}}// 关闭文件流ifs.close();
}void TextQuery::query(const string &word) {// 先判断是否存在auto dictIt = _dict.find(word);if (dictIt == _dict.end()) {cout << "\"" << word << "\"" << " never appears!" << endl;return;}// 查找数据cout << "\"" << word << "\"" << " occurs " << dictIt->second << ((dictIt->second > 1) ? " times" : " time") << endl;auto wordsNumbersIt = _wordsNumbers.find(word); // 不用再校验for (const auto &lineNumber : wordsNumbersIt->second) {cout << lineNumber << ": ";// 标亮显示行内容istringstream iss(_lines[lineNumber - 1]);string tempWord;while (getline(iss, tempWord, ' ')) {if (word == tempWord) {cout << "\033[31m" << tempWord << "\033[0m" << " "; } else {cout << tempWord << " ";}}cout << endl;}
}void test0() {TextQuery tq;tq.readFile("./test.txt");string word;while (cout << "请输入要查询的关键字:", getline(cin, word)) {tq.query(word);} cout << endl;
}int main(void) {test0();return 0;
}
这篇关于使用map和set实现简单的词频统计的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!