中文分词算法 之 基于词典的正向最大匹配算法

基于词典的正向最大匹配算法最长词优先匹配,算法会根据词典文件自动调整最大长度,分词的好坏彻底取决于词典。java

 

算法流程图以下:node

 

Java实现代码以下:git

 

/**
 * 基于词典的正向最大匹配算法
 * @author 杨尚川
 */
public class WordSeg {
    private static final List<String> DIC = new ArrayList<>();
    private static final int MAX_LENGTH;
    static{
        try {
            System.out.println("开始初始化词典");
            int max=1;
            int count=0;
            List<String> lines = Files.readAllLines(Paths.get("D:/dic.txt"), Charset.forName("utf-8"));
            for(String line : lines){
                DIC.add(line);
                count++;
                if(line.length()>max){
                    max=line.length();
                }
            }
            MAX_LENGTH = max;
            System.out.println("完成初始化词典,词数目:"+count);
            System.out.println("最大分词长度:"+MAX_LENGTH);
        } catch (IOException ex) {
            System.err.println("词典装载失败:"+ex.getMessage());
        }
        
    }
    public static void main(String[] args){
        String text = "杨尚川是APDPlat应用级产品开发平台的做者";  
        System.out.println(seg(text));
    }
    public static List<String> seg(String text){        
        List<String> result = new ArrayList<>();
        while(text.length()>0){
            int len=MAX_LENGTH;
            if(text.length()<len){
                len=text.length();
            }
            //取指定的最大长度的文本去词典里面匹配
            String tryWord = text.substring(0, 0+len);
            while(!DIC.contains(tryWord)){
                //若是长度为一且在词典中未找到匹配,则按长度为一切分
                if(tryWord.length()==1){
                    break;
                }
                //若是匹配不到,则长度减一继续匹配
                tryWord=tryWord.substring(0, tryWord.length()-1);
            }
            result.add(tryWord);
            //从待分词文本中去除已经分词的文本
            text=text.substring(tryWord.length());
        }
        return result;
    }
}

 

词典文件下载地址dic.rar,简单吧,呵呵github

 

实现功能是简单,不过这里的词典中词的数目为:427452,咱们须要频繁执行DIC.contains(tryWord))来判断一个词是否在词典中,因此优化这行代码可以显著提高分词效率(不要过早优化、不要作不成熟的优化)。算法

 

上面的代码是利用了JDK的Collection接口的contains方法来判断一个词是否在词典中,而这个方法的不一样实现,其性能差别极大,上面的初始版本是用了ArrayList:List<String> DIC = new ArrayList<>()。那么这个ArrayList的性能如何呢?还有更好性能的实现吗?数组

 

一般来讲,对于查找算法,在有序列表中查找比在无序列表中查找更快,分区查找全局遍历要快。安全

 

经过查看ArrayList、LinkedList、HashSet的contains方法的源代码,发现ArrayList和LinkedList采用全局遍历的方式且未利用有序列表的优点,HashSet使用了分区查找,若是hash分布均匀冲突少,则须要遍历的列表就不多甚至不须要。理论归理论,仍是写个代码来测测更直观放心,测试代码以下:数据结构

 

/**
 * 比较词典查询算法的性能
 * @author 杨尚川
 */
public class SearchTest {
    //为了生成随机查询的词列表
    private static final List<String> DIC_FOR_TEST = new ArrayList<>();
    //经过更改这里DIC的实现来比较不一样实现之间的性能
    private static final List<String> DIC = new ArrayList<>();
    static{
        try {
            System.out.println("开始初始化词典");
            int count=0;
            List<String> lines = Files.readAllLines(Paths.get("D:/dic.txt"), Charset.forName("utf-8"));
            for(String line : lines){
                DIC.add(line);
                DIC_FOR_TEST.add(line);
                count++;
            }
            System.out.println("完成初始化词典,词数目:"+count);
        } catch (IOException ex) {
            System.err.println("词典装载失败:"+ex.getMessage());
        }        
    }
    public static void main(String[] args){
        //选取随机值
        List<String> words = new ArrayList<>();
        for(int i=0;i<100000;i++){
            words.add(DIC_FOR_TEST.get(new Random(System.nanoTime()+i).nextInt(427452)));
        }
        long start = System.currentTimeMillis();
        for(String word : words){
            DIC.contains(word);
        }
        long cost = System.currentTimeMillis()-start;
        System.out.println("cost time:"+cost+" ms");
    }
}

 

#分别运行10次测试,而后取平均值
LinkedList     10000次查询       cost time:48812 ms
ArrayList      10000次查询       cost time:40219 ms
HashSet        10000次查询       cost time:8 ms
HashSet        1000000次查询     cost time:258 ms
HashSet        100000000次查询   cost time:28575 ms

 

咱们发现HashSet性能最好,比LinkedList和ArrayList快约3个数量级!这个测试结果跟前面的分析一致,LinkedList要比ArrayList慢一些,虽然他们都是全局遍历,可是LinkedList须要操做下一个数据的引用,因此会多一些操做,LinkedList由于须要保存前驱后继引用,占用的内存也要高一些。dom

 

虽然HashSet已经有不错的性能了,可是若是词典愈来愈大,内存占用愈来愈多怎么办?若是有一个数据结构,有接近HashSet性能的同时,又能对词典的数据进行压缩以减小内存占用,那就完美了。oop

 

前缀树(Trie)有可能能够实现“鱼与熊掌兼得”的好事,本身实现一个Trie的数据结构,代码以下:

 

/**
 * 前缀树的Java实现
 * 用于查找一个指定的字符串是否在词典中
 * @author 杨尚川
 */
public class Trie {
    private final TrieNode ROOT_NODE = new TrieNode('/');

    public boolean contains(String item){
        //去掉首尾空白字符
        item=item.trim();
        int len = item.length();
        if(len < 1){
            return false;
        }
        //从根节点开始查找
        TrieNode node = ROOT_NODE;
        for(int i=0;i<len;i++){
            char character = item.charAt(i);
            TrieNode child = node.getChild(character);
            if(child == null){
                //未找到匹配节点
                return false;
            }else{
                //找到节点,继续往下找
                node = child;
            }
        }
        if(node.isTerminal()){
            return true;
        }
        return false;
    }
    public void addAll(List<String> items){
        for(String item : items){
            add(item);
        }
    }
    public void add(String item){
        //去掉首尾空白字符
        item=item.trim();
        int len = item.length();
        if(len < 1){
            //长度小于1则忽略
            return;
        }
        //从根节点开始添加
        TrieNode node = ROOT_NODE;
        for(int i=0;i<len;i++){
            char character = item.charAt(i);
            TrieNode child = node.getChildIfNotExistThenCreate(character);
            //改变顶级节点
            node = child;
        }
        //设置终结字符,表示从根节点遍历到此是一个合法的词
        node.setTerminal(true);
    }
    private static class TrieNode{
        private char character;
        private boolean terminal;
        private final Map<Character,TrieNode> children = new ConcurrentHashMap<>();        
        public TrieNode(char character){
            this.character = character;
        }
        public boolean isTerminal() {
            return terminal;
        }
        public void setTerminal(boolean terminal) {
            this.terminal = terminal;
        }        
        public char getCharacter() {
            return character;
        }
        public void setCharacter(char character) {
            this.character = character;
        }
        public Collection<TrieNode> getChildren() {
            return this.children.values();
        }
        public TrieNode getChild(char character) {
            return this.children.get(character);
        }        
        public TrieNode getChildIfNotExistThenCreate(char character) {
            TrieNode child = getChild(character);
            if(child == null){
                child = new TrieNode(character);
                addChild(child);
            }
            return child;
        }
        public void addChild(TrieNode child) {
            this.children.put(child.getCharacter(), child);
        }
        public void removeChild(TrieNode child) {
            this.children.remove(child.getCharacter());
        }        
    }
    
    public void show(){
        show(ROOT_NODE,"");
    }
    private void show(TrieNode node, String indent){
        if(node.isTerminal()){
            System.out.println(indent+node.getCharacter()+"(T)");
        }else{
            System.out.println(indent+node.getCharacter());
        }
        for(TrieNode item : node.getChildren()){
            show(item,indent+"\t");
        }
    }
    public static void main(String[] args){
        Trie trie = new Trie();
        trie.add("APDPlat");
        trie.add("APP");
        trie.add("APD");
        trie.add("Nutch");
        trie.add("Lucene");
        trie.add("Hadoop");
        trie.add("Solr");
        trie.add("杨尚川");
        trie.add("杨尚昆");
        trie.add("杨尚喜");
        trie.add("中华人民共和国");
        trie.add("中华人民打太极");
        trie.add("中华");
        trie.add("中心思想");
        trie.add("杨家将");        
        trie.show();
    }
}

 

 

修改前面的测试代码,把List<String> DIC = new ArrayList<>()改成Trie DIC = new Trie(),使用Trie来作词典查找,最终的测试结果以下:

 

#分别运行10次测试,而后取平均值
LinkedList     10000次查询       cost time:48812 ms
ArrayList      10000次查询       cost time:40219 ms
HashSet        10000次查询       cost time:8 ms
HashSet        1000000次查询     cost time:258 ms
HashSet        100000000次查询   cost time:28575 ms
Trie           10000次查询       cost time:15 ms
Trie           1000000次查询     cost time:1024 ms
Trie           100000000次查询   cost time:104635 ms

 

能够发现Trie和HashSet的性能差别较小,在半个数量级之内,经过jvisualvm惊奇地发现Trie占用的内存比HashSet的大约2.6倍,以下图所示:

 

HashSet:

 

Trie:


 

词典中词的数目为427452,HashSet是基于HashMap实现的,因此咱们看到占内存最多的是HashMap$Node、char[]和String,手动执行GC屡次,这三种类型的实例数一直在变化,固然都始终大于词数427452。Trie是基于ConcurrentHashMap实现的,因此咱们看到占内存最多的是ConcurrentHashMap、ConcurrentHashMap$Node[]、ConcurrentHashMap$Node、Trie$TrieNode和Character,手动执行GC屡次,发现Trie$TrieNode的实例数一直保持不变,说明427452个词通过Trie处理后的节点数为603141。

 

很明显地能够看到,这里Trie的实现不够好,选用ConcurrentHashMap占用的内存至关大,那么咱们如何来改进呢?把ConcurrentHashMap替换为HashMap能够吗?HashSet不是也基于HashMap吗?看看把ConcurrentHashMap替换为HashMap后的效果,以下图所示:



 

内存占用虽然少了10M左右,但仍然是HashSet的约2.4倍,原本是打算使用Trie来节省内存,没想反正更加占用内存了,既然使用HashMap来实现Trie占用内存极高,那么试试使用数组的方式,以下代码所示:

 

/**
 * 前缀树的Java实现
 * 用于查找一个指定的字符串是否在词典中
 * @author 杨尚川
 */
public class TrieV2 {
    private final TrieNode ROOT_NODE = new TrieNode('/');

    public boolean contains(String item){
        //去掉首尾空白字符
        item=item.trim();
        int len = item.length();
        if(len < 1){
            return false;
        }
        //从根节点开始查找
        TrieNode node = ROOT_NODE;
        for(int i=0;i<len;i++){
            char character = item.charAt(i);
            TrieNode child = node.getChild(character);
            if(child == null){
                //未找到匹配节点
                return false;
            }else{
                //找到节点,继续往下找
                node = child;
            }
        }
        if(node.isTerminal()){
            return true;
        }
        return false;
    }
    public void addAll(List<String> items){
        for(String item : items){
            add(item);
        }
    }
    public void add(String item){
        //去掉首尾空白字符
        item=item.trim();
        int len = item.length();
        if(len < 1){
            //长度小于1则忽略
            return;
        }
        //从根节点开始添加
        TrieNode node = ROOT_NODE;
        for(int i=0;i<len;i++){
            char character = item.charAt(i);
            TrieNode child = node.getChildIfNotExistThenCreate(character);
            //改变顶级节点
            node = child;
        }
        //设置终结字符,表示从根节点遍历到此是一个合法的词
        node.setTerminal(true);
    }
    private static class TrieNode{
        private char character;
        private boolean terminal;
        private TrieNode[] children = new TrieNode[0];
        public TrieNode(char character){
            this.character = character;
        }
        public boolean isTerminal() {
            return terminal;
        }
        public void setTerminal(boolean terminal) {
            this.terminal = terminal;
        }        
        public char getCharacter() {
            return character;
        }
        public void setCharacter(char character) {
            this.character = character;
        }
        public Collection<TrieNode> getChildren() {
            return Arrays.asList(children);            
        }
        public TrieNode getChild(char character) {
            for(TrieNode child : children){
                if(child.getCharacter() == character){
                    return child;
                }
            }
            return null;
        }        
        public TrieNode getChildIfNotExistThenCreate(char character) {
            TrieNode child = getChild(character);
            if(child == null){
                child = new TrieNode(character);
                addChild(child);
            }
            return child;
        }
        public void addChild(TrieNode child) {
            children = Arrays.copyOf(children, children.length+1);
            this.children[children.length-1]=child;
        }
    }
    
    public void show(){
        show(ROOT_NODE,"");
    }
    private void show(TrieNode node, String indent){
        if(node.isTerminal()){
            System.out.println(indent+node.getCharacter()+"(T)");
        }else{
            System.out.println(indent+node.getCharacter());
        }        
        for(TrieNode item : node.getChildren()){
            show(item,indent+"\t");
        }
    }
    public static void main(String[] args){
        TrieV2 trie = new TrieV2();
        trie.add("APDPlat");
        trie.add("APP");
        trie.add("APD");
        trie.add("杨尚川");
        trie.add("杨尚昆");
        trie.add("杨尚喜");
        trie.add("中华人民共和国");
        trie.add("中华人民打太极");
        trie.add("中华");
        trie.add("中心思想");
        trie.add("杨家将");        
        trie.show();
    }
}

 

 

内存占用状况以下图所示:

 

 

如今内存占用只有HashSet方式的80%了,内存问题总算是解决了,进一步分析,若是词典够大,词典中有共同前缀的词足够多,节省的内存空间必定很是客观。那么性能呢?看以下从新测试的数据:

#分别运行10次测试,而后取平均值
LinkedList     10000次查询       cost time:48812 ms
ArrayList      10000次查询       cost time:40219 ms
HashSet        10000次查询       cost time:8 ms
HashSet        1000000次查询     cost time:258 ms
HashSet        100000000次查询   cost time:28575 ms
Trie           10000次查询       cost time:15 ms
Trie           1000000次查询     cost time:1024 ms
Trie           100000000次查询   cost time:104635 
TrieV1         10000次查询       cost time:16 ms
TrieV1         1000000次查询     cost time:780 ms
TrieV1         100000000次查询   cost time:90949 ms
TrieV2         10000次查询       cost time:50 ms
TrieV2         1000000次查询     cost time:4361 ms
TrieV2         100000000次查询   cost time:483398

 

 

总结一下,ArrayList和LinkedList方式实在太慢,跟最快的HashSet比将近慢约3个数量级,果断抛弃。Trie比HashSet慢约半个数量级,内存占用多约2.6倍,改进的TrieV1比Trie稍微节省一点内存约10%,速度差很少。进一步改进的TrieV2比Trie大大节省内存,只有HashSet的80%,不过速度比HashSet慢约1.5个数量级。

 

TrieV2实现了节省内存的目标,节省了约70%,可是速度也慢了,慢了约10倍,能够对TrieV2作进一步优化,TrieNode的数组children采用有序数组,采用二分查找来加速。

 

下面看看TrieV3的实现:



 

使用了一个新的方法insert来加入数组元素,从无到有构建有序数组,把新的元素插入到已有的有序数组中,insert的代码以下:

 

        /**
         * 将一个字符追加到有序数组
         * @param array 有序数组
         * @param element 字符
         * @return 新的有序数字
         */
        private TrieNode[] insert(TrieNode[] array, TrieNode element){
            int length = array.length;
            if(length == 0){
                array = new TrieNode[1];
                array[0] = element;
                return array;
            }
            TrieNode[] newArray = new TrieNode[length+1];
            boolean insert=false;
            for(int i=0; i<length; i++){
                if(element.getCharacter() <= array[i].getCharacter()){
                    //新元素找到合适的插入位置
                    newArray[i]=element;
                    //将array中剩下的元素依次加入newArray便可退出比较操做
                    System.arraycopy(array, i, newArray, i+1, length-i);
                    insert=true;
                    break;
                }else{
                    newArray[i]=array[i];
                }
            }
            if(!insert){
                //将新元素追加到尾部
                newArray[length]=element;
            }
            return newArray;
        }

 

 

有了有序数组,在搜索的时候就能够利用有序数组的优点,重构搜索方法getChild:



  

数组中的元素是TrieNode,因此须要自定义TrieNode的比较方法:



 

好了,一个基于有序数组的二分搜索的性能提高重构就完成了,良好的单元测试是重构的安全防御网,没有单元测试的重构就犹如高空走钢索却没有防御垫同样危险,同时,不过早优化不作不成熟的优化是咱们应该谨记的原则,要根据应用的具体场景在算法的时空中作权衡。

 

OK,看看TrieV3的性能表现,固然了,内存使用没有变化,和TrieV2同样:

 

TrieV2         10000次查询       cost time:50 ms
TrieV2         1000000次查询     cost time:4361 ms
TrieV2         100000000次查询   cost time:483398 ms
TrieV3         10000次查询       cost time:21 ms
TrieV3         1000000次查询     cost time:1264 ms
TrieV3         100000000次查询   cost time:121740 ms

 

 

提高效果很明显,约4倍。性能还有提高的空间吗?呵呵......

 

代码托管于GITHUB

 

 

参考资料:

一、中文分词十年回顾 

二、中文信息处理中的分词问题

三、汉语自动分词词典机制的实验研究

四、由字构词_中文分词新方法

五、汉语自动分词研究评述

 

NUTCH/HADOOP视频教程

相关文章
相关标签/搜索