湖南大学-数据结构-实验四 词频统计

问题描述:

【问题描述】

   编写程序统计一个英文文本文件中每个单词的出现次数(词频统计),并将统计结果按单词出现频率由高至低输出到指定文件中。

   本题采用的数据结构为trie树,也称为字典树,具体请参考相关资料。

   注:在此单词为仅由字母组成的字符序列。包含大写字母的单词应将大写字母转换为小写字母后统计。此外,由于输入文件是英文小说,因此不会出现无意义的单词。

【输入形式】

   打开当前目录下文件in.txt,从中读取英文单词进行词频统计。
【输出形式】

   按单词出现次数由高至低输出前100个(不足100个按实际个数输出),每行输出一个单词及其出现次数,单词和其出现次数间由一个空格分隔。

如图:

废话不多说,直接上代码

如果出现运行时间过长,就再次提交,每次样例都不一样


#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <vector>
#include <algorithm>


const std::string file_path = "in.txt";
const int sum = 100;
struct WordFrequency {
    std::string word;
    int count;
};

// 比较函数用于排序
bool compare(const WordFrequency& a, const WordFrequency& b) {
    if (a.count != b.count)
        return a.count > b.count;
    else
        return a.word < b.word;
}

// 函数用于统计单词频率并按字典序输出
void word_count(const std::string& file_path) {
    std::ifstream file(file_path);
    if (!file.is_open()) {
        std::cerr << "Error opening file." << std::endl;
        std::ofstream ofs;
        ofs.open(file_path, std::ios::out);//用输出的方式打开文件--写文件 

        exit(EXIT_FAILURE);
    }

    std::map<std::string, int> word_freq_map;

    // 读取文件内容
    std::string line;
    while (std::getline(file, line)) {
        std::istringstream iss(line);
        std::string word;
        // 分词并统计单词频率
        while (iss >> word) {
            
            std::string currentWord;

            for (char ch : word) {
                // 检查字符是否为英文字母
                if (std::isalpha(ch)) {
                    // 如果是字母,则追加到当前单词
                    currentWord += ch;
                }
                else if (!currentWord.empty()) {
                    // 如果当前单词不为空,将其添加到单词列表中
                     // 将单词转换为小写
                    std::transform(currentWord.begin(), currentWord.end(), currentWord.begin(), ::tolower);

                    // 更新单词频率
                    word_freq_map[currentWord]++;
                    currentWord.clear(); // 清空当前单词,为下一个单词做准备
                }
                 将单词转换为小写
                //std::transform(currentWord.begin(), currentWord.end(), currentWord.begin(), ::tolower);

                 更新单词频率
                //word_freq_map[currentWord]++;
            }
            if (!currentWord.empty()) {
                std::transform(currentWord.begin(), currentWord.end(), currentWord.begin(), ::tolower);

                // 更新单词频率
                word_freq_map[currentWord]++;
            }
        }
    }

    file.close();

    // 将频率统计结果存入结构体数组
    std::vector<WordFrequency> word_freq_vector;
    for (const auto& pair : word_freq_map) {
        word_freq_vector.push_back({ pair.first, pair.second });
    }

    // 按字典序对单词进行排序
    std::sort(word_freq_vector.begin(), word_freq_vector.end(), compare);

    // 输出排序后的单词及其频率
    if (word_freq_map.size() <= sum) {
        for (const auto& word_freq : word_freq_vector) {
            std::cout << word_freq.word << " " << word_freq.count << std::endl;
        }
    }
    else {
        for (int i = 0; i < sum; i++) {
            const auto& word_freq = word_freq_vector[i];
            std::cout << word_freq.word << " " << word_freq.count << std::endl;

        }
    }
    /*for (const auto& word_freq : word_freq_vector) {
        std::cout << word_freq.word << " " << word_freq.count << std::endl;
    }*/
}

int main() {
    
    word_count(file_path);

    return 0;
}

温馨提示:

1.同学们遇到没有思路的题可以选择上网搜相似的题目,未必是原题

2.根据调试情况再改进

        可以使用AI工具帮你写代码,前提是你要提供正确的思路

3.得到最终的代码,可以再深入学习

  • 14
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值