DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  4737 随笔 :: 2 文章 :: 542 评论 :: 1615万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

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

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

[C++ code]

  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]

  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]


参考:实验室小熊
posted on   DoubleLi  阅读(3471)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示