Java詞頻統計算法(使用單詞樹)

  許多英語培訓機構(如新東方)都會出幾本“高頻詞彙”的書,主要內容是統計近幾年來各類外語考試中屢次出現的高頻詞彙,幫助考生減少需要背的生詞的數量。但這些高頻是如何被統計出來的呢?顯然不會用手工去計算。
  假如我們已經將一篇文章存在一字符串(String)對象中,爲了統計詞彙出現頻率,最簡單直接的做法是另外建一個Map:key是單詞,value是次數。將文章從頭讀到尾,讀到一個單詞就到Map裏查一下,如果查到了則次數加一,沒查到則往Map裏一扔。這樣做雖然代碼寫起來簡單,但性能卻非常差。首先查詢Map的代價是O(logn),假設文章的字母數爲m,則整個統計程序的時間複雜度爲O(mlogn)不說,如果要拿高頻詞可能還需要對統計結果進行排序。即便對結構上進行優化性能仍然不高。如果能夠將時間複雜度從O(mlogn)減少到O(m)的話不是更好?
  爲了改進算法我們首先引進單詞樹。與單詞前綴樹不同,單詞樹的結構相當簡單,結構如圖所示:

 

  從圖中我們可以看出,樹中每個結點保存屬性值cnt與指向其26個子結點的指針(每一條路徑代表一個英文字母),其中cnt爲到達該結點經過路徑所對應的英文單詞在文章中出現的次數。也就是說,我們開始讀文章時讓一個指針指向單詞數的根結點,之後每度一個字母就讓該指針指向當前結點對應路徑上的子結點(若子結點爲空則新建一個),一個單詞讀完後讓當前結點的cnt值加一,並讓指針重新指向根結點。而當一篇文章讀完之後我們的單詞樹也就已經建立完畢了。之後只要去遍歷它並把取到的單詞根據次數進行排序就行了(時間複雜度爲O(nlogn))。

  程序代碼如下,首先是存放單詞及出現次數的JavaBean

  1. public class WordCount {
  2.     private String word;
  3.     
  4.     private int count;
  5.     public int getCount() {
  6.         return count;
  7.     }
  8.     public void setCount(int count) {
  9.         this.count = count;
  10.     }
  11.     public String getWord() {
  12.         return word;
  13.     }
  14.     public void setWord(String word) {
  15.         this.word = word;
  16.     }
  17. }

  其次是實現詞頻表生成算法的類:

  1. public class WordCountService {
  2.     
  3.     /**
  4.      * 根據文章生成單詞樹
  5.      * @param text
  6.      * @return
  7.      */
  8.     private static CharTreeNode geneCharTree(String text){
  9.         CharTreeNode root = new CharTreeNode();
  10.         CharTreeNode p = root;
  11.         char c = ' ';
  12.         for(int i = 0; i < text.length(); ++i){
  13.             c = text.charAt(i);
  14.             if(c >= 'A' && c <= 'Z')
  15.                 c = (char)(c + 'a' - 'A');
  16.             if(c >= 'a' && c <= 'z'){
  17.                 if(p.children[c-'a'] == null)
  18.                     p.children[c-'a'] = new CharTreeNode();
  19.                 p = p.children[c-'a'];
  20.             }
  21.             else{
  22.                 p.cnt ++;
  23.                 p = root;
  24.             }
  25.         }
  26.         if(c >= 'a' && c <= 'z')
  27.             p.cnt ++;
  28.         return root;
  29.     }
  30.     
  31.     /**
  32.      * 使用深度優先搜索遍歷單詞樹並將對應單詞放入結果集中
  33.      * @param result
  34.      * @param p
  35.      * @param buffer
  36.      * @param length
  37.      */
  38.     private static void getWordCountFromCharTree(List result,CharTreeNode p, char[] buffer, int length){
  39.         for(int i = 0; i < 26; ++i){
  40.             if(p.children[i] != null){
  41.                 buffer[length] = (char)(i + 'a');
  42.                 if(p.children[i].cnt > 0){
  43.                     WordCount wc = new WordCount();
  44.                     wc.setCount(p.children[i].cnt);
  45.                     wc.setWord(String.valueOf(buffer, 0, length+1));
  46.                     result.add(wc);
  47.                 }
  48.                 getWordCountFromCharTree(result,p.children[i],buffer,length+1);
  49.             }
  50.         }
  51.     }
  52.     
  53.     private static void getWordCountFromCharTree(List result,CharTreeNode p){
  54.         getWordCountFromCharTree(result,p,new char[100],0);
  55.     }
  56.     
  57.     /**
  58.      * 得到詞頻表的主算法,供外部調用
  59.      * @param article
  60.      * @return
  61.      */
  62.     public static List getWordCount(String article){
  63.         CharTreeNode root = geneCharTree(article);
  64.         List result = new ArrayList();//此處也可用LinkedList鏈表,以避免數組滿了擴容導致的性能損失
  65.         getWordCountFromCharTree(result,root);
  66.         Collections.sort(result, new Comparator(){
  67.             public int compare(Object o1, Object o2) {
  68.                 WordCount wc1 = (WordCount)o1;
  69.                 WordCount wc2 = (WordCount)o2;
  70.                 return wc2.getCount() - wc1.getCount();
  71.             }
  72.         });
  73.         return result;
  74.     }
  75. }
  76. /**
  77.  * 單詞樹結點的定義
  78.  * @author FlameLiu
  79.  *
  80.  */
  81. class CharTreeNode{
  82.     int cnt = 0;
  83.     CharTreeNode[] children = new CharTreeNode[26];
  84. }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章