IKAnalyzer分词工具的spring boot项目整合代码版

简介


IK Analyzer是什么呢,一个很轻量的中文分词工具,是基于java开发的轻量级的中文分词工具包。它是以开源项目Luence为主体的,结合词典分词和文法分析算法的中文分词组件。IK有很多版本,在2012版本中,IK实现了简单的分词歧义排除算法。

我们为什么选择IK作为我们的分词工具呢,这里我们简单介绍一下。这里我们采用了网上的一些介绍。

1、IK才用了特有的“正向迭代最细粒度切分算法”,支持细粒度和智能分词两种切分模式。

2、在系统环境:Core2 i7 3.4G双核,4G内存,window 7 64位, Sun JDK 1.6_29 64位 普通pc环境测试,IK2012具有160万字/秒(3000KB/S)的高速处理能力。

3、2012版的只能分词模式支持简单的分词排歧义处理和数量词合并输出。

4、用了多子处理器分析模式,支持 英文字母 数字 中文词汇等

5、优化词典存储,更小的内存占用。

IK的引入使用


在pom.xml中加入如下配置即可

<!-- ikanalyzer 中文分词器  -->
        <dependency>
            <groupId>com.janeluo</groupId>
            <artifactId>ikanalyzer</artifactId>
            <version>2012_u6</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.lucene</groupId>
                    <artifactId>lucene-core</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.lucene</groupId>
                    <artifactId>lucene-queryparser</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.lucene</groupId>
                    <artifactId>lucene-analyzers-common</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--  lucene-queryparser 查询分析器模块 -->
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-queryparser</artifactId>
            <version>7.3.0</version>
        </dependency>

IK的两个重要词典


扩展词典:为的是让需要切分的字符串的词语 根据扩展词典里的词,不要切分开来。

例如:扩展词典中有:中国的台湾 。那么原本会切分成:中国 的 台湾 在 东海 。会切分成:中国的台湾 在 东海

停止词典:对比停止词典,直接删掉停止词典中出现的词语


IK的使用


项目:maven工程

resource目录下三个配置文件

1.IKAnalyzer.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <comment>IK Analyzer 扩展配置</comment>
    <entry key="ext_dict">/extend.dic</entry>
    <entry key="ext_stopwords">/stopword.dic</entry>
</properties>

2.extend.dic 扩展词典

这是一个
巨大的墙

3.stopword.dic

一个
一
个
的

4.IKAnalyzerSupport.java

public class IKAnalyzerSupport {

    private static final Logger LOGGER = LoggerFactory.getLogger(IKAnalyzerSupport.class);

    /**
     * IK分词
     * @param target
     * @return
     */
    public static List<String> iKSegmenterToList(String target) throws Exception {
        if (StringUtils.isEmpty(target)){
            return Lists.newArrayList();
        }
        List<String> result = new ArrayList<>();
        StringReader sr = new StringReader(target);
        // 关闭智能分词 (对分词的精度影响较大)
        IKSegmenter ik = new IKSegmenter(sr, false);
        Lexeme lex;
        while((lex=ik.next())!=null) {
            String lexemeText = lex.getLexemeText();
            result.add(lexemeText);
        }

        //LOGGER.info("company:{}, iKSegmenterToList:{}", target, JSON.toJSON(result));
        return result;
    }
}

测试

public static void main(String[] args) throws Exception {
        String str="http://www.relaxheart.cn 是xx同学的个人兴趣分享网站";
        System.out.println(iKSegmenterToList(str));
    }

结果:

http, www.relaxheart.cn, www, relaxheart, cn, 是, 王, 琦, 同学, 的, 个人兴趣, 个人, 兴趣, 分享, 网站

 

 
posted @ 2020-03-21 18:05  liuyanntes'cnblogs  阅读(2932)  评论(0编辑  收藏  举报