Source code download: http://download.csdn.net/detail/nuptboyzhb/4987141
1. Count the frequency of Chinese characters in the text to lay the foundation for subsequent text classification. For Chinese Character statistics, you need to determine whether the read is a Chinese character. The source code is as follows:
[C ++ Code]
/** @ Author: Zheng Haibo http://blog.csdn.net/NUPTboyZHB * Reference: Laboratory bear * Note: There are strikethrough */# 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) // GB 2312 {infile> temp [0]; If (infile. EOF () break; If (temp [0]> = 0xb0) // Chinese Characters Under gb2312, minimum 0xb0 {S + = temp [0]; infile> temp [1]; S + = temp [1];} else // non-Chinese characters are not counted {S = ""; continue ;} wordcount [s] ++; S = "" ;}cout <"Word type:" <wordcount. size () <Endl; // a small top heap is used in the priority queue, with a small number at the top. ">"; 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; // The number of backend columns is small. The number of backend columns is larger than that of 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/nuptboyzhb1_n"; while (true) {cout <"view the top K Chinese characters with the highest frequency, K = "; cin> K; If (k <= 0) break; topk (k);} return 0 ;}
[Figure 1]
2. Count the occurrence frequency of English words. This is easier than counting Chinese characters. Because words are separated by spaces, you can directly store words in strings.
[C ++ Code]
/** @ Author: Zheng Haibo http://blog.csdn.net/NUPTboyZHB * Reference: Laboratory bear * Note: There are strikethrough */# 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 <"Word type:" <wordcount. size () <Endl; // The priority queue uses a small top heap with a small number at the top. Use ">"; 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/nuptboyzhb1_n"; while (true) {cout <"put in K:"; CIN> K; If (k <= 0) break; topk (k);} return 0 ;}
[Figure 2]
Reference: lab bear