Lucene IK分词器集成,词典扩展

 本文主要介绍在Lucene中集成IKAnalyzer

1 环境介绍

  系统:win10

  lucene版本:7.3.0   https://lucene.apache.org/

  jdk:1.8

 

2 IKAnalyzer 集成说明

      IK分词器最先作为lucence上使用而开发,主要用于对中文的分词,后来发展成独立的分词组件,目前只提供到lucence 4.0版本的支持,我们在使用4.0以后的版本的时候需要简单的集成一下。

      IK需要集成一因为lucence4.0后,Analyer的createComponents方法的参数改变了。

      我们在ikAnalyzer包中提供的Lucence支持类中可以看到

      

      

    

        4.0后的版本中,该方法的参数只有一个fileldName,没有第二个输入流参数。故需要修改后使用

3 maven 依赖引用

<!-- lucene 核心模块  -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>7.3.0</version>
</dependency>
<!-- ikanalyzer 中文分词器  -->
<dependency>
<groupId>com.janeluo</groupId>
<artifactId>ikanalyzer</artifactId>
<version>2012_u6</version>

<exclusions>
<exclusion>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
</exclusion>
</exclusions>
</dependency>

4 重写IKAnalyer 类

   我们重命名为IKAnalyzer4Lucene7,我们把ik包下的ikanalyer类里面的内容全部复制到IKAnalyzer4Lucene7中即可。然后修改createComponents方法。

public class IKAnalyzer4Lucene7 extends Analyzer {

private boolean useSmart = false;

public IKAnalyzer4Lucene7() {
this(false);
}

public IKAnalyzer4Lucene7(boolean useSmart) {
super();
this.useSmart = useSmart;
}

public boolean isUseSmart() {
return useSmart;
}

public void setUseSmart(boolean useSmart) {
this.useSmart = useSmart;
}

@Override
protected TokenStreamComponents createComponents(String fieldName) {
IKTokenizer4Lucene7 tk = new IKTokenizer4Lucene7(this.useSmart);
return new TokenStreamComponents(tk);
}

}

  5 重写IKTokenizer类

    我们重命名为IKTokenizer4Lucene7,我们把ik包下的iktokenizer类里面的内容全部复制到IKTokenizer4Lucene7中即可。然后修改构造方法。

public class IKTokenizer4Lucene7 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;

/**
* @param in
* @param useSmart
*/
public IKTokenizer4Lucene7(boolean useSmart) {
super();
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);
}
}

  6 词典扩展

    6.1 停用词扩展

         1 在类目录下创建IK的配置问题:IKAnalyzer.cfg.xml

         2  在配置文件中增加配置的停用词文件的节点: <entry key="ext_stopwords">my_ext_stopword.dic</entry>

         3 在类目前下创建扩展停用词文件my_ext_stopword.dic

         4, 在停用词文件中添加,一行一个。

    6.2 扩展字典 

          字典中的词,IK会作为一个整体去分词,如‘厉害了我的国’,不会进行拆分

          1 在类目前下创建扩展词文件ext.dic

          2 在文件中添加,一行一个。

          3 在配置文件中增加配置的停用词文件的节点: <entry key="ext_dict">ext.dic</entry> 

    整体如下图:

         

         配置文件IKAnalyer.cfg.xml:

         

        停用词文件:

         

        扩展词文件:

       

  

    7 IK分词器使用效果

public class IkAnalyzerTestDemo {

private static void doToken(TokenStream ts) throws IOException {
ts.reset();
CharTermAttribute cta = ts.getAttribute(CharTermAttribute.class);
while (ts.incrementToken()) {
System.out.print(cta.toString() + "|");
}
System.out.println();
ts.end();
ts.close();
}

public static void main(String[] args) throws IOException {
String etext = "Analysis is one of the main causes of slow indexing. Simply put, ";
// String chineseText = "张三说的确实在理。";
String chineseText = "厉害了我的国一经播出,受到各方好评,强烈激发了国人的爱国之情、自豪感!";
// IKAnalyzer 细粒度切分
try (Analyzer ik = new IKAnalyzer4Lucene7();) {
TokenStream ts = ik.tokenStream("content", etext);
System.out.println("IKAnalyzer中文分词器 细粒度切分,英文分词效果:");
doToken(ts);
ts = ik.tokenStream("content", chineseText);
System.out.println("IKAnalyzer中文分词器 细粒度切分,中文分词效果:");
doToken(ts);
}

// IKAnalyzer 智能切分
try (Analyzer ik = new IKAnalyzer4Lucene7(true);) {
TokenStream ts = ik.tokenStream("content", etext);
System.out.println("IKAnalyzer中文分词器 智能切分,英文分词效果:");
doToken(ts);
ts = ik.tokenStream("content", chineseText);
System.out.println("IKAnalyzer中文分词器 智能切分,中文分词效果:");
doToken(ts);
}
}
}

  运行结果: 

  

      注意:一定要确认加载了扩展文件,否则不会生效。

posted @ 2018-09-05 10:08  黑色如此幽默  阅读(2535)  评论(0编辑  收藏  举报