QQ登录

只需要一步,快速开始

APP扫码登录

只需要一步,快速开始

查看: 2449|回复: 0

[JAVA/JSP] 使用IKanalyzer分词器分词并且统计词频

[复制链接]

等级头衔

积分成就    金币 : 2861
   泡泡 : 1516
   精华 : 6
   在线时间 : 1322 小时
   最后登录 : 2025-10-13

丰功伟绩

优秀达人突出贡献荣誉管理论坛元老活跃会员

联系方式
发表于 2021-2-6 19:51:30 | 显示全部楼层 |阅读模式
       IKAnalyzer是一个开源的,基于java语言开发的轻量级的中文分词工具包。从2006年12月推出1.0版开始,IKAnalyzer已经推出了3个大版本。最初,它是以开源项目Luence为应用主体的,结合词典分词和文法分析算法的中文分词组件。新版本的 IKAnalyzer3.0则发展为面向Java的公用分词组件,独立于Lucene项目,同时提供了对Lucene的默认优化实现。$ U5 b3 r% M6 q; w& H3 a
IKAnalyzer3.0特性:* N( |/ f0 i9 m# m
  • 采用了特有的“正向迭代最细粒度切分算法“,支持细粒度和最大词长两种切分模式;具有83万字/秒(1600KB/S)的高速处理能力。
  • 采用了多子处理器分析模式,支持:英文字母、数字、中文词汇等分词处理,兼容韩文、日文字符
  • 优化的词典存储,更小的内存占用。支持用户词典扩展定义
  • 针对Lucene全文检索优化的查询分析器IKQueryParser(作者吐血推荐);引入简单搜索表达式,采用歧义分析算法优化查询关键字的搜索排列组合,能极大的提高Lucene检索的命中率。
    & F+ U  B7 T3 c  _' g- J
使用方法:
) J' ~! U0 W( |3 d' e! _% r3 P1 d) l6 w       引入 ikanalyzer相关jar包,将以下依赖加入工程的pom.xml中的<dependencies>...</dependencies>部分。; m2 M& k/ p# _# d/ t
<dependency>
    <groupId>org.wltea.ik-analyzer</groupId>
    <artifactId>ik-analyzer</artifactId>
    <version>3.2.8</version>
    </dependency>
/**
 * @Description:
 * @Version: 1.0
 */
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.*;

public class Test {
    /**
     * 对语句进行分词
     * @param text 语句
     * @return 分词后的集合
     * @throws IOException
     */
    private static Map segment(String text) throws IOException {
        Map<String,Integer> map = new HashMap<String,Integer>();
        StringReader re = new StringReader(text);   
        IKSegmenter ik = new IKSegmenter(re, false);//true 使用smart分词,false使用最小颗粒分词
Lexeme lex; while ((lex = ik.next()) != null) { if(lex.getLexemeText().length()>1){ if(map.containsKey(lex.getLexemeText())){ map.put(lex.getLexemeText(),map.get(lex.getLexemeText())+1); }else{ map.put(lex.getLexemeText(),1); } } } return map; } public static void main(String[] args) throws IOException { Map<String,Integer> map = segment("中国,中国,我爱你"); System.out.println(map.toString()); } }
输出结果: 1.jpg & ]# a! j7 u# S2 a, n" g/ m
       整理简单工具类,代码如下:, g; }6 M3 m2 z3 I. o7 \. u
2.jpg # i. M+ g/ |% X5 G
% G; u% W# A( B, e
IKanalyzer分词器源码下载>>
) m/ G3 D  J5 {- J6 J
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|paopaomj.COM ( 渝ICP备18007172号|渝公网安备50010502503914号 )

GMT+8, 2025-10-15 06:59

Powered by paopaomj X3.5 © 2016-2025 sitemap

快速回复 返回顶部 返回列表