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

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

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

[C++ code]

[cpp] view plaincopy

  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 plaincopy

  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]

参考:实验室小熊

时间: 2024-08-19 12:00:23

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

css中英文字母和汉字行高不同怎么办

文章简介:css中英文字母和汉字行高不一样的解决办法. 最近在写一个css的时候遇到一个问题:英文字母和汉字的行高不一样,导致在全汉字.全英文字母以及汉字字母混合时设置好的margin或padding 属性出现偏差-当然这种情况之存在于IE浏览器,Safari.Google Chrome.Firefox.Opera均不存在类似的问题 产生的原因: 全汉字的时候: 一般情况在选中文本可以看出汉字是上对齐的(具体表现是选中字体,从背景上看上去下面多了一部分,用css术语讲就是产生了padding-b

正则表达式——去除文本中的非汉字(VB2005)

本人由于工作关系,需要一段代码,将给定的字符串中的非汉字去除,只保留汉字部分. 这个一般用正则表达式比较简单,网上有一些匹配汉字的正则表达式,拿来经过改造就能实现我前面的要求. 注释一下,正则表达式[\u4e00-\u9fa5]表示匹配中文,则正则表达式[^\u4e00-\u9fa5]匹配非中文.用Replace方法将匹配的非中文替换为空字符串,也就是去除了文本中的非中文字符. 代码格式修正于2012年1月5日  Imports System.Text.RegularExpressionsPub

PHP实现过滤掉非汉字字符只保留中文字符

  这篇文章主要介绍了PHP实现过滤掉非汉字字符只保留中文字符,本文直接给出实现代码,需要的朋友可以参考下 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <?php   $str = "a 1b 2b<中文>.xxyy字符";   //转换 GB2312 -> UTF-8 $str = mb_convert_encoding($str, 'UTF-8', 'GB2312');   preg_match_all('/[x{4e0

为什么点击文本查询单词就闪退

问题描述 为什么点击文本查询单词就闪退 就是我把别人低版本的词典源代码原装复制过来结果一查单词就闪退,告诉我访问不了数据库.但是我把数据库已经复制过来了呀!下面是报错的地方 05-23 10:14:13.195: E/AndroidRuntime(5635): java.lang.NullPointerException 05-23 10:14:13.195: E/AndroidRuntime(5635): at com.example.nnnnn.Main.afterTextChanged(M

江湖救急:中文汉字字符用正则表达式怎么表示?

问题描述 江湖救急:中文汉字字符用正则表达式怎么表示? 百思不得其解,"[答案]"或"答案 :"用正则表达式怎么表示? 解决方案 正则表达式 - 中文字符的匹配 解决方案二: 如果是单纯的中文汉字,java里面用unicode中文码,那么正则表达式是:String regexStr = "[u4E00-u9FA5]"; 你如果只是需要匹配"答案"这两个中文汉字的话,可以找到这两个中文汉字的unicode分别是u7b54,u68

php ucwords() 函数将字符串中每个单词的首字符转换为大写(实现代码)_php实例

php ucwords() 函数将字符串中每个单词的首字符转换为大写, 本文章向码农介绍php ucwords() 函数的基本使用方法和实例,感兴趣的码农可以参考一下. 定义和用法 ucwords() 函数把字符串中每个单词的首字符转换为大写. 注释:该函数是二进制安全的. 相关函数: lcfirst() - 把字符串中的首字符转换为小写 strtolower() - 把字符串转换为小写 strtoupper() - 把字符串转换为大写 ucfirst() - 把字符串中的首字符转换为大写 语法

PHP实现过滤掉非汉字字符只保留中文字符_php技巧

<?php $str = "a 1b 2b<中文>.xxyy字符"; //转换 GB2312 -> UTF-8 $str = mb_convert_encoding($str, 'UTF-8', 'GB2312'); preg_match_all('/[\x{4e00}-\x{9fff}]+/u', $str, $matches); $str = join('', $matches[0]); //转换 UTF-8 -> GB2312 $str = mb_c

Ruby中实现统计文件行数、单词数和字符数_ruby专题

在Ruby中我们定义一个wc方法,用来统计文件中出现的文本行数.单词数和字符数,ruby代码程序如下: 复制代码 代码如下: def wc(filename)   nline = nword = nchar = 0   File.open(filename) do |io|     io.each_line do |line|       words = line.split(/\s+/).reject{|w| w.empty? }       #本例中使用了split方法分割单词,当行首有空白

c++ 编程问题-输入一串字符,统计ff,fl,fi出现的次数,为什么输入奇数个f就会出错啊?

问题描述 输入一串字符,统计ff,fl,fi出现的次数,为什么输入奇数个f就会出错啊? #include #include using namespace std; int main() { int a=0,b=0,c=0; char aa,bb=' '; vector ss; while(cin.get(aa)) { ss.push_back(aa); } for(vector::iterator i=ss.begin();i!=ss.end();++i) { if(bb=='f') { sw