歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 基於Solr 4.0正式版的IKAnalyzer分詞庫修改

基於Solr 4.0正式版的IKAnalyzer分詞庫修改

日期:2017/2/28 15:30:31   编辑:Linux教程

Solr 4.0發布了,不過最新的IK分詞還不支持,急著用,只能是自己修改了。

相關閱讀:Solr 4.0已正式發布了 http://www.linuxidc.com/Linux/2012-10/72372.htm

IKAnalyzer.java

/**
 * IK 中文分詞  版本 5.0
 * IK Analyzer release 5.0
 * 
 * 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.
 *
 * 源代碼由林良益([email protected])提供
 * 版權聲明 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.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.Tokenizer;

/**
 * IK分詞器,Lucene Analyzer接口實現 兼容Lucene 3.1以上版本
 */
public final class IKAnalyzer extends Analyzer {
	private boolean isMaxWordLength = false;

	/**
	 * IK分詞器Lucene Analyzer接口實現類 默認最細粒度切分算法
	 */
	public IKAnalyzer() {
		this(false);
	}

	/**
	 * IK分詞器Lucene Analyzer接口實現類
	 * 
	 * @param isMaxWordLength
	 *            當為true時,分詞器進行最大詞長切分
	 */
	public IKAnalyzer(boolean isMaxWordLength) {
		super();
		this.setMaxWordLength(isMaxWordLength);
	}

	@Override
	public TokenStreamComponents createComponents(String fieldName,
			Reader reader) {
		Tokenizer tokenizer = new IKTokenizer(reader, isMaxWordLength());
		return new TokenStreamComponents(tokenizer, null);
	}

	public void setMaxWordLength(boolean isMaxWordLength) {
		this.isMaxWordLength = isMaxWordLength;
	}

	public boolean isMaxWordLength() {
		return isMaxWordLength;
	}
}

IKTokenizer.java

/**
* IK 中文分詞 版本 5.0
* IK Analyzer release 5.0
*
* 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.
*
* 源代碼由林良益([email protected])提供
* 版權聲明 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.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

/**
* IK分詞器 Lucene Tokenizer適配器類 兼容Lucene 3.1以上版本
*/
public final class IKTokenizer extends Tokenizer {

// IK分詞器實現
private IKSegmenter _IKImplement;
// 詞元文本屬性
private CharTermAttribute termAtt;
// 詞元位移屬性
private OffsetAttribute offsetAtt;
// 記錄最後一個詞元的結束位置
private int finalOffset;

/**
* Lucene 3.5 Tokenizer適配器類構造函數
*
* @param in
* @param useSmart
*/
public IKTokenizer(Reader in, boolean useSmart) {
super(in);
offsetAtt = addAttribute(OffsetAttribute.class);
termAtt = addAttribute(CharTermAttribute.class);
_IKImplement = new IKSegmenter(in, useSmart);
}

/*
* (non-Javadoc)
*
* <A class=referer href="http://www.linuxidc.com" target=_blank>@see</A> 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());
// 記錄分詞的最後位置
finalOffset = nextLexeme.getEndPosition();
// 返會true告知還有下個詞元
return true;
}
// 返會false告知詞元輸出完畢
return false;
}

/*
* (non-Javadoc)
*
* <A class=referer href="http://www.linuxidc.com" target=_blank>@see</A> org.apache.lucene.analysis.Tokenizer#reset(java.io.Reader)
*/
public void reset(Reader input) throws IOException {
super.reset();
_IKImplement.reset(input);
}

@Override
public final void end() {
// set final offset
offsetAtt.setOffset(finalOffset, finalOffset);
}
}

IKTokenizerFactory.java

/**
* IK 中文分詞 版本 5.0
* IK Analyzer release 5.0
*
* 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.
*
* 源代碼由林良益([email protected])提供
* 版權聲明 2012,烏龍茶工作室
* provided by Linliangyi and copyright 2012 by Oolong studio
*
*
*/
package org.wltea.analyzer.solr;

import java.io.Reader;
import java.util.Map;

import org.apache.lucene.analysis.Tokenizer;
import org.apache.lucene.analysis.util.TokenizerFactory;
import org.wltea.analyzer.lucene.IKTokenizer;

/**
* IK中文分詞
* Solr分詞器工廠實現
*
* 2012-3-6
*
*/
public class IKTokenizerFactory extends TokenizerFactory {


private boolean useSmart = false;

@Override
public void init(Map<String, String> params) {
super.init(params);
String useSmartParam = params.get("useSmart");
this.useSmart = (useSmartParam != null ? Boolean.parseBoolean(useSmartParam) : false);
}

/* (non-Javadoc)
* <A class=referer href="http://www.linuxidc.com" target=_blank>@see</A> org.apache.solr.analysis.TokenizerFactory#create(java.io.Reader)
*/
public Tokenizer create(Reader in) {
return new IKTokenizer(in , this.useSmart);
}

}

Copyright © Linux教程網 All Rights Reserved