c++实现文本中英文单词和汉字字符的统计

简介: 源代码下载:http://download.csdn.net/detail/nuptboyzhb/49871411.统计文本中汉字的频数,为后续的文本分类做基础。对于汉字的统计,需要判断读取的是否为汉字。

源代码下载:http://download.csdn.net/detail/nuptboyzhb/4987141

1.统计文本中汉字的频数,为后续的文本分类做基础。对于汉字的统计,需要判断读取的是否为汉字。源代码如下:

[C++ code]

[cpp]   view plain copy
  1. /* 
  2.  *@author:郑海波 http://blog.csdn.net/NUPTboyZHB 
  3.  *参考:实验室小熊 
  4.  *注:有删改 
  5.  */  
  6. #pragma warning(disable:4786)  
  7. #include <iostream>  
  8. #include <vector>  
  9. #include <fstream>  
  10. #include <string>  
  11. #include <map>  
  12. #include <queue>  
  13. #include <ctime>  
  14. using namespace std;  
  15. void topK(const int &K)  
  16. {  
  17.     double t=clock();  
  18.   
  19.     ifstream infile("test.txt");  
  20.     if (!infile)  
  21.         cout<<"can not open file"<<endl;  
  22.   
  23.     string s="";  
  24.     map<string,int>wordcount;  
  25.     unsigned char temp[2];  
  26.     while(true)//国标2312  
  27.     {  
  28.         infile>>temp[0];  
  29.         if(infile.eof()) break;  
  30.         if (temp[0]>=0xB0)//GB2312下的汉字,最小是0XB0  
  31.         {  
  32.             s+=temp[0];  
  33.             infile>>temp[1];  
  34.             s+=temp[1];  
  35.         }  
  36.         else//非汉字字符不统计  
  37.         {  
  38.             s="";  
  39.             continue;  
  40.         }  
  41.         wordcount[s]++;  
  42.         s="";  
  43.     }  
  44.     cout<<"单词种类:"<<wordcount.size()<<endl;  
  45.     //优先队列使用小顶堆,排在前面的数量少,使用">";  
  46.     priority_queue< pair< int,string >,vector< pair< int,string > >,greater< pair< int,string> > > queueK;  
  47.     for (map<string,int>::iterator iter=wordcount.begin(); iter!=wordcount.end(); iter++)  
  48.     {  
  49.         queueK.push(make_pair(iter->second,iter->first));  
  50.         if(queueK.size()>K)  
  51.             queueK.pop();  
  52.     }  
  53.     pair<int,string>tmp;  
  54.     //将排在后面的数量少,排在前面的数量多  
  55.     priority_queue< pair< int,string >,vector< pair< int,string > >,less< pair< int,string> > > queueKless;  
  56.     while (!queueK.empty())  
  57.     {  
  58.         tmp=queueK.top();  
  59.         queueK.pop();  
  60.         queueKless.push(tmp);  
  61.     }  
  62.     while(!queueKless.empty())  
  63.     {  
  64.         tmp=queueKless.top();  
  65.         queueKless.pop();  
  66.         cout<<tmp.second<<"\t"<<tmp.first<<endl;  
  67.     }  
  68.     cout<<"< Elapsed Time: "<<(clock()-t)/CLOCKS_PER_SEC<<" s>"<<endl;  
  69. }  
  70.   
  71. int main()  
  72. {  
  73.     int k=0;  
  74.     cout<<"http://blog.csdn.net/NUPTboyZHB\n";  
  75.     while (true)  
  76.     {  
  77.         cout<<"查看前K个频率最高的汉字,K=";  
  78.         cin>>k;  
  79.         if(k<=0)break;  
  80.         topK(k);  
  81.     }  
  82.     return 0;  
  83. }  


[图1]


2.统计英文单词的出现频率。这比统计汉字更加的容易,因为单词和单词之间是用空格分开的,所以,直接将单词保存到string中即可。

[c++ code]

[cpp]   view plain copy
  1. /* 
  2.  *@author:郑海波 http://blog.csdn.net/NUPTboyZHB 
  3.  *参考:实验室小熊 
  4.  *注:有删改 
  5.  */  
  6. #pragma warning(disable:4786)  
  7. #include <iostream>  
  8. #include <vector>  
  9. #include <fstream>  
  10. #include <string>  
  11. #include <map>  
  12. #include <queue>  
  13. #include <ctime>  
  14. using namespace std;  
  15. void topK(const int &K)  
  16. {  
  17.     double t=clock();  
  18.   
  19.     ifstream infile;  
  20.     infile.open("test.txt");  
  21.     if (!infile)  
  22.         cout<<"can not open file"<<endl;  
  23.     string s;  
  24.     map<string,int>wordcount;  
  25.   
  26.     while(true)  
  27.     {  
  28.         infile>>s;  
  29.         if(infile.eof()) break;  
  30.         wordcount[s]++;  
  31.     }  
  32.     cout<<"单词种类:"<<wordcount.size()<<endl;  
  33.     //优先队列使用小顶堆,排在前面的数量少,使用">";  
  34.     priority_queue< pair< int,string >,vector< pair< int,string > >,greater< pair< int,string> > > queueK;  
  35.     for (map<string,int>::iterator iter=wordcount.begin(); iter!=wordcount.end(); iter++)  
  36.     {  
  37.         queueK.push(make_pair(iter->second,iter->first));  
  38.         if(queueK.size()>K)  
  39.             queueK.pop();  
  40.     }  
  41.     pair<int,string>tmp;  
  42.     priority_queue< pair< int,string >,vector< pair< int,string > >,less< pair< int,string> > > queueKless;  
  43.     while (!queueK.empty())  
  44.     {  
  45.         tmp=queueK.top();  
  46.         queueK.pop();  
  47.         queueKless.push(tmp);  
  48.     }  
  49.     while(!queueKless.empty())  
  50.     {  
  51.         tmp=queueKless.top();  
  52.         queueKless.pop();  
  53.         cout<<tmp.second<<"\t"<<tmp.first<<endl;  
  54.     }  
  55.     cout<<"< Elapsed Time: "<<(clock()-t)/CLOCKS_PER_SEC<<" >"<<endl;  
  56. }  
  57. int main()  
  58. {  
  59.     int k=0;  
  60.     cout<<"http://blog.csdn.net/NUPTboyZHB\n";  
  61.     while (true)  
  62.     {  
  63.         cout<<"PUT IN K: ";  
  64.         cin>>k;  
  65.         if(k<=0)break;  
  66.         topK(k);  
  67.     }  
  68.     return 0;  
  69. }  


[图2]


参考:实验室小熊
目录
相关文章
|
2月前
|
存储 算法 编译器
【C++ 字符数组的模板特化】面向字符串的C++模板特化:理解与实践
【C++ 字符数组的模板特化】面向字符串的C++模板特化:理解与实践
54 1
|
2月前
|
对象存储 C++
在C++语言中字符串流
在C++语言中字符串流
18 2
|
2月前
|
存储 C++ 索引
C++ string容器-字符存取讲解
C++ string容器-字符存取讲解
26 0
|
2月前
|
编译器 C++
c++关键字与三字符组
c++关键字与三字符组
21 0
|
2月前
|
存储 C++
c++字符和不常见常量
c++字符和不常见常量
28 0
|
3月前
|
算法 测试技术 C++
【动态规划】【图论】【C++算法】1575统计所有可行路径
【动态规划】【图论】【C++算法】1575统计所有可行路径
|
3月前
|
人工智能 算法 测试技术
【动态规划】【二分查找】C++算法 466 统计重复个数
【动态规划】【二分查找】C++算法 466 统计重复个数
|
4月前
|
C++ 索引
c++:string相关的oj题(415. 字符串相加、125. 验证回文串、541. 反转字符串 II、557. 反转字符串中的单词 III)
c++:string相关的oj题(415. 字符串相加、125. 验证回文串、541. 反转字符串 II、557. 反转字符串中的单词 III)
44 0
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
1月前
|
算法 测试技术 C#
【字典树】【KMP】【C++算法】3045统计前后缀下标对 II
【字典树】【KMP】【C++算法】3045统计前后缀下标对 II