之前的示例中,使用的是默认的StandardAnalyzer分词器,不能有效的进行中文分词,下面演示下如何在Lucene5.0中使用IKAnalyzer分词器。
首先下载IKAnalyzer分词器源码,IKAnalyzer分词器源码托管在OSChina的git上。下载地址:
http://git.oschina.net/wltea/IK-Analyzer-2012FF
请如图下载IK的源代码:
然后打开Eclipse新建一个Java Project:
然后解压下载下来的IKAnalyzer源码压缩包,你将得到如图这样的目录结构:
打开src目录,ctrl + A,然后ctrl + C,粘帖到新建的project的src源码包下,如图:
然后新建一个lib目录存放IK依赖的Lucene Jar包,如图:
然后全选lib下的jar包鼠标右键Build Path -->Add to Build Path,如图:
导好Jar包后,由于Lucene5.0 API上有些变化,我们需要对IK源码做些修改,具体修改如下:
第一处需要修改的就是IKTokenizer类,在其构造函数里把//super(in);这句注释掉即可,下面这是我修改过后的源码:
/**
* IK 中文分词 版本 5.0.1
* IK Analyzer release 5.0.1
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* 源代码由林良益(linliangyi2005@gmail.com)提供
* 版权声明 2012,乌龙茶工作室
* provided by Linliangyi and copyright 2012 by Oolong studio
*
*
*/
package org.wltea.analyzer.lucene;
import java.io.IOException;
import java.io.Reader;
import org.apache.lucene.analysis.Tokenizer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;
/**
* IK分词器 Lucene Tokenizer适配器类
* 兼容Lucene 4.0版本
*/
public final class IKTokenizer extends Tokenizer {
//IK分词器实现
private IKSegmenter _IKImplement;
//词元文本属性
private final CharTermAttribute termAtt;
//词元位移属性
private final OffsetAttribute offsetAtt;
//词元分类属性(该属性分类参考org.wltea.analyzer.core.Lexeme中的分类常量)
private final TypeAttribute typeAtt;
//记录最后一个词元的结束位置
private int endPosition;
private Version version = Version.LATEST;
/**
* Lucene 4.0 Tokenizer适配器类构造函数
* @param in
* @param useSmart
*/
public IKTokenizer(Reader in , boolean useSmart){
//super(in);
offsetAtt = addAttribute(OffsetAttribute.class);
termAtt = addAttribute(CharTermAttribute.class);
typeAtt = addAttribute(TypeAttribute.class);
_IKImplement = new IKSegmenter(input , useSmart);
}
/* (non-Javadoc)
* @see org.apache.lucene.analysis.TokenStream#incrementToken()
*/
@Override
public boolean incrementToken() throws IOException {
//清除所有的词元属性
clearAttributes();
Lexeme nextLexeme = _IKImplement.next();
if(nextLexeme != null){
//将Lexeme转成Attributes
//设置词元文本
termAtt.append(nextLexeme.getLexemeText());
//设置词元长度
termAtt.setLength(nextLexeme.getLength());
//设置词元位移
offsetAtt.setOffset(nextLexeme.getBeginPosition(), nextLexeme.getEndPosition());
//记录分词的最后位置
endPosition = nextLexeme.getEndPosition();
//记录词元分类
typeAtt.setType(nextLexeme.getLexemeTypeString());
//返会true告知还有下个词元
return true;
}
//返会false告知词元输出完毕
return false;
}
/*
* (non-Javadoc)
* @see org.apache.lucene.analysis.Tokenizer#reset(java.io.Reader)
*/
@Override
public void reset() throws IOException {
super.reset();
_IKImplement.reset(input);
}
@Override
public final void end() {
// set final offset
int finalOffset = correctOffset(this.endPosition);
offsetAtt.setOffset(finalOffset, finalOffset);
}
}
还有一个比较要的类就是IKAnalyzer,其中的createComponents方法是继承Luecene的Analyzer接口的,由于Lucene5.0里把createComponents方法的第二个参数去掉了,所以需要对该方法做如下修改:
/**
* 重载Analyzer接口,构造分词组件
*/
@Override
protected TokenStreamComponents createComponents(String text) {
Reader reader = new BufferedReader(new StringReader(text));
Tokenizer _IKTokenizer = new IKTokenizer(reader , this.useSmart());
return new TokenStreamComponents(_IKTokenizer);
}
当然还有其他地方需要修改,但其他的地方我觉得很容易就知道怎么改,我就不过多说明了。这里我把我修改好了的IKAnalyzer分词器源码以及打包好的jar包下载地址分享给你们,如果你们不想自己再弄一遍,就访问这个地址去下载吧:
IKAnalyzer5.0下载地址:
http://pan.baidu.com/s/1o6wr5zk
IKAnalyzer Jar包弄好了,我们还需要把Jar包安装到我们本地仓库中,因为我们的demo是Maven Project,至于如何把Jar包安装到本地仓库中,请参看我这篇博文《Maven如何安装Jar包到本地仓库》。jar安装到本地仓库后,我们就可以在pom.xml中配置IK依赖了,从而导入IKAnalyzer jar包。
<dependency> <groupId>org.wltea.analyzer</groupId> <artifactId>IKAnalyzer</artifactId> <version>5.0</version> </dependency>
接下来我们创建分词器的时候,直接new IKAnalyzer()即可。注意创建索引时候使用的分词器对象类型必须要和查询时使用的分词器对象类型保持一致。
至于IKAnalyzer如何添加自定义词典和自定义停用词词典,这个很简单,你只需要从下载下来的源码包里找到默认的两个dic词典文件,复制一份重命名一下,然后复制到项目的src目录下添加词典内容,然后把IKAnalyzer.cfg.xml配置文件也copy到项目的src目录下,打开IKAnalyzer.cfg.xml配置文件,在里面配置上你新添加的词典文件,多个词典文件是用分号分割的,如ext.dic;aaaaa.dic;bbbbb.dic;
XML里面有注释说明,你懂的。IKAnalyzer分词器的使用介绍就到这里。
如果你还有什么问题请加我Q-Q:7-3-6-0-3-1-3-0-5,
或者加裙
一起交流学习。