github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/core/analysis/tokenattributes/position.go (about)

     1  package tokenattributes
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/balzaczyy/golucene/core/util"
     6  )
     7  
     8  /*
     9  Determines the position of this token relative to the previous Token
    10  in a TokenStream, used in phrase searching.
    11  
    12  The default value is one.
    13  
    14  Some common uses for this are:
    15  
    16  	- Set it to zero to put multiple terms in the same position. This
    17  	is useful if, e.g., a word has multiple stems. Searches for phrases
    18  	including either stem will match. In this case, all but the first
    19  	stem's increment should be set to zero: the increment of the first
    20  	instance should be one. Repeating a token with an increment of zero
    21  	can also be used to boost the scores of matches on that token.
    22  
    23  	-	Set it to values greater than one to inhibit exact phrase matches.
    24  	If, for example, one does not want phrases to match across removed
    25  	stop words, then one could build a stop word filter that removes
    26  	stop words and also sets the incremeent to the number of stop words
    27  	removed before each non-stop word. Then axact phrase queries will
    28  	only match when the terms occur with no intervening stop words.
    29  */
    30  type PositionIncrementAttribute interface {
    31  	util.Attribute
    32  	// Set the position increment. The deafult value is one.
    33  	SetPositionIncrement(int)
    34  	// Returns the position increment of this token.
    35  	PositionIncrement() int
    36  }
    37  
    38  /* Default implementation of ositionIncrementAttribute */
    39  type PositionIncrementAttributeImpl struct {
    40  	positionIncrement int
    41  }
    42  
    43  func newPositionIncrementAttributeImpl() util.AttributeImpl {
    44  	return &PositionIncrementAttributeImpl{
    45  		positionIncrement: 1,
    46  	}
    47  }
    48  
    49  func (a *PositionIncrementAttributeImpl) Interfaces() []string {
    50  	return []string{"PositionIncrementAttribute"}
    51  }
    52  
    53  func (a *PositionIncrementAttributeImpl) SetPositionIncrement(positionIncrement int) {
    54  	assert2(positionIncrement >= 0, "Increment must be zero or greater: got %v", positionIncrement)
    55  	a.positionIncrement = positionIncrement
    56  }
    57  
    58  func assert2(ok bool, msg string, args ...interface{}) {
    59  	if !ok {
    60  		panic(fmt.Sprintf(msg, args...))
    61  	}
    62  }
    63  
    64  func (a *PositionIncrementAttributeImpl) PositionIncrement() int {
    65  	return a.positionIncrement
    66  }
    67  
    68  func (a *PositionIncrementAttributeImpl) Clear() {
    69  	a.positionIncrement = 1
    70  }
    71  
    72  func (a *PositionIncrementAttributeImpl) Clone() util.AttributeImpl {
    73  	return &PositionIncrementAttributeImpl{
    74  		positionIncrement: a.positionIncrement,
    75  	}
    76  }
    77  
    78  func (a *PositionIncrementAttributeImpl) CopyTo(target util.AttributeImpl) {
    79  	target.(PositionIncrementAttribute).SetPositionIncrement(a.positionIncrement)
    80  }