public class IKSynonymsAnalyzer5x extends Analyzer {
@Override
protected TokenStreamComponents createComponents(String fieldName) {
IKTokenizer5x tokenizer5x = new IKTokenizer5x(true);
Map paramsMap=new HashMap();
paramsMap.put("luceneMatchVersion", "LUCENE_11");
paramsMap.put("synonyms", "luceneIndexCreate/synonyms.txt");
paramsMap.put("expand", "true");
SynonymFilterFactory factory=new SynonymFilterFactory(paramsMap);
ClasspathResourceLoader loader = new ClasspathResourceLoader();
try {
factory.inform(loader);
} catch (IOException e) {
e.printStackTrace();
}
return new TokenStreamComponents(tokenizer5x, factory.create(tokenizer5x));
}
}
public class IKTokenizer5x extends Tokenizer {
private IKSegmenter _IKImplement;
private final CharTermAttribute termAtt = (CharTermAttribute)this.addAttribute(CharTermAttribute.class);
private final OffsetAttribute offsetAtt = (OffsetAttribute)this.addAttribute(OffsetAttribute.class);
private final TypeAttribute typeAtt = (TypeAttribute)this.addAttribute(TypeAttribute.class);
private int endPosition;
public IKTokenizer5x() {
this._IKImplement = new IKSegmenter(this.input, true);
}
public IKTokenizer5x(boolean useSmart) {
this._IKImplement = new IKSegmenter(this.input, useSmart);
}
public IKTokenizer5x(AttributeFactory factory) {
super(factory);
this._IKImplement = new IKSegmenter(this.input, true);
}
public boolean incrementToken() throws IOException {
this.clearAttributes();
Lexeme nextLexeme = this._IKImplement.next();
if(nextLexeme != null) {
this.termAtt.append(nextLexeme.getLexemeText());
this.termAtt.setLength(nextLexeme.getLength());
this.offsetAtt.setOffset(nextLexeme.getBeginPosition(), nextLexeme.getEndPosition());
this.endPosition = nextLexeme.getEndPosition();
this.typeAtt.setType(nextLexeme.getLexemeTypeString());
return true;
} else {
return false;
}
}
public void reset() throws IOException {
super.reset();
this._IKImplement.reset(this.input);
}
public final void end() {
int finalOffset = this.correctOffset(this.endPosition);
this.offsetAtt.setOffset(finalOffset, finalOffset);
}
}
luceneIndexCreate/synonyms.txt
女鞋,女靴
靴子,长靴,短靴