分词常见算法-----小整理

来源:互联网 发布:ibatis执行sql语句 编辑:程序博客网 时间:2024/06/06 09:04

 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

0 0
原创粉丝点击