Guest User

Untitled

a guest
May 19th, 2014
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. package org.apache.lucene.analysis;
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Iterator;
  6. import java.util.LinkedList;
  7. import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
  8. import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
  9. import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
  10. import org.apache.lucene.analysis.util.CharacterUtils;
  11. import org.apache.lucene.util.Version;
  12.  
  13. public final class ExtendedNameFilter extends TokenFilter
  14. {
  15. private final CharTermAttribute termAtt = (CharTermAttribute)addAttribute(CharTermAttribute.class);
  16. private PositionIncrementAttribute posIncAttr;
  17. private OffsetAttribute setOffsetAttr;
  18. private final int extendedWordCount;
  19. LinkedList<String> list = new LinkedList();
  20. ArrayList<Integer> startOffsetList = new ArrayList();
  21. int endOffset = 0;
  22. int count = 0;
  23.  
  24. public ExtendedNameFilter(Version matchVersion, TokenStream in, int extendedWordCount)
  25. {
  26. super(in);
  27. CharacterUtils.getInstance(matchVersion);
  28. this.extendedWordCount = extendedWordCount;
  29. this.posIncAttr = ((PositionIncrementAttribute)addAttribute(PositionIncrementAttribute.class));
  30. this.setOffsetAttr = ((OffsetAttribute)addAttribute(OffsetAttribute.class));
  31. }
  32.  
  33. public final boolean incrementToken()
  34. throws IOException
  35. {
  36. int len = 0;
  37.  
  38. while (this.input.incrementToken()) {
  39. this.list.add(this.termAtt.toString());
  40. this.startOffsetList.add(Integer.valueOf(this.setOffsetAttr.startOffset()));
  41. this.endOffset = this.setOffsetAttr.endOffset();
  42. }
  43.  
  44. Iterator iterator = this.list.iterator();
  45. len = this.list.size();
  46.  
  47. if ((len > 0) && (this.extendedWordCount < 0)) {
  48. this.termAtt.setEmpty();
  49. while (iterator.hasNext()) {
  50. this.termAtt.append((CharSequence)iterator.next());
  51. }
  52. this.list.removeFirst();
  53.  
  54. this.posIncAttr.setPositionIncrement(10);
  55. this.setOffsetAttr.setOffset(((Integer)this.startOffsetList.get(this.count)).intValue(), this.endOffset);
  56. this.count += 1;
  57. return true;
  58. }
  59. if ((len > 0) && (this.count < this.extendedWordCount)) {
  60. this.termAtt.setEmpty();
  61. while (iterator.hasNext()) {
  62. this.termAtt.append((CharSequence)iterator.next());
  63. }
  64. this.list.removeFirst();
  65.  
  66. this.posIncAttr.setPositionIncrement(10);
  67. this.setOffsetAttr.setOffset(((Integer)this.startOffsetList.get(this.count)).intValue(), this.endOffset);
  68. this.count += 1;
  69. return true;
  70. }
  71.  
  72. return false;
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment