ik分词器:效果展示
实现步骤
1.引入maven依赖
<!--ik分词-->
<dependency>
<groupId>com.janeluo</groupId>
<artifactId>ikanalyzer</artifactId>
<version>2012_u6</version>
</dependency>
2.IK分词工具类
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
/**
* @PackageName: com.hrt.zxxc.sxy
* @ClassName: IKAnalyzerUtil
* @Description: TODO ik分词器
* @Author:
* @Date: 2019/11/11 8:50
* @Versoin: 1.0
**/
public class IKAnalyzerUtil {
public static List<String> cut(String msg) throws IOException {
StringReader sr=new StringReader(msg);
IKSegmenter ik=new IKSegmenter(sr, true);
Lexeme lex=null;
List<String> list=new ArrayList<>();
while((lex=ik.next())!=null){
list.add(lex.getLexemeText());
}
return list;
}
}
3.调用
public List<String> getTitleList(String title) throws IOException {
return IKAnalyzerUtil.cut(title.trim());
}
4.一些配置文件
① 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">local.dic;</entry>
<!--用户可以在这里配置自己的扩展停止词字典 -->
<entry key="ext_stopwords">stop.dic;</entry>
</properties>
②local.dic
③stop.dic