c++實現文字中英文單詞和漢字字元的統計
[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]
[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++實現文字中英文單詞和漢字字元的統計
1.統計文字中漢字的頻數,為後續的文字分類做基礎。對於漢字的統計,需要判斷讀取的是否為漢字。原始碼如下: [C++ code] /* *@author:鄭海波 http://blog.csdn.
C實現頭插法和尾插法來構建單鏈表(不帶頭結點)
res rgb eof uci fun while data 尾插法 輸入數據 鏈表的構建事實上也就是不斷插入節點的過程。而節點的插入能夠分為頭插法和尾插法。頭插法就是在頭結點後插入該節點,始終把該節點作為第一個節點。尾插法就是在鏈表的最後一個節點處插入元
C#實現左截取和右截取字符串實例
poi renren www. itl text ear 我們 沒有 splay 本文實例講述了C#實現左截取和右截取字符串的方法,分享給大家供大家參考。具體方法分析如下: 問題如下: 使用C#語法編寫程序時,我們需要截取一個字符串左邊或右邊的若幹個字符,該如何操作呢?
c#實現wc基本功能和擴展功能
length lan soft 表格 command 這一 play nal 就會 c#實現wc基本功能和擴展功能 github:鏈接 一、項目要求 wc.exe 是一個常見的工具,它能統計文本文件的字符數、單詞數和行數。這個項目要求寫一個命令行程序,模仿已有wc.exe
C++實現快取演算法LRU和LFU
LRU的實現 運用你所掌握的資料結構,設計和實現一個LRU (最近最少使用) 快取機制 。它應該支援以下操作: 獲取資料 get 和 寫入資料 put 。 獲取資料 get(key) - 如果金鑰 (key) 存在於快
C實現繼承、封裝和多型思路
1、繼承 把父類資料結構放在子資料結構的首位置,方便以後資料訪問和資料的強轉 struct parent{ int info; }; struct child { s
android實現文字漸變效果和歌詞進度的效果
要用TextView使用漸變色,那我們就必須要了解LinearGradient(線性漸變)的用法。 LinearGradient的引數解釋 LinearGradient也稱作線性渲染,LinearGradient的作用是實現某一區域內顏色的線性漸變效果,
用c#實現簡單的登入和註冊功能
這兩天c#大作業要求做一個簡單的通訊錄系統,我就先做了登入和註冊的功能,在網上看了一些程式碼,自己再做,終於做出來了。做的不是很美觀,但是可以簡單實現。 首先用sqlserver建表。我建了一個名為user_info的表,新增username和passdwo
科大訊飛實現“文字轉語音”和“語音轉文字”
請在這裡檢視示例 ☞ iat示例 詳細介紹 這裡整合了科大訊飛官方示例,去除一些不必要的元素,便於開發者理解和應用相關功能 經測試,在chrome瀏覽器下不允許在http協議下使用html5的ap
鬼吹燈文字挖掘5:sklearn實現文字聚類和文字分類
1. 準備資料import numpy as np import pandas as pd import re import jieba # 章節判斷用變數預處理 def is_chap_head(tmpstr): import re pattern = r
C實現Unix時間戳和本地時間轉化
我們平常說時間都說的幾點幾分幾秒,星期幾,但是在計算機裡面並不是直接使用我們所說的時間,而是使用Unix時間戳,這樣不管是哪個平臺,哪個系統,都可以根據自己對時間的定義進行轉換,像Java,PHP等都提供了介面來進行轉化,C庫裡面也有這樣的函式,那具體是怎麼實現
c++實現簡單選擇排序和堆排序
接下來我們對簡單選擇排序和堆排序做詳細額介紹,排序過程中使用的資料仍和前兩節排序過程中使用的資料相同:int a[10] = {45,12,36,76,45,9,33,19,87,23}; 1、簡單選
用c++實現單向連結串列和雙向連結串列
連結串列是一種非常基礎的資料結構,本身也比較靈活,突破了陣列在一開始就要確定長度的限制,能夠做到隨時使用隨時分配記憶體。同時還有新增,刪除,查詢等功能。 總的來說,連結串列是由幾個模組構成的。 一,單向連結串列 //連結串列基本元素 struct Nod
C# 實現Json 序列化和反序列化功能
1、新建一個 JSON 類,實現如下所示程式碼: /* * * 表示層的輔助類 * * 功能:JSON序列化和反序列化 * 作者:凌霜殘血 * */ public class JSON {
c++實現陣列的插入和刪除
#include <iostream> using namespace std; void Print (int * arr,int len) { for(int i=0;i&l
linux 下C++實現 ARP釋出,和ARP監聽
改造自http://blog.csdn.net/xiaodao1986/article/details/6628250 g++ -o即可編譯通過。 ubuntu 14.04 可以用適當的方法,在寢室裡,讓室友不能上網。 #include <stdio.h>
C實現頭插法和尾插法來構建單鏈表(帶頭結點)
核心程式碼如下://建立帶頭結點的單鏈表(尾插法) void CreateListTailInsert(Node *pNode){ /** * 就算一開始輸入的數字小於等於0,帶頭
[Trie樹] 統計英文文字中單詞出現的個數 - C語言實現 - 考慮數字、英文
【英文文字】 However, after reaching the shore there are plenty of challenges waiting for him."The biggest challenge now is learning to walk agai
c實現功能(7)寫入和讀取文字檔案
#include <stdio.h> #include <string.h> int main() { //向一個檔案中寫入內容 char s[1024] = {0}; //開啟一個檔案 FILE *p = fopen(
輸入一串字元,並判斷其中英文字元和數字字元的個數——C語言實現(C語言練習)
先看程式執行的結果,(文末有該程式的完整程式碼) 1、先寫好框架 #include<stdio.h> void main() { } 2、再定義所需要的變數 char str;//定義一個字元型變數 str int i=0,counte