Lucene高版本(5.5.4) IKAnalyzer2012_u6版本Jar包修改方式

Lucene高版本(5.5.4) IKAnalyzer2012_u6版本Jar包修改方式_第1张图片

Maven配置

清除IKAnalyzer2012_u6所依赖的Lucene4.7.2相关jar包

引入IKAnalyzer2012_u6所依赖的Lucene5.5.4相关jar包

pom.xml


  4.0.0
  cn.et
  IkAnalyzerLucene
  0.0.1-SNAPSHOT
  
 	  
        com.janeluo  
        ikanalyzer  
        2012_u6  
        
        
        	
        		org.apache.lucene  
        		lucene-core  
        	
        	
        		org.apache.lucene  
        		lucene-queries  
        	
        	
        		org.apache.lucene  
        		lucene-sandbox  
        	
        	
        		org.apache.lucene  
        		lucene-analyzers-common  
        	
        	
        		org.apache.lucene  
        		lucene-queryparser  
        	
        
    
    
    	org.apache.lucene  
    	lucene-core  
    	5.5.4
    
    
    	org.apache.lucene  
    	lucene-queries 
    	5.5.4 
    
    
    	org.apache.lucene  
    	lucene-sandbox
    	5.5.4  
    
    
    	org.apache.lucene  
    	lucene-analyzers-common  
   		5.5.4
   	
    
    	org.apache.lucene  
    	lucene-queryparser  
   		5.5.4
   	
  
  
    
	  
	    org.apache.maven.plugins
	  	maven-compiler-plugin
	  	
	  	  1.7
	  	  1.7
	  	  UTF-8
	  	
	  
    
  
重新写IKAnalyzer的jar包下org.wltea.analyzer.lucene下的IKANalyzer.java和IKTokenizer.java

package org.wltea.analyzer.lucene;

import java.io.Reader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.Tokenizer;

public final class IKAnalyzer extends Analyzer {

	private boolean useSmart;

	public boolean useSmart() {
		return useSmart;
	}

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

	public IKAnalyzer() {
		this(false);
	}

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

	/*
	//Lucene4.7.2
	@Override
	protected TokenStreamComponents createComponents(String fieldName, final Reader in) {
		Tokenizer _IKTokenizer = new IKTokenizer(in, this.useSmart());
		return new TokenStreamComponents(_IKTokenizer);
	}
	*/
	/**
	 *支持 Lucene5.5.4
	 * @author lucheng
	 */
	@Override
	protected TokenStreamComponents createComponents(String fieldName) {
		Tokenizer _IKTokenizer = new IKTokenizer(this.useSmart());
		return new TokenStreamComponents(_IKTokenizer);
	}

}
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.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

public final class IKTokenizer extends Tokenizer {
	private IKSegmenter _IKImplement;
	private final CharTermAttribute termAtt;
	private final OffsetAttribute offsetAtt;
	private final TypeAttribute typeAtt;
	private int endPosition;
	
	/*
	//支持Lucene4.7.2
	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);
	}
	*/
	/**
	 * 支持Lucene5.5.4
	 * @author lucheng
	 */
	public IKTokenizer(boolean useSmart) {
		super();
		offsetAtt = addAttribute(OffsetAttribute.class);
		termAtt = addAttribute(CharTermAttribute.class);
		typeAtt = addAttribute(TypeAttribute.class);
		_IKImplement = new IKSegmenter(input, useSmart);
	}
	
	@Override
	public boolean incrementToken() throws IOException {
		clearAttributes();
		Lexeme nextLexeme = _IKImplement.next();
		if (nextLexeme != null) {
			termAtt.append(nextLexeme.getLexemeText());

			termAtt.setLength(nextLexeme.getLength());

			offsetAtt.setOffset(nextLexeme.getBeginPosition(), nextLexeme.getEndPosition());

			endPosition = nextLexeme.getEndPosition();

			typeAtt.setType(nextLexeme.getLexemeTypeString());

			return true;
		}
		return false;
	}

	@Override
	public void reset() throws IOException {
		super.reset();
		_IKImplement.reset(input);
	}
	@Override
	public final void end() {
		int finalOffset = correctOffset(this.endPosition);
		offsetAtt.setOffset(finalOffset, finalOffset);
	}
}

从Maven本地中仓库中将com.janeluo.ikanalyzer.2012_u6.ikanalyzer-2012_u6.jar复制出来用WinRAR打开,找到org.wltea.analyzer.lucene下的IKANalyzer.class和IKTokenizer.class

Lucene高版本(5.5.4) IKAnalyzer2012_u6版本Jar包修改方式_第2张图片

删除原有的IKANalyzer.class和IKTokenizer.class。

找到我们创建的org.wltea.analyzer.lucene包下修改后的IKANalyzer.java和IKTokenizer.java所编译后生成的class文件,将这两个class文件粘粘进去。


你可能感兴趣的:(Lucene高版本(5.5.4) IKAnalyzer2012_u6版本Jar包修改方式)