编写程序统计一个英文文本文件中每个单词的出现次数

您所在的位置:网站首页 厨房和鸡肉英文单词区分 编写程序统计一个英文文本文件中每个单词的出现次数

编写程序统计一个英文文本文件中每个单词的出现次数

2024-03-13 09:28| 来源: 网络整理| 查看: 265

目录

题目简述

代码编写

Python代码实现

C语言代码实现

C++代码实现

题目简述

【问题描述】 编写程序统计一个英文文本文件中每个单词的出现次数(词频统计),并将统计结果按单词字典序输出到屏幕上。 注:在此单词为仅由字母组成的字符序列。包含大写字母的单词应将大写字母转换为小写字母后统计。

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

【输出形式】 程序将单词统计结果按单词字典序输出到屏幕上,每行输出一个单词及其出现次数,单词和其出现次数间由一个空格分隔,出现次数后无空格, 直接为回车。

【样例输入】 当前目录下文件article.txt内容如下: “Do not take to heart every thing you hear."“Do not spend all that you have." “Do not sleep as long as you want,”

【样例输出】

all 1

as 2

do 3

every 1

have 1

……

代码编写 Python代码实现 import re def word_count(file_path): # 创建一个空字典以存储单词频率 word_dict = {} # 打开文件并逐行读取 with open(file_path, 'r', encoding='utf-8') as file: # 遍历文件中的每一行 for line in file: # 使用正则表达式在行中查找所有单词(忽略大小写) words = re.findall(r'\b\w+\b', line.lower()) # 遍历每个单词并在字典中更新单词频率 for word in words: word_dict[word] = word_dict.get(word, 0) + 1 # 按字典序对单词字典进行排序 sorted_word_dict = sorted(word_dict.items()) # 打印每个单词及其频率 for word, count in sorted_word_dict: print(f'{word} {count}') if __name__ == "__main__": # 设置文件路径 file_path = r"D:\untitled13\9.2\.vscode\article.txt" # 调用 word_count 函数并传入指定的文件路径 word_count(file_path)

请注意,该程序会将文件中的每一行都分词,并使用正则表达式 `\b\w+\b` 来匹配由字母组成的单词。然后,将单词转换为小写,并使用字典进行词频统计。最后,将统计结果按单词字典序排序,并输出到屏幕上。

C语言代码实现 #include #include #include #include #define MAX_WORD_LENGTH 100 // 结构体用于存储单词和其频率 struct WordFrequency { char word[MAX_WORD_LENGTH]; int count; }; // 比较函数用于排序 int compare(const void *a, const void *b) { return strcmp(((struct WordFrequency *)a)->word, ((struct WordFrequency *)b)->word); } // 函数用于统计单词频率并按字典序输出 void word_count(const char *file_path) { FILE *file = fopen(file_path, "r"); if (file == NULL) { perror("Error opening file"); exit(EXIT_FAILURE); } struct WordFrequency *word_freq_array = NULL; int array_size = 0; // 读取文件内容 char line[256]; while (fgets(line, sizeof(line), file) != NULL) { char *token = strtok(line, " \t\n"); while (token != NULL) { // 将单词转换为小写 for (int i = 0; token[i]; i++) { token[i] = tolower(token[i]); } // 在 word_freq_array 中查找单词 int found = 0; for (int i = 0; i < array_size; i++) { if (strcmp(word_freq_array[i].word, token) == 0) { word_freq_array[i].count++; found = 1; break; } } // 如果单词未找到,则添加到 word_freq_array 中 if (!found) { array_size++; word_freq_array = realloc(word_freq_array, array_size * sizeof(struct WordFrequency)); if (word_freq_array == NULL) { perror("Memory allocation error"); exit(EXIT_FAILURE); } strcpy(word_freq_array[array_size - 1].word, token); word_freq_array[array_size - 1].count = 1; } token = strtok(NULL, " \t\n"); } } fclose(file); // 按字典序对单词进行排序 qsort(word_freq_array, array_size, sizeof(struct WordFrequency), compare); // 输出排序后的单词及其频率 for (int i = 0; i < array_size; i++) { printf("%s %d\n", word_freq_array[i].word, word_freq_array[i].count); } // 释放动态分配的内存 free(word_freq_array); } int main() { const char *file_path = "D:\\untitled13\\9.2\\.vscode\\article.txt"; word_count(file_path); return 0; } C++代码实现 #include #include #include #include #include #include struct WordFrequency { std::string word; int count; }; // 比较函数用于排序 bool compare(const WordFrequency& a, const WordFrequency& b) { return a.word < b.word; } // 函数用于统计单词频率并按字典序输出 void word_count(const std::string& file_path) { std::ifstream file(file_path); if (!file.is_open()) { std::cerr word) { // 将单词转换为小写 std::transform(word.begin(), word.end(), word.begin(), ::tolower); // 更新单词频率 word_freq_map[word]++; } } file.close(); // 将频率统计结果存入结构体数组 std::vector 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); // 输出排序后的单词及其频率 for (const auto& word_freq : word_freq_vector) { std::cout


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3