您的位置 首页 java

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

项目结构:

2.导入必需的jar包之后配置IKAnalyzer.cfg.xml,将扩展字典和扩展停用词、主词典载入;

3.main2016.dic

4.测试方法:

分词方法:

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.结果

文章来源:智云一二三科技

文章标题:Lucene的中文分词器IKAnalyzer

文章地址:https://www.zhihuclub.com/183257.shtml

关于作者: 智云科技

热门文章

网站地图