统计文本中英文单词的出现频率
2014-11-05 19:29
597 查看
/* *@author:郑海波 http://blog.csdn.net/NUPTboyZHB *参考:实验室小熊 *注:有删改 */ #pragma warning(disable:4786) #include <iostream> #include <vector> #include <fstream> #include <string> #include <map> #include <queue> #include <ctime> using namespace std; void topK(const int &K) { double t=clock(); ifstream infile; infile.open("test.txt"); if (!infile) cout<<"can not open file"<<endl; string s; map<string,int>wordcount; while(true) { infile>>s; if(infile.eof()) break; wordcount[s]++; } cout<<"单词种类:"<<wordcount.size()<<endl; //优先队列使用小顶堆,排在前面的数量少,使用">"; priority_queue< pair< int,string >,vector< pair< int,string > >,greater< pair< int,string> > > queueK; for (map<string,int>::iterator iter=wordcount.begin(); iter!=wordcount.end(); iter++) { queueK.push(make_pair(iter->second,iter->first)); if(queueK.size()>K) queueK.pop(); } pair<int,string>tmp; priority_queue< pair< int,string >,vector< pair< int,string > >,less< pair< int,string> > > queueKless; while (!queueK.empty()) { tmp=queueK.top(); queueK.pop(); queueKless.push(tmp); } while(!queueKless.empty()) { tmp=queueKless.top(); queueKless.pop(); cout<<tmp.second<<"\t"<<tmp.first<<endl; } cout<<"< Elapsed Time: "<<(clock()-t)/CLOCKS_PER_SEC<<" >"<<endl; } int main() { int k=0; cout<<"http://blog.csdn.net/NUPTboyZHB\n"; while (true) { cout<<"PUT IN K: "; cin>>k; if(k<=0)break; topK(k); } return 0; }
相关文章推荐
- 个人项目:统计文本中的单词出现频率
- 统计一个英文文本的单词出现的频率(有标点符号的)
- 用hash表统计文本文件中每个单词出现的频率
- 统计文本文件中单词出现频率(用java集合框架编写)
- python实现统计文本中单词出现的频率
- 一个简单的程序,统计文本文档中的单词和汉字数,逆序排列(出现频率高的排在最前面)。python实现。
- 统计文本中每个单词出现的频率(附C++完整程序)
- 统计文本文件中单词出现频率(用java集合框架编写)
- 统计文本中各单词出现的频率(JavaWeb)
- 统计文本文件中单词出现频率,自己编写的Java小程序
- 模拟MapReduce编程的程序案例(用于统计文本中单词出现频率)
- Hadoop:使用原生python编写MapReduce来统计文本文件中所有单词出现的频率功能
- 基于bf算法统计文本中某个单词出现的频率
- 关于统计ascii码文本中英语单词的出现频率的c++实现
- go语言之map练习(二):编写一个程序wordfreq程序,统计输入文本中每个单词出现的频率(次数)
- 统计英文文本单词出现频率
- 统计文本中出现单词的频率
- 如何使用linux命令统计文本中某个单词的出现频率
- Java编程:统计文本文件中单词出现频率
- 统计英文文本文档中前十个出现频率最多的单词