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). ...
随机推荐
- Learn Python The Hard Way学习笔记001
今天搜索了一下raw_input() 和 input()的区别,引用下原文部分内容 两个函数均能接收 字符串 ,但 raw_input() 直接读取控制台的输入(任何类型的输入它都可以接收).而对于 ...
- 实例介绍Cocos2d-x物理引擎:使用关节
在游戏中我们可以通过关节约束两个物体的运动.我们通过一个距离关节实例,介绍一下如何在使用关节. 这个实例的运行后的场景如图所示,当场景启动后,玩家可以触摸点击屏幕,每次触摸时候,就会在触摸点和附近生成 ...
- ASP.NET中使用开源插件zTree的小结
在最近的项目应用中,找到了zTree免费的好东西,这里总结一下: 源码下载:http://www.ztree.me/ 效果是酱紫的: 前台代码: 样式和脚本 <link rel="st ...
- ajax — get? or post?
ajax - get? or post? 与 POST 相比,GET 更简单也更快,并且在大部分情况下都能用. 然而,在以下情况中,请使用 POST 请求: 无法使用缓存文件(更新服务器上的文件或数据 ...
- CSS的继承与优先级
CSS样式继承性 body,div,p{} html文档可以上图的种种节点树的形式表示,css层叠样式表中的各元素也有这种对应关系 <body>是文档中最大的根节点,body中的所有元素都 ...
- windows phone URI映射
UriMapping用于在一个较短的URI和你项目中的xaml页的完整路径定义一个映射(别名).通过使用别名URI,开发者可以在不改变导航代码的情况下来改变一个项目的内部结构.该机制还提供了一个简单的 ...
- google map 点击获取经纬度
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- php截取字符串的实例代码(支持utf-8)
分享下php中截取字符串的例子,支持utf-8格式. 1,截取字符串 <?php $string="2006年4月我又长大了一岁!"; echo substr($string ...
- web一次请求的流程
1.客户端(浏览器输入网址)请求 2.发送http协议到web服务器(nginx),检测请求类别,如果时纯静态页面,则返响应返回给客户端. 3.如果有动态脚本(php语法)启动fastcgi进程,用解 ...
- 关于maven参数过滤
一.maven通过设置过滤器,可以使maven在编译打包时实现参数过滤的功能(详细配置说明略) <filters> <filter>../antx.properties< ...