计算单词的频率

1、getline() 将读取的每一行保存至 vector<string> v_lines

2、每循环一次 将一行(*iter_line)划分成一个个单词,保存至 vector<string> v_words,并统计至 map<string,int> m_words;

代码:

  1 /*
  2  * FILE: WordFrequency.cpp
  3  * -----------------------
  4  * DATE: 20170819
  5  * This program computes the frequency of words in a text file.
  6  *
  7  */
  8 
  9 #include <iostream>
 10 #include <iomanip>      // setw(n) 将输出字段的宽度设置为n个字符
 11 #include <fstream>
 12 #include <vector>
 13 #include <map>
 14 #include <algorithm>    // transform() 将字符串string转为小写
 15 
 16 void OpenFile(std::ifstream &infile)
 17 {
 18         std::string prompt = "Please input filename: ";
 19         while(true)
 20         {
 21                 std::cout<< prompt;
 22                 std::string filename;
 23                 getline(std::cin, filename);    // 将输入的文件名保存至变量filename
 24                 // 打开文件 infile.open("test.txt");
 25                 // 如果文件名存储在string变量中,则 infile.open(filename.c_str());
 26                 infile.open(filename.c_str());
 27                 if(!infile.fail())
 28                         return;
 29                 infile.clear(); //重新输入前调用clear重置
 30                 std::cout<< "Unable to open the file. Try again." <<std::endl;
 31         }
 32 }
 33 
 34 /* 
 35  * Extracts words from the line into the string vector words 
 36  * 将一行字符串 提取为一个个单词 存储至 顺序容器vector
 37  * extract: 提取
 38  */
 39 void extractWords(std::string line, std::vector<std::string> &v_words)
 40 {
 41         v_words.clear();
 42         int start = -1;
 43         for(int i = 0; i < line.length(); i++)
 44         {
 45                 if(isalpha(line[i]))    // isalpha() 如果是字母,则返回true
 46                 {
 47                         if(start == -1)
 48                                 start = i;
 49                 }
 50                 else
 51                 {
 52                         if(start >= 0)
 53                         {
 54                                 //push_back() 向顺序容器vector中 添加元素
 55                                 //substr(pos, n) 从pos位置开始截取n个字符,或直到末尾
 56                                 v_words.push_back(line.substr(start, i - start));
 57                                 start = -1;
 58                         }
 59                 }
 60         }
 61         if(start >= 0)
 62                 v_words.push_back(line.substr(start));
 63 }
 64 
 65 /*
 66  * Counts words in the input stream, storing the results in map<string,int>
 67  * 关联容器 map
 68  */
 69 void countWords(std::istream &is, std::map<std::string,int> &m_words)
 70 {
 71         std::vector<std::string> v_lines;
 72         std::vector<std::string> v_words;
 73         std::string str;
 74 
 75         while(getline(is, str)) // getline() 一行一行读取
 76                 v_lines.push_back(str); // 将每行字符串保存至 顺序容器vector
 77         for(std::vector<std::string>::iterator iter_line = v_lines.begin(); iter_line != v_lines.end(); iter_line++)
 78         {
 79                 extractWords(*iter_line, v_words);      // 每循环一次,提取一行中的单词
 80                 for(std::vector<std::string>::iterator iter_word = v_words.begin(); iter_word != v_words.end(); iter_word++)
 81                 {
 82                         // 将提取的单词 统计并保存至关联容器map
 83                         // 库<algorithm> 中的 transform() 将字符串转为小写
 84                         transform((*iter_word).begin(), (*iter_word).end(), (*iter_word).begin(), ::tolower);
 85                         m_words[*iter_word]++;  //保存至 关联容器map
 86                 }
 87         }
 88 }
 89 
 90 /* Display the map */
 91 void displayMap(std::map<std::string,int> &m)
 92 {
 93         for(std::map<std::string,int>::iterator iter = m.begin(); iter != m.end(); iter++)
 94         // 库<iomanip> 中的setw() 设置输出字段的宽度
 95                 std::cout<< std::left << std::setw(15) << iter->first
 96                         << std::right << std::setw(15) << iter->second <<std::endl;
 97 }
 98 
 99 int main()
100 {
101         std::ifstream infile;
102         std::map<std::string,int> m_words;
103         OpenFile(infile);
104         countWords(infile, m_words);
105 
106         infile.close();
107         displayMap(m_words);
108         return 0;
109 }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python单词的出现频率可以通过以下步骤计算: 1. 将文本文件读入Python中 2. 将所有的大写字母转换成小写字母,以避免单词大小写不匹配 3. 去除所有的标点符号和特殊字符,只留下单词 4. 将所有的单词转换成字典中的键,并将出现次数记录在字典的值中 5. 对字典按照值进行排序,输出出现频率最高的单词 示例代码: ```python import string def count_words(filename): """计算单词出现的频率""" # 创建空字典 word_counts = {} # 打开文件 with open(filename) as file: # 遍历文件中的每一行 for line in file: # 去除空格和换行符 line = line.strip() # 去除标点符号和特殊字符 line = line.translate(str.maketrans("", "", string.punctuation)) # string.punctuation包含所有的标点符号 # 将大写字母转换成小写字母 line = line.lower() # 拆分行中的单词 words = line.split() # 遍历所有的单词,并将出现次数记录在字典中 for word in words: if word not in word_counts: word_counts[word] = 1 else: word_counts[word] += 1 # 对字典按照值进行排序 sorted_word_counts = sorted(word_counts.items(), key=lambda x: x[1], reverse=True) # 输出出现频率最高的单词 for word, count in sorted_word_counts[:10]: print(word, count) # 测试 count_words("test.txt") ``` 上述代码中打开的文件名为 test.txt,可以根据需要进行修改。输出结果中包含出现频率最高的前10个单词及其出现次数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值