分詞常見算法-----小整理

 1 單字分詞

[java] view plaincopy
  1. public static void  tokennizer(String a){  
  2.             for(int i=0;i<a.length();i++){  
  3.              System.out.prinltn(a.charAt[i]);   
  4.        }  
  5.    }  

    二元分詞

[java] view plaincopy
  1.      
  2. public static void split(String a) {  
  3. int len = a.length();  
  4. String key;  
  5. for (int i = 0; i < len; i++) {  
  6. if ((i + 2) <= len) {  
  7. key = a.substring(i, i + 2);  
  8. System.out.println(i + " ---- " + key);  
  9. }  
  10. }  
  11.   
  12. }  


    3   最大正向匹配

       (相當於有倆個指針,j,i,  當start=0, end=n,  則 substring(0,n),如此,先變end,再變start,可以確保取得正向的最長的字符串)

[java] view plaincopy
  1.          
  2. public static void match(String s, int n) {  
  3. int start = 0;  
  4. int end = n;  
  5. while (start < end) {  
  6. for (; end > start; end--) {  
  7. String key = s.substring(start, end);// 切出最大字符串  
  8. if (lt.contains(key)) {// 判斷當前字符串是否在詞典中  
  9. // j = j + i;  
  10. System.out.println(key);  
  11. break;  
  12. }  
  13. }  
  14. ++start;  
  15. }  
  16. }  

    4   最大逆向匹配

      逆向最長匹配法是基於字符串匹配的一種分詞算法,即按從右至左的順序對句子循環掃描字符串,並與所提供的關鍵詞表進行比較,如存在則提取出該串作爲關鍵詞。相比較正向最大匹配法,逆向匹配的分詞精度略高於正向匹配。

   (相當於有倆個指針,start,end,  當start=0,end=n,  則 substring(0,n),如此,先變start,在變end,可以確保取得逆向最長的字符串)

[java] view plaincopy
  1. public static void matchrev(String s, int n) {  
  2. int end = n;  
  3. int start = 0;  
  4. while (end >= start) {  
  5. for (; start < end; start++) {  
  6. String key = s.substring(start, end);  
  7. if (lt.contains(key)) {  
  8. System.out.println(key);  
  9. break;  
  10. }  
  11. }  
  12. --end;  
  13. }  
  14. }  

    文章是自己的一些整理,以便自己日後複習,若能幫到您,不勝榮幸,有誤之處,請您諒解

 參考 :http://blog.csdn.net/fuyangchang/article/details/1822684

        http://blog.csdn.net/yaoxy/article/details/4288241

轉載地址:http://blog.csdn.net/wenbingoon/article/details/8974360

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章