中文分词选取-不成词个数判断法

运用前面几篇文章中的分词算法,可以把中文中的词语分出来,但是不同算法可能得到的分词结果不一样,到底如何确定那种分词效果最好呢。我们在这篇文章中探讨一种判断分词效果好坏的方法。

        在分词的时候,有些单字是不成词的。可以搜索所有分词的可能性,然后对每一种分词结果进行统计,规则如下:每分出一个词就给分词统计结果加一,如果遇到不成词的单字就再给该分词结果加一。得到的结果分别计算出来。选出一个得分最低的就是所要分词的字符串的结果。

        下面就用代码实现一下上面的思想。分别用正向最大匹配和逆向最大匹配,然后在对分词结果进行统计,选出一个最好的结果


[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package com;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. public class Segmentation4 {  
  7.     private List<String> dictionary = new ArrayList<String>();  
  8.     private List<String> notWords = new ArrayList<String>();  
  9.     private static String request = "他说的确实在理";  
  10.       
  11.     public void setDictionary() {  
  12.         dictionary.add("的确");  
  13.         dictionary.add("确实");  
  14.         dictionary.add("实在");  
  15.         dictionary.add("在理");  
  16.     }  
  17.       
  18.     public void setNotWords() {  
  19.         notWords.add("确");  
  20.         notWords.add("实");  
  21.         notWords.add("理");  
  22.     }  
  23.       
  24.     public String leftMax() {  
  25.         String response = "";  
  26.         String s = "";  
  27.         for(int i=0; i<request.length(); i++) {  
  28.             s += request.charAt(i);  
  29.             if(isIn(s, dictionary) && aheadCount(s, dictionary)==1) {  
  30.                 response += (s + "/");  
  31.                 s = "";  
  32.             } else if(aheadCount(s, dictionary) > 0) {  
  33.                   
  34.             } else {  
  35.                 response += (s + "/");  
  36.                 s = "";  
  37.             }  
  38.         }  
  39.         return response;  
  40.     }  
  41.       
  42.     private boolean isIn(String s, List<String> list) {  
  43.         for(int i=0; i<list.size(); i++) {  
  44.             if(s.equals(list.get(i))) return true;  
  45.         }  
  46.         return false;  
  47.     }  
  48.       
  49.     private int aheadCount(String s, List<String> list) {  
  50.         int count = 0;  
  51.         for(int i=0; i<list.size(); i++) {  
  52.             if((s.length()<=list.get(i).length()) && (s.equals(list.get(i).substring(0, s.length())))) count ++;  
  53.         }  
  54.         return count;  
  55.     }  
  56.       
  57.     public String rightMax() {  
  58.         String response = "";  
  59.         String s = "";  
  60.         for(int i=request.length()-1; i>=0; i--) {  
  61.             s = request.charAt(i) + s;  
  62.             if(isIn(s, dictionary) && tailCount(s, dictionary)==1) {  
  63.                 response = (s + "/") + response;  
  64.                 s = "";  
  65.             } else if(tailCount(s, dictionary) > 0) {  
  66.                   
  67.             } else {  
  68.                 response = (s + "/") + response;  
  69.                 s = "";  
  70.             }  
  71.         }  
  72.         return response;  
  73.     }  
  74.       
  75.     private int tailCount(String s, List<String> list) {  
  76.         int count = 0;  
  77.         for(int i=0; i<list.size(); i++) {  
  78.             if((s.length()<=list.get(i).length()) && (s.equals(list.get(i).substring(list.get(i).length()-s.length(), list.get(i).length())))) count ++;  
  79.         }  
  80.         return count;  
  81.     }  
  82.       
  83.     public int getCount(String s) {  
  84.         String[] words = s.split("/");  
  85.         int count = words.length;  
  86.         for(String word : words) {  
  87.             if(isIn(word, notWords)) count++;  
  88.         }  
  89.         return count;  
  90.     }  
  91.       
  92.     public static void main(String[] args) {  
  93.         System.out.println(request);  
  94.         String response;  
  95.         Segmentation4 seg = new Segmentation4();  
  96.         seg.setDictionary();  
  97.         seg.setNotWords();  
  98.         String response1 = seg.leftMax();  
  99.         System.out.println(response1);  
  100.         String response2 = seg.rightMax();  
  101.         System.out.println(response2);  
  102.         if(seg.getCount(response1)<=seg.getCount(response2))  
  103.             response = response1;  
  104.         else response = response2;  
  105.         System.out.println(response);  
  106.     }  
  107. }  

上面程序运行的结果是:

他说的确实在理
他/说/的确/实在/理/
他/说/的/确实/在理/
他/说/的/确实/在理/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值