ik分词器下载地址:https://code.google.com/archive/p/ik-analyzer/downloads
需要翻墙
配置文件:
IKAnalyzer2012.jar(主 jar 包)
IKAnalyzer.cfg.xml(分词器扩展配置文件)
stopword.dic(停止词典)
ext.dic (扩展词典)
import java.io.StringReader; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; import org.wltea.analyzer.lucene.IKAnalyzer; /** * Ik分词器 * * @author jiang * */ public class Ik { public static void main(String[] args) { try { // 检索内容 String text = "中国人"; // 当为 true 时,分词器采用智能切分 ;当为 false时,分词器进行最细粒度切分。 IKAnalyzer anal = new IKAnalyzer(false); StringReader reader = new StringReader(text); // 分词 TokenStream ts = anal.tokenStream("s", reader); CharTermAttribute term = ts.getAttribute(CharTermAttribute.class); // 遍历分词数据 while (ts.incrementToken()) { System.out.print(term.toString() + "|"); } reader.close(); } catch (Exception e) { e.printStackTrace(); } } }
控制台:
加载扩展词典:ext.dic
加载扩展停止词典:stopword.dic
中国人|中国|国人|
lucene4.0 与iku_1兼容,其它不兼容情况可以更换jar包