c++实现文本中英文单词和汉字字符的统计
2013-01-12 10:05
363 查看
源代码下载:http://download.csdn.net/detail/nuptboyzhb/4987141
1.统计文本中汉字的频数,为后续的文本分类做基础。对于汉字的统计,需要判断读取的是否为汉字。源代码如下:
[C++ code]
[图1]
![](http://img.my.csdn.net/uploads/201301/12/1357956593_7825.png)
2.统计英文单词的出现频率。这比统计汉字更加的容易,因为单词和单词之间是用空格分开的,所以,直接将单词保存到string中即可。
[c++ code]
[图2]
![](http://img.my.csdn.net/uploads/201301/12/1357956646_6671.png)
参考:实验室小熊
1.统计文本中汉字的频数,为后续的文本分类做基础。对于汉字的统计,需要判断读取的是否为汉字。源代码如下:
[C++ code]
/* *@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("test.txt"); if (!infile) cout<<"can not open file"<<endl; string s=""; map<string,int>wordcount; unsigned char temp[2]; while(true)//国标2312 { infile>>temp[0]; if(infile.eof()) break; if (temp[0]>=0xB0)//GB2312下的汉字,最小是0XB0 { s+=temp[0]; infile>>temp[1]; s+=temp[1]; } else//非汉字字符不统计 { s=""; continue; } wordcount[s]++; 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<<" s>"<<endl; } int main() { int k=0; cout<<"http://blog.csdn.net/NUPTboyZHB\n"; while (true) { cout<<"查看前K个频率最高的汉字,K="; cin>>k; if(k<=0)break; topK(k); } return 0; }
[图1]
![](http://img.my.csdn.net/uploads/201301/12/1357956593_7825.png)
2.统计英文单词的出现频率。这比统计汉字更加的容易,因为单词和单词之间是用空格分开的,所以,直接将单词保存到string中即可。
[c++ code]
/* *@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; }
[图2]
![](http://img.my.csdn.net/uploads/201301/12/1357956646_6671.png)
参考:实验室小熊
相关文章推荐
- c++实现文本中英文单词和汉字字符的统计
- c++实现文本中英文单词和汉字字符的统计
- 【编程珠玑】第十五章--字符串:统计文本中单词数量(C++的Map&C的结构体实现&POJ2418)
- 统计分析文章中英文单词出现次数及频率(C++实现)
- 一个简单的程序,统计文本文档中的单词和汉字数,逆序排列(出现频率高的排在最前面)。python实现。
- 关于统计ascii码文本中英语单词的出现频率的c++实现
- Python实现统计文本当中单词的数量,
- 学习C/C++语言:字符数组的使用,统计字符串中单词个数
- 统计文件中单词个数--c++实现
- c++ 统计英文文本中每个单词的词频并且按照词频对每行排序
- 利用二叉搜索树来实现输入文本的单词统计
- 【ThinkingInC++】4、统计txt文本中单词的个数
- 编译器DIY之———统计英文文本中的单词数,字符数和行数
- 统计文本中英文单词的出现频率
- 统计一个文本的字符、单词数
- c++第二周任务三:/*(3‐1)输入一行字符,统计其中有多少个单词
- python实现统计汉字/英文单词数的正则表达式
- c++ 统计英文文本中每个单词的词频并且按照词频对每行排序
- 统计一篇文本中空格、单词、句子、非字母字符个数
- [NLP自然语言处理]计算熵和KL距离,java实现汉字和英文单词的识别,UTF8变长字符读取