源代码下载: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. ifstream infile("test.txt");
  19. if (!infile)
  20. cout<<"can not open file"<<endl;
  21. string s="";
  22. map<string,int>wordcount;
  23. unsigned char temp[2];
  24. while(true)//国标2312
  25. {
  26. infile>>temp[0];
  27. if(infile.eof()) break;
  28. if (temp[0]>=0xB0)//GB2312下的汉字,最小是0XB0
  29. {
  30. s+=temp[0];
  31. infile>>temp[1];
  32. s+=temp[1];
  33. }
  34. else//非汉字字符不统计
  35. {
  36. s="";
  37. continue;
  38. }
  39. wordcount[s]++;
  40. s="";
  41. }
  42. cout<<"单词种类:"<<wordcount.size()<<endl;
  43. //优先队列使用小顶堆,排在前面的数量少,使用">";
  44. priority_queue< pair< int,string >,vector< pair< int,string > >,greater< pair< int,string> > > queueK;
  45. for (map<string,int>::iterator iter=wordcount.begin(); iter!=wordcount.end(); iter++)
  46. {
  47. queueK.push(make_pair(iter->second,iter->first));
  48. if(queueK.size()>K)
  49. queueK.pop();
  50. }
  51. pair<int,string>tmp;
  52. //将排在后面的数量少,排在前面的数量多
  53. priority_queue< pair< int,string >,vector< pair< int,string > >,less< pair< int,string> > > queueKless;
  54. while (!queueK.empty())
  55. {
  56. tmp=queueK.top();
  57. queueK.pop();
  58. queueKless.push(tmp);
  59. }
  60. while(!queueKless.empty())
  61. {
  62. tmp=queueKless.top();
  63. queueKless.pop();
  64. cout<<tmp.second<<"\t"<<tmp.first<<endl;
  65. }
  66. cout<<"< Elapsed Time: "<<(clock()-t)/CLOCKS_PER_SEC<<" s>"<<endl;
  67. }
  68. int main()
  69. {
  70. int k=0;
  71. cout<<"http://blog.csdn.net/NUPTboyZHB\n";
  72. while (true)
  73. {
  74. cout<<"查看前K个频率最高的汉字,K=";
  75. cin>>k;
  76. if(k<=0)break;
  77. topK(k);
  78. }
  79. return 0;
  80. }

[图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. ifstream infile;
  19. infile.open("test.txt");
  20. if (!infile)
  21. cout<<"can not open file"<<endl;
  22. string s;
  23. map<string,int>wordcount;
  24. while(true)
  25. {
  26. infile>>s;
  27. if(infile.eof()) break;
  28. wordcount[s]++;
  29. }
  30. cout<<"单词种类:"<<wordcount.size()<<endl;
  31. //优先队列使用小顶堆,排在前面的数量少,使用">";
  32. priority_queue< pair< int,string >,vector< pair< int,string > >,greater< pair< int,string> > > queueK;
  33. for (map<string,int>::iterator iter=wordcount.begin(); iter!=wordcount.end(); iter++)
  34. {
  35. queueK.push(make_pair(iter->second,iter->first));
  36. if(queueK.size()>K)
  37. queueK.pop();
  38. }
  39. pair<int,string>tmp;
  40. priority_queue< pair< int,string >,vector< pair< int,string > >,less< pair< int,string> > > queueKless;
  41. while (!queueK.empty())
  42. {
  43. tmp=queueK.top();
  44. queueK.pop();
  45. queueKless.push(tmp);
  46. }
  47. while(!queueKless.empty())
  48. {
  49. tmp=queueKless.top();
  50. queueKless.pop();
  51. cout<<tmp.second<<"\t"<<tmp.first<<endl;
  52. }
  53. cout<<"< Elapsed Time: "<<(clock()-t)/CLOCKS_PER_SEC<<" >"<<endl;
  54. }
  55. int main()
  56. {
  57. int k=0;
  58. cout<<"http://blog.csdn.net/NUPTboyZHB\n";
  59. while (true)
  60. {
  61. cout<<"PUT IN K: ";
  62. cin>>k;
  63. if(k<=0)break;
  64. topK(k);
  65. }
  66. return 0;
  67. }

[图2]

参考:实验室小熊

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

  1. 题目--统计一行文本的单词个数(PTA预习题)

    PTA预习题——统计一行文本的单词个数 7-1 统计一行文本的单词个数 (15 分) 本题目要求编写程序统计一行字符中单词的个数.所谓“单词”是指连续不含空格的字符串,各单词之间用空格分隔,空格数可以 ...

  2. 《c程序设计语言》读书笔记--统计 行数、单词数、字符数

    #include <stdio.h> int main() { int lin = 0,wor = 0,cha = 0; int flag = 0; int c; while((c = g ...

  3. C语言输出单个汉字字符

    #include "stdio.h" #include "windows.h" int main() { ] = { "多字节字符串!OK!" ...

  4. shell统计文本中单词的出现次数

    Ubuntu14.04 给定一个文本,统计其中单词出现的次数 方法1 # solution 1 grep与awk配合使用,写成一个sh脚本 fre.sh sh fre.sh wordfretest.t ...

  5. JS实现文本中查找并替换字符

    JS实现文本中查找并替换字符 效果图: 代码如下,复制即可使用: <!DOCTYPE html><html> <head> <style type=" ...

  6. java统计文本中单词出现的个数

    package com.java_Test; import java.io.File; import java.util.HashMap; import java.util.Iterator; imp ...

  7. C 循环统计输入的单词个数和字符长度

    C 循环统计输入的单词个数和字符长度 #include <stdio.h> #include <Windows.h> int main(void) { ]; ; ; print ...

  8. linux wc 的用法-linux 下统计行数、单词数、字符个数

    linux wc 的用法-linux 下统计行数.单词数.字符个数   wc : wc -l 统计有多少行 wc -w 统计有多少个单词 wc -c 统计有多少个字符

  9. 华为oj之字符个数统计

    题目:字符个数统计 热度指数:4720 时间限制:1秒 空间限制:32768K 本题知识点: 字符串 题目描述 编写一个函数,计算字符串中含有的不同字符的个数.字符在ACSII码范围内(0~127). ...

随机推荐

  1. SQL按照日、周、月、年统计数据

    写sql语句分别按日,星期,月,季度,年统计销售额 --按日 select sum(consume),day([date]) from consume_record where year([date] ...

  2. 教您如何使用SQL中的SELECT LIKE like语句

    LIKE语句在SQL有着不可替代的重要作用,下文就将为您介绍SQL语句中SELECT LIKE like的详细用法,希望对您能有所帮助. LIKE语句的语法格式是:select * from 表名 w ...

  3. Swift类型检查与转换

    继承会发生在子类和父类中,如图所示,是一系列类的继承关系类图,Person是类层次结构中的根类,Student是Person的直接子类,Worker是Person的直接子类.这个继承关系类图的具体实现 ...

  4. C# 线程--第四线程实例

    概述 在前面几节中和大家分享了线程的一些基础使用方法,本章结合之前的分享来编写一些日常开发中应用实例,和编写多线程时一些注意点.如大家有好的实例也欢迎分享.. 应用实例 应用:定时任务程序 场景:系统 ...

  5. 使用PSD设计网页页面

    一.一个独立的页面 1.分析这个页面,在脑海或草稿上要确立页面板块布局(如版块区域的,位置和大小)2.根据设计稿的的情况,分析背景图的分布.ICO图的分布等 3.切割相应的图片,导出.合并图片(一般用 ...

  6. Git 技巧小结

    本篇博客内的内容,主要摘抄自 廖雪峰的 Git教程,这篇教程写的通俗易懂,步步深入,是我见过最棒的Git教程了.下面的全部内容,摘抄自此教程,有需要的朋友,请看完整版. Git版本库 git在创建版本 ...

  7. 管理员取得所有权(复制以下代码粘贴至记事本然后把后缀名改为reg)

    Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\shell\runas] @="获取权限" "NoWo ...

  8. 多项分布(multinominal distribution)

    简介 更一般性的问题会问:“点数1~6的出现次数分别为(x1,x2,x3,x4,x5,x6)时的概率是多少?其中sum(x1~x6)= n”.这就是一个多项式分布. 定义 把二项分布推广至多个(大于2 ...

  9. 使用分部类给Models添加验证Attributes

    网摘1: 在使用Entity Framework 的Database frist或model first时,直接加attribute到modle类上是太现实也不合理的,因为model类是自动生成的,重 ...

  10. C# 导出 Excel

    /// <summary> /// 导出Excel /// </summary> public void ExportExcel() { #region 添加引用 Micros ...