Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.apache.lucene.analysis;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.LinkedList;
- import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
- import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
- import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
- import org.apache.lucene.analysis.util.CharacterUtils;
- import org.apache.lucene.util.Version;
- public final class ExtendedNameFilter extends TokenFilter
- {
- private final CharTermAttribute termAtt = (CharTermAttribute)addAttribute(CharTermAttribute.class);
- private PositionIncrementAttribute posIncAttr;
- private OffsetAttribute setOffsetAttr;
- private final int extendedWordCount;
- LinkedList<String> list = new LinkedList();
- ArrayList<Integer> startOffsetList = new ArrayList();
- int endOffset = 0;
- int count = 0;
- public ExtendedNameFilter(Version matchVersion, TokenStream in, int extendedWordCount)
- {
- super(in);
- CharacterUtils.getInstance(matchVersion);
- this.extendedWordCount = extendedWordCount;
- this.posIncAttr = ((PositionIncrementAttribute)addAttribute(PositionIncrementAttribute.class));
- this.setOffsetAttr = ((OffsetAttribute)addAttribute(OffsetAttribute.class));
- }
- public final boolean incrementToken()
- throws IOException
- {
- int len = 0;
- while (this.input.incrementToken()) {
- this.list.add(this.termAtt.toString());
- this.startOffsetList.add(Integer.valueOf(this.setOffsetAttr.startOffset()));
- this.endOffset = this.setOffsetAttr.endOffset();
- }
- Iterator iterator = this.list.iterator();
- len = this.list.size();
- if ((len > 0) && (this.extendedWordCount < 0)) {
- this.termAtt.setEmpty();
- while (iterator.hasNext()) {
- this.termAtt.append((CharSequence)iterator.next());
- }
- this.list.removeFirst();
- this.posIncAttr.setPositionIncrement(10);
- this.setOffsetAttr.setOffset(((Integer)this.startOffsetList.get(this.count)).intValue(), this.endOffset);
- this.count += 1;
- return true;
- }
- if ((len > 0) && (this.count < this.extendedWordCount)) {
- this.termAtt.setEmpty();
- while (iterator.hasNext()) {
- this.termAtt.append((CharSequence)iterator.next());
- }
- this.list.removeFirst();
- this.posIncAttr.setPositionIncrement(10);
- this.setOffsetAttr.setOffset(((Integer)this.startOffsetList.get(this.count)).intValue(), this.endOffset);
- this.count += 1;
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment