c++实现文本中英文单词和汉字字符的统计
源代码下载:http://download.csdn.net/detail/nuptboyzhb/4987141
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]

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]

参考:实验室小熊
c++实现文本中英文单词和汉字字符的统计的更多相关文章
- 题目--统计一行文本的单词个数(PTA预习题)
PTA预习题——统计一行文本的单词个数 7-1 统计一行文本的单词个数 (15 分) 本题目要求编写程序统计一行字符中单词的个数.所谓“单词”是指连续不含空格的字符串,各单词之间用空格分隔,空格数可以 ...
- 《c程序设计语言》读书笔记--统计 行数、单词数、字符数
#include <stdio.h> int main() { int lin = 0,wor = 0,cha = 0; int flag = 0; int c; while((c = g ...
- C语言输出单个汉字字符
#include "stdio.h" #include "windows.h" int main() { ] = { "多字节字符串!OK!" ...
- shell统计文本中单词的出现次数
Ubuntu14.04 给定一个文本,统计其中单词出现的次数 方法1 # solution 1 grep与awk配合使用,写成一个sh脚本 fre.sh sh fre.sh wordfretest.t ...
- JS实现文本中查找并替换字符
JS实现文本中查找并替换字符 效果图: 代码如下,复制即可使用: <!DOCTYPE html><html> <head> <style type=" ...
- java统计文本中单词出现的个数
package com.java_Test; import java.io.File; import java.util.HashMap; import java.util.Iterator; imp ...
- C 循环统计输入的单词个数和字符长度
C 循环统计输入的单词个数和字符长度 #include <stdio.h> #include <Windows.h> int main(void) { ]; ; ; print ...
- linux wc 的用法-linux 下统计行数、单词数、字符个数
linux wc 的用法-linux 下统计行数.单词数.字符个数 wc : wc -l 统计有多少行 wc -w 统计有多少个单词 wc -c 统计有多少个字符
- 华为oj之字符个数统计
题目:字符个数统计 热度指数:4720 时间限制:1秒 空间限制:32768K 本题知识点: 字符串 题目描述 编写一个函数,计算字符串中含有的不同字符的个数.字符在ACSII码范围内(0~127). ...
随机推荐
- TestNG使用总结
1.Eclipse中安装TestNG的方法 打开Eclipse Help ->Install New Software , 然后Add "http://beust.com/ ...
- 基于css3新属性transform及原生js实现鼠标拖动3d立方体旋转
基于css3新属性transform,实现3d立方体的旋转 通过原生JS,点击事件,鼠标按下.鼠标抬起和鼠标移动事件,实现3d立方体的拖动旋转,并将旋转角度实时的反应至界面上显示 实现原理:通过获取鼠 ...
- asp.net 点击按钮,页面没有任何变化,后台代码不触发
asp.net 点击按钮,页面没有任何变化,后台代码不触发 和可能是 asp.net button 缺少validationGroup 导致的,需要查看页面的validation并且让他们抛出错误信 ...
- “Cache-control”常见的取值有private、no-cache、max-age、must-revalidate等
网页的缓存由HTTP消息头中的"Cache-Control" 来控制的,常见的取值有private.no-cache.max-age.must-revalidate等,默认为pri ...
- php curl 伪造IP来源的代码分享
php curl 可以模仿用户登录,还可以模仿用户IP地址.伪造IP来源. 1,curl发出请求的文件fake_ip.php: <?php $ch = curl_init(); $url = & ...
- Cadence封装制作之表贴封装的制作
以0805封装为例 1.打开PCB editor-> Allegro PCB Design XL 2.File -> New ① Drawing Type -> Package Sy ...
- 拥抱ARM妹子 序章!ARM妹子~~ 哥我来啦!
一个负心汉即将移情别恋,从51转到ARM妹子啦?其实8是的,俺准备开后宫.哇——咔~咔~~.考虑功耗和成本等问题,只有51肯定是不够的,所以嘛~~(一脸坏笑)嘿嘿~~,ARM妹子俺追定了.出于对ARM ...
- 十六、mysql 分区之 简单sql优化2
.索引的分类 B-Tree 基本支持 HASH 只有memory支持 R-Tree myisam支持 Full-text myisam支持(全文索引) .Memory引擎下只有“=”条件才会使用索引 ...
- HDFS导论(转)
1.流式数据访问 HDFS的构建思想是这样的:一次写入,多次读取是最高效的访问模式.数据集通常有数据源生成或从数据源复制而来,接着长时间在此数据集上进行各类分析.每次分析都将设计数据集的大部分数据甚至 ...
- jQuery uploadify-v3.1 批量上传
引用: <link href="/UI.Web.CRM.Main/jQuery.Uploadify/uploadify.css" rel="stylesheet&q ...