github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/core/analysis/tokenattributes/offset.go (about) 1 package tokenattributes 2 3 import ( 4 "github.com/balzaczyy/golucene/core/util" 5 ) 6 7 /* The start and end character offset of a Token. */ 8 type OffsetAttribute interface { 9 util.Attribute 10 // Returns this Token's starting offset, the position of the first 11 // character corresponding to this token in the source text. 12 StartOffset() int 13 // Returns this Token's starting offset, the position of the first 14 // character corresponding to this token in the source text. 15 // 16 // Note that the difference between endOffset() and startOffset() 17 // may not be equal to the termText.Length(), as the term text may 18 // have been altered by a stemmer or some other filter. 19 // StartOffset() int 20 // Set the starting and ending offset. 21 SetOffset(int, int) 22 // Returns this TOken's ending offset, one greater than the 23 // position of the last character corresponding to this token in 24 // the source text. The length of the token in the source text is 25 // (endOffset() - startOffset()). 26 EndOffset() int 27 } 28 29 /* Default implementation of OffsetAttribute */ 30 type OffsetAttributeImpl struct { 31 startOffset, endOffset int 32 } 33 34 func newOffsetAttributeImpl() util.AttributeImpl { 35 return new(OffsetAttributeImpl) 36 } 37 38 func (a *OffsetAttributeImpl) Interfaces() []string { 39 return []string{"OffsetAttribute"} 40 } 41 42 func (a *OffsetAttributeImpl) StartOffset() int { 43 return a.startOffset 44 } 45 46 func (a *OffsetAttributeImpl) SetOffset(startOffset, endOffset int) { 47 // TODO: we could assert that this is set-once, ie, current value 48 // are -1? Very few token filters should change offsets once set by 49 // the tokenizer... and tokenizer should call clearAtts before 50 // re-using OffsetAtt 51 assert2(startOffset >= 0 && startOffset <= endOffset, 52 "startOffset must be non-negative, and endOffset must be >= startOffset, startOffset=%v,endOffset=%v", 53 startOffset, endOffset) 54 a.startOffset = startOffset 55 a.endOffset = a.endOffset 56 } 57 58 func (a *OffsetAttributeImpl) EndOffset() int { 59 return a.endOffset 60 } 61 62 func (a *OffsetAttributeImpl) Clear() { 63 // TODO: we could use -1 as default here? Then we can assert in SetOffset... 64 a.startOffset = 0 65 a.endOffset = 0 66 } 67 68 func (a *OffsetAttributeImpl) Clone() util.AttributeImpl { 69 return &OffsetAttributeImpl{ 70 startOffset: a.startOffset, 71 endOffset: a.endOffset, 72 } 73 } 74 75 func (a *OffsetAttributeImpl) CopyTo(target util.AttributeImpl) { 76 target.(OffsetAttribute).SetOffset(a.startOffset, a.endOffset) 77 }