c++實現文本中英文單詞和漢字字元的統計

來源:互聯網
上載者:User

原始碼下載: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]


參考:實驗室小熊

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.