Lucene的中文分词器IKAnalyzer

项目中需要优化关键字搜索,最近在看Lucene,总结了一下:
IK Analyzer是一个开源的,基于java语言开发的轻量级的中文分词工具包。到现在,IK发展为面向Java的公用分词组件,独立于Lucene项目,同时提供了对Lucene的默认优化实现。在2012版本中,IK实现了简单的分词歧义排除算法,标志着IK分词器从单纯的词典分词向模拟语义分词衍化。
1.需要在项目中引入:
IKAnalyzer.cfg.xml
IKAnalyzer2012FF_u1.jar
lucene-core-4.0.jar
项目结构:
lucene
2.导入必需的jar包之后配置IKAnalyzer.cfg.xml,将扩展字典和扩展停用词、主词典载入;
zidian
3.main2016.dic
dic
4.测试方法:
lucene
分词方法:
public static String IKAnalysis(String str) {
StringBuffer sb = new StringBuffer();
try {
// InputStream in = new FileInputStream(str);//
byte[] bt = str.getBytes();// str
InputStream ip = new ByteArrayInputStream(bt);
Reader read = new InputStreamReader(ip);
IKSegmenter iks = new IKSegmenter(read, true);
Lexeme t;
while ((t = iks.next()) != null) {
sb.append(t.getLexemeText() + “|”);
}
sb.delete(sb.length() - 1, sb.length());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(sb.toString());
return sb.toString();

}

5.结果
结果

原文地址:https://www.cnblogs.com/luweiwei/p/5320797.html