IK Analyzer是一个开源的,基于java语言开发的轻量级的中文分词工具包。从2006年12月推出1.0版开始, IKAnalyzer已经推出了4个大版本。最初,它是以开源项目Luence为应用主体的,结合词典分词和文法分析算法的中文分词组件。从3.0版本开始,IK发展为面向Java的公用分词组件,独立于Lucene项目,同时提供了对Lucene的默认优化实现。在2012版本中,IK实现了简单的分词歧义排除算法,标志着IK分词器从单纯的词典分词向模拟语义分词衍化。
IK Analyzer 2012特性:
1.采用了特有的“正向迭代最细粒度切分算法“,支持细粒度和智能分词两种切分模式;
2.在系统环境:Core2 i7 3.4G双核,4G内存,window 7 64位, Sun JDK 1.6_29 64位 普通pc环境测试,IK2012具有160万字/秒(3000KB/S)的高速处理能力。
3.2012版本的智能分词模式支持简单的分词排歧义处理和数量词合并输出。
4.采用了多子处理器分析模式,支持:英文字母、数字、中文词汇等分词处理,兼容韩文、日文字符
5.优化的词典存储,更小的内存占用。支持用户词典扩展定义。特别的,在2012版本,词典支持中文,英文,数字混合词语。
项目网站:
google:https://code.google.com/p/ik-analyzer
下载最新的https://ik-analyzer.googlecode.com/files/IK%20Analyzer%202012FF_hf1.zip,将如下文件放到classpath下:
stopwords.dic 是停用词文件
ext.dic 自定义词典
IKAnalyzer.cfg.xml 分词扩张配置文件(主要用于调取stopwords.dic, ext.dic)
1. IKAnalyzer2012中文分词和去停用词零基础入门
2. Lucene的中文分词器IKAnalyzer
3. IKAnalyzer结合Lucene使用和单独使用例子 简单性能测试
4. Lucene学习——IKAnalyzer中文分词
IK Analyzer 2012特性:
1.采用了特有的“正向迭代最细粒度切分算法“,支持细粒度和智能分词两种切分模式;
2.在系统环境:Core2 i7 3.4G双核,4G内存,window 7 64位, Sun JDK 1.6_29 64位 普通pc环境测试,IK2012具有160万字/秒(3000KB/S)的高速处理能力。
3.2012版本的智能分词模式支持简单的分词排歧义处理和数量词合并输出。
4.采用了多子处理器分析模式,支持:英文字母、数字、中文词汇等分词处理,兼容韩文、日文字符
5.优化的词典存储,更小的内存占用。支持用户词典扩展定义。特别的,在2012版本,词典支持中文,英文,数字混合词语。
项目网站:
google:https://code.google.com/p/ik-analyzer
github:https://github.com/wks/ik-analyzer
https://github.com/linvar/IKAnalyzer
下载最新的https://ik-analyzer.googlecode.com/files/IK%20Analyzer%202012FF_hf1.zip,将如下文件放到classpath下:
stopwords.dic 是停用词文件
ext.dic 自定义词典
IKAnalyzer.cfg.xml 分词扩张配置文件(主要用于调取stopwords.dic, ext.dic)
将IKAnalyzer2012FF_u1.jar添加到工程的lib下:
<properties>
<lucene.version>4.10.4</lucene.version>
</properties>
<dependencies>
<dependency>
<groupId> org.apache.lucene</groupId>
<artifactId> lucene-core</artifactId>
<version> ${lucene.version}</version>
</dependency>
<dependency>
<groupId> org.apache.lucene</groupId>
<artifactId> lucene-analyzers-common</artifactId>
<version> ${lucene.version}</version>
</dependency>
<dependency>
<groupId> org.apache.lucene</groupId>
<artifactId> lucene-queryparser</artifactId>
<version> ${lucene.version}</version>
</dependency>
<dependency>
<groupId> org.apache.lucene</groupId>
<artifactId> lucene-highlighter</artifactId>
<version> ${lucene.version}</version>
</dependency>
<dependency>
<groupId>org.wltea.ik-analyzer</groupId>
<artifactId>IKAnalyzer2012FF_u1</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/IKAnalyzer2012FF_u1.jar</systemPath>
</dependency>
</dependencies>
测试代码:
package cn.slimsmart.lucene.demo.example;
import java.io.IOException;
import java.io.StringReader;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;
import org.wltea.analyzer.lucene.IKAnalyzer;
@SuppressWarnings("resource")
public class IKAnalyzerTest {
public static void main(String[] args) throws Exception {
demo();
}
// 分词测试
public static void demo() throws Exception {
IKAnalyzer analyzer = new IKAnalyzer(true);
System.out.println("当前使用的分词器:" + analyzer.getClass().getSimpleName());
TokenStream tokenStream = analyzer.tokenStream("content", "王五好吃懒做,溜须拍马,跟着李四,也过着小康的日子");
CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
tokenStream.reset();// 必须先调用reset方法
while (tokenStream.incrementToken()) {
System.out.println(new String(charTermAttribute.buffer()));
}
tokenStream.close();
}
// 单独的使用IKAnalyzer,可以直接使用IK分词器的核心类,真正分词的实现类IKSegmenter分词器进行分词
public static void demo1() throws Exception {
StringReader reader = new StringReader("王五好吃懒做,溜须拍马,跟着李四,也过着小康的日子");
IKSegmenter ik = new IKSegmenter(reader, true);// 当为true时,分词器进行最大词长切分
Lexeme lexeme = null;
try {
while ((lexeme = ik.next()) != null)
System.out.println(lexeme.getLexemeText());
} catch (IOException e) {
e.printStackTrace();
} finally {
reader.close();
}
}
}
注:可以根据我们自身的项目业务扩展自己的词典和StopWord
参考文章:1. IKAnalyzer2012中文分词和去停用词零基础入门
2. Lucene的中文分词器IKAnalyzer
3. IKAnalyzer结合Lucene使用和单独使用例子 简单性能测试
4. Lucene学习——IKAnalyzer中文分词