C++ 對一段英文進行詞頻統計

  1. /** 
  2.  * 對一段英文的詞頻統計 
  3.  * @Author [email protected] 
  4.  * date 2010/06/17 
  5.  */  
  6. #include <iostream>  
  7. #include <string>  
  8.   
  9. using namespace std;  
  10. /** 
  11.  * 單詞對象 
  12.  */  
  13. struct Word  
  14. {  
  15.     Word() : Str(""), Count(0)  
  16.     {}  
  17.     string Str;  
  18.     int Count;  
  19.   
  20.     /** 
  21.      * 交換單詞(用於排序) 
  22.      * @param word 交換的單詞對象 
  23.      */  
  24.     void exchange(Word &word)  
  25.     {  
  26.         string tStr = word.Str;  
  27.         int tCount = word.Count;  
  28.         word.Str = Str;  
  29.         word.Count = Count;  
  30.         Str = tStr;  
  31.         Count = tCount;  
  32.     }  
  33. };  
  34.   
  35. /** 
  36.  * 統計詞頻 
  37.  * @param words 單詞數組 
  38.  * @param newWord 單詞內容 
  39.  * @param size 單詞總數 
  40.  */  
  41. void CalcCount(Word * words, string &newWord, int size)  
  42. {  
  43.     int i = 0;  
  44.     for(; i < size; i++)  
  45.     {  
  46.         if(words[i].Str == newWord)  
  47.         {  
  48.             words[i].Count++;  
  49.             return;  
  50.         }  
  51.         else if(words[i].Str == "")  
  52.             break;  
  53.     }  
  54.     words[i].Str = newWord;  
  55.     words[i].Count = 1;  
  56. }  
  57.   
  58. /** 
  59.  * 以單詞出現頻率降序排列單詞 
  60.  * @param words 單詞數組 
  61.  * @param size 單詞數量 
  62.  */  
  63. void SortWordDown(Word * words, int size)  
  64. {  
  65.     for(int i = 0; i < size; i++)  
  66.     {  
  67.         for(int j = 0; j < size-1; j++)  
  68.         {  
  69.             if(words[j].Count <  words[j+1].Count)  
  70.             {  
  71.                 words[j].exchange(words[j+1]);  
  72.             }  
  73.         }  
  74.     }  
  75. }  
  76.   
  77. int main()  
  78. {  
  79.     Word * words;  
  80.     string content;  
  81.     cout << "輸入一段英文:";  
  82.     getline(cin, content);  
  83.   
  84.     //計算單詞總數  
  85.     int wCount = 1;  
  86.     for(unsigned int i = 0; i < content.length(); i++)  
  87.     {  
  88.         if(content[i] == ' ')  
  89.             wCount++;  
  90.     }  
  91.     words = new Word[wCount];  
  92.   
  93.     string::size_type offset = content.find(' ');//單詞都是以空格隔開  
  94.     while(offset != string::npos)  
  95.     {  
  96.         string wStr = content.substr(0, offset);  
  97.         content.erase(0, offset+1);  
  98.         CalcCount(words, wStr, wCount);  
  99.         offset = content.find(' ');  
  100.     }  
  101.     CalcCount(words, content, wCount);//計算最後一個單詞  
  102.   
  103.     SortWordDown(words, wCount);  
  104.     int printCount = wCount < 5 ? wCount : 5;  
  105.     cout << "出現頻率最高的前" << printCount << "個單詞是:" << endl;  
  106.     for(int i = 0; i < printCount; i++)  
  107.     {  
  108.         cout << words[i].Str << "/t頻率:" << words[i].Count << "次" << endl;  
  109.     }  
  110.   
  111.     delete [] words;  
  112.     return 0;  
  113. }  

 

效果:

發佈了23 篇原創文章 · 獲贊 4 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章