标签:中文分词
中文分词应用很广泛,网上也有很多开源项目。我在这里主要讲一下中文分词里面算法的简单实现,废话不多说了,现在先上代码
package com; import java.util.ArrayList; import java.util.List; public class Segmentation1 { private List<String> dictionary = new ArrayList<String>(); private String request = "北京大学生前来应聘"; public void setDictionary() { dictionary.add("北京"); dictionary.add("北京大学"); dictionary.add("大学"); dictionary.add("大学生"); dictionary.add("生前"); dictionary.add("前来"); dictionary.add("应聘"); } public String leftMax() { String response = ""; String s = ""; for(int i=0; i<request.length(); i++) { s += request.charAt(i); if(isIn(s, dictionary) && aheadCount(s, dictionary)==1) { response += (s + "/"); s = ""; } else if(aheadCount(s, dictionary) > 0) { } else { response += (s + "/"); s = ""; } } return response; } private boolean isIn(String s, List<String> list) { for(int i=0; i<list.size(); i++) { if(s.equals(list.get(i))) return true; } return false; } private int aheadCount(String s, List<String> list) { int count = 0; for(int i=0; i<list.size(); i++) { if((s.length()<=list.get(i).length()) && (s.equals(list.get(i).substring(0, s.length())))) count ++; } return count; } public static void main(String[] args) { Segmentation1 seg = new Segmentation1(); seg.setDictionary(); String response1 = seg.leftMax(); System.out.println(response1); } }
算法的核心就是从前往后搜索,然后找到最长的字典分词。
标签:中文分词
原文地址:http://blog.csdn.net/xiaoyeyopulei/article/details/25194021