整理的jar包、源码、使用手册及api链接:http://pan.baidu.com/s/1eRWyjeE 密码:pyl5
可以看到src目录下有三个配置文件,分别为扩展字典文件ext.dic,停止词字典文件stopwprd.dic和配置文件IKAnalyzer.cfg.xml。配置文件IKAnalyzer.cfg.xml为配置扩展字典文件和停止词字典文件路径。IKAnalyzer.cfg.xml文件默认放置在classpath的根目录下,可以修改源码来改变该文件位置。
在程序中使用IK很简单,只需要创建IKAnalyzer对象即可,因为IKAnalyzer继承于Lucene的Analyzer。
IK无参构造函数默认采用细粒度切分算法,
Analyzer analyzer = new IKAnalyzer();//细粒度切分算法
当然也可以传入参数设置采用智能切分算法。
Analyzer analyzer = new IKAnalyzer(true);//智能切分
手册中有详细的配置及使用说明
IK Analyzer的简单实现
package com.jmsht.util.test;
import java.io.StringReader;
import org.apache.lucene.analysis.Analyzer;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;
import org.wltea.analyzer.lucene.IKAnalyzer;
public class IKAnalyzerTest {
public static void main(String[] args){
String text = "搜索小米华为手机电视电脑裤子袜子外设化妆品香水海外代购热门个人护理面膜热销精品锅巴小黄人,永和服装饰品有限公司,和服韩版修身汉服潮流,昨天村支书赵永来电说家里的母猪生了4头小猪 ";
Analyzer analyzer = new IKAnalyzer();//细粒度切分算法
Analyzer analyzer1 = new IKAnalyzer(true);//智能切分
//独立Lucene实现
StringReader re = new StringReader(text);
IKSegmenter ik = new IKSegmenter(re,true);
Lexeme lex = null;
try {
while((lex=ik.next())!=null){
System.out.print(lex.getLexemeText()+"-");
}
}catch(Exception e){
e.printStackTrace();
}
}
}