Lucene++ - a full-featured, c++ search engine
API Documentation
A Token is an occurrence of a term from the text of a field. It consists of a term's text, the start and end offset of the term in the text of the field and a type string. More...
#include <Token.h>
Public Member Functions | |
Token () | |
Constructs a Token will null text. | |
Token (int32_t start, int32_t end) | |
Constructs a Token with null text and start and end offsets. | |
Token (int32_t start, int32_t end, const String &type) | |
Constructs a Token with null text and start and end offsets plus the Token type. | |
Token (int32_t start, int32_t end, int32_t flags) | |
Constructs a Token with null text and start and end offsets plus flags. | |
Token (const String &text, int32_t start, int32_t end) | |
Constructs a Token with the given term text, start and end offsets. The type defaults to "word." NOTE: for better indexing speed you should instead use the char[] termBuffer methods to set the term text. | |
Token (const String &text, int32_t start, int32_t end, const String &type) | |
Constructs a Token with the given term text, start and end offsets and type. NOTE: for better indexing speed you should instead use the char[] termBuffer methods to set the term text. | |
Token (const String &text, int32_t start, int32_t end, int32_t flags) | |
Constructs a Token with the given term text, start and end offsets and flags. NOTE: for better indexing speed you should instead use the char[] termBuffer methods to set the term text. | |
Token (CharArray startTermBuffer, int32_t termBufferOffset, int32_t termBufferLength, int32_t start, int32_t end) | |
Constructs a Token with the given term buffer (offset and length), start and end offsets. | |
virtual | ~Token () |
virtual String | getClassName () |
boost::shared_ptr< Token > | shared_from_this () |
virtual void | setPositionIncrement (int32_t positionIncrement) |
Set the position increment. This determines the position of this token relative to the previous Token in a TokenStream , used in phrase searching. | |
virtual int32_t | getPositionIncrement () |
Returns the position increment of this Token. | |
virtual String | term () |
Returns the Token's term text. | |
virtual void | setTermBuffer (const wchar_t *buffer, int32_t offset, int32_t length) |
Copies the contents of buffer, starting at offset for length characters, into the termBuffer array. | |
virtual void | setTermBuffer (const String &buffer) |
Copies the contents of buffer into the termBuffer array. | |
virtual void | setTermBuffer (const String &buffer, int32_t offset, int32_t length) |
Copies the contents of buffer, starting at offset and continuing for length characters, into the termBuffer array. | |
virtual CharArray | termBuffer () |
Returns the internal termBuffer character array which you can then directly alter. If the array is too small for your token, use resizeTermBuffer(int) to increase it. After altering the buffer be sure to call setTermLength to record the number of valid characters that were placed into the termBuffer. | |
virtual wchar_t * | termBufferArray () |
Optimized implementation of termBuffer. | |
virtual CharArray | resizeTermBuffer (int32_t newSize) |
Grows the termBuffer to at least size newSize, preserving the existing content. Note: If the next operation is to change the contents of the term buffer use setTermBuffer(char[], int, int) , setTermBuffer(String) , or setTermBuffer(String, int, int) to optimally combine the resize with the setting of the termBuffer. | |
virtual int32_t | termLength () |
Return number of valid characters (length of the term) in the termBuffer array. | |
virtual void | setTermLength (int32_t length) |
Set number of valid characters (length of the term) in the termBuffer array. Use this to truncate the termBuffer or to synchronize with external manipulation of the termBuffer. Note: to grow the size of the array, use resizeTermBuffer(int) first. | |
virtual int32_t | startOffset () |
Returns this Token's starting offset, the position of the first character corresponding to this token in the source text. | |
virtual void | setStartOffset (int32_t offset) |
Set the starting offset. | |
virtual int32_t | endOffset () |
Returns this Token's ending offset, one greater than the position of the last character corresponding to this token in the source text. The length of the token in the source text is (endOffset - startOffset). | |
virtual void | setEndOffset (int32_t offset) |
Set the ending offset. | |
virtual void | setOffset (int32_t startOffset, int32_t endOffset) |
Set the starting and ending offset. | |
virtual String | type () |
Returns this Token's lexical type. Defaults to "word". | |
virtual void | setType (const String &type) |
Set the lexical type. | |
virtual int32_t | getFlags () |
Get the bitset for any bits that have been set. This is completely distinct from type() , although they do share similar purposes. The flags can be used to encode information about the token for use by other TokenFilter s. | |
virtual void | setFlags (int32_t flags) |
virtual PayloadPtr | getPayload () |
Returns this Token's payload. | |
virtual void | setPayload (const PayloadPtr &payload) |
Sets this Token's payload. | |
virtual String | toString () |
Returns a string representation of the object. | |
virtual void | clear () |
Resets the term text, payload, flags, and positionIncrement, startOffset, endOffset and token type to default. | |
virtual LuceneObjectPtr | clone (const LuceneObjectPtr &other=LuceneObjectPtr()) |
Shallow clone. Subclasses must override this if they need to clone any members deeply. | |
TokenPtr | clone (CharArray newTermBuffer, int32_t newTermOffset, int32_t newTermLength, int32_t newStartOffset, int32_t newEndOffset) |
Makes a clone, but replaces the term buffer and start/end offset in the process. This is more efficient than doing a full clone (and then calling setTermBuffer) because it saves a wasted copy of the old termBuffer. | |
virtual bool | equals (const LuceneObjectPtr &other) |
All values used for computation of hashCode() should be checked here for equality. | |
virtual int32_t | hashCode () |
Subclasses must implement this method and should compute a hashCode similar to this: | |
TokenPtr | reinit (CharArray newTermBuffer, int32_t newTermOffset, int32_t newTermLength, int32_t newStartOffset, int32_t newEndOffset, const String &newType) |
Shorthand for calling clear , setTermBuffer(char[], int, int) , setStartOffset , setEndOffset , setType . | |
TokenPtr | reinit (CharArray newTermBuffer, int32_t newTermOffset, int32_t newTermLength, int32_t newStartOffset, int32_t newEndOffset) |
Shorthand for calling clear , setTermBuffer(char[], int, int) , setStartOffset , setEndOffset , setType on Token::DEFAULT_TYPE. | |
TokenPtr | reinit (const String &newTerm, int32_t newStartOffset, int32_t newEndOffset, const String &newType) |
Shorthand for calling clear , setTermBuffer(String) , setStartOffset , setEndOffset , setType . | |
TokenPtr | reinit (const String &newTerm, int32_t newTermOffset, int32_t newTermLength, int32_t newStartOffset, int32_t newEndOffset, const String &newType) |
Shorthand for calling clear , setTermBuffer(String) , setStartOffset , setEndOffset , setType . | |
TokenPtr | reinit (const String &newTerm, int32_t newStartOffset, int32_t newEndOffset) |
Shorthand for calling clear , setTermBuffer(String) , setStartOffset , setEndOffset , setType on Token::DEFAULT_TYPE. | |
TokenPtr | reinit (const String &newTerm, int32_t newTermOffset, int32_t newTermLength, int32_t newStartOffset, int32_t newEndOffset) |
Shorthand for calling clear , setTermBuffer(String, int, int) , setStartOffset , setEndOffset , setType on Token::DEFAULT_TYPE. | |
void | reinit (const TokenPtr &prototype) |
Copy the prototype token's fields into this one. Note: Payloads are shared. | |
void | reinit (const TokenPtr &prototype, const String &newTerm) |
Copy the prototype token's fields into this one, with a different term. Note: Payloads are shared. | |
void | reinit (const TokenPtr &prototype, CharArray newTermBuffer, int32_t offset, int32_t length) |
Copy the prototype token's fields into this one, with a different term. Note: Payloads are shared. | |
virtual void | copyTo (const AttributePtr &target) |
Copies the values from this Attribute into the passed-in target attribute. The target implementation must support all the Attributes this implementation supports. | |
![]() | |
virtual | ~Attribute () |
boost::shared_ptr< Attribute > | shared_from_this () |
![]() | |
virtual | ~LuceneObject () |
virtual void | initialize () |
Called directly after instantiation to create objects that depend on this object being fully constructed. | |
virtual int32_t | compareTo (const LuceneObjectPtr &other) |
Compare two objects. | |
![]() | |
virtual | ~LuceneSync () |
virtual SynchronizePtr | getSync () |
Return this object synchronize lock. | |
virtual LuceneSignalPtr | getSignal () |
Return this object signal. | |
virtual void | lock (int32_t timeout=0) |
Lock this object using an optional timeout. | |
virtual void | unlock () |
Unlock this object. | |
virtual bool | holdsLock () |
Returns true if this object is currently locked by current thread. | |
virtual void | wait (int32_t timeout=0) |
Wait for signal using an optional timeout. | |
virtual void | notifyAll () |
Notify all threads waiting for signal. | |
Static Public Member Functions | |
static String | _getClassName () |
static const String & | DEFAULT_TYPE () |
static AttributeFactoryPtr | TOKEN_ATTRIBUTE_FACTORY () |
Convenience factory that returns Token as implementation for the basic attributes. | |
![]() | |
static String | _getClassName () |
Protected Member Functions | |
void | ConstructToken (int32_t start, int32_t end, const String &type, int32_t flags) |
Construct Token and initialize values. | |
void | growTermBuffer (int32_t newSize) |
Allocates a buffer char[] of at least newSize, without preserving the existing content. Its always used in places that set the content. | |
void | initTermBuffer () |
void | clearNoTermBuffer () |
Like clear() but doesn't clear termBuffer/text. | |
![]() | |
LuceneObject () | |
Protected Attributes | |
CharArray | _termBuffer |
int32_t | _termLength |
int32_t | _startOffset |
int32_t | _endOffset |
String | _type |
int32_t | flags |
PayloadPtr | payload |
int32_t | positionIncrement |
![]() | |
SynchronizePtr | objectLock |
LuceneSignalPtr | objectSignal |
Static Protected Attributes | |
static const int32_t | MIN_BUFFER_SIZE |
A Token is an occurrence of a term from the text of a field. It consists of a term's text, the start and end offset of the term in the text of the field and a type string.
The start and end offsets permit applications to re-associate a token with its source text, eg., to display highlighted query terms in a document browser, or to show matching text fragments in a <abbr title="KeyWord In Context">KWIC</abbr> display, etc.
The type is a string, assigned by a lexical analyzer (a.k.a. tokenizer), naming the lexical or syntactic class that the token belongs to. For example an end of sentence marker token might be implemented with type "eos". The default token type is "word".
A Token can optionally have metadata (a.k.a. Payload) in the form of a variable length byte array. Use TermPositions#getPayloadLength()
and TermPositions#getPayload(byte[], int)
to retrieve the payloads from the index.
Tokenizers and TokenFilters should try to re-use a Token instance when possible for best performance, by implementing the TokenStream#incrementToken()
API. Failing that, to create a new Token you should first use one of the constructors that starts with null text. To load the token from a char[] use setTermBuffer(char[], int, int)
. To load from a String use setTermBuffer(String)
or setTermBuffer(String, int, int)
. Alternatively you can get the Token's termBuffer by calling either termBuffer()
, if you know that your text is shorter than the capacity of the termBuffer or resizeTermBuffer(int)
, if there is any possibility that you may need to grow the buffer. Fill in the characters of your term into this buffer, with String#getChars(int, int, char[], int)
if loading from a string, or with System#arraycopy(Object, int, Object, int, int)
, and finally call setTermLength(int)
to set the length of the term text.
Typical Token reuse patterns:
Copying text from a string (type is reset to DEFAULT_TYPE
if not specified):
return reusableToken->reinit(string, startOffset, endOffset[, type]);
Copying some text from a string (type is reset to DEFAULT_TYPE
if not specified):
return reusableToken->reinit(string, 0, string.length(), startOffset, endOffset[, type]);
Copying text from char[] buffer (type is reset to DEFAULT_TYPE
if not specified):
return reusableToken->reinit(buffer, 0, buffer.length, startOffset, endOffset[, type]);
Copying some text from a char[] buffer (type is reset to DEFAULT_TYPE
if not specified):
return reusableToken->reinit(buffer, start, end - start, startOffset, endOffset[, type]);
Copying from one one Token to another (type is reset to DEFAULT_TYPE
if not specified):
return reusableToken.reinit(source.termBuffer(), 0, source.termLength(), source.startOffset(), source.endOffset()[, source.type()]);
A few things to note: clear() initializes all of the fields to default values. This was changed in contrast to Lucene 2.4, but should affect no one. Because TokenStreams can be chained, one cannot assume that the Token's current type is correct. The startOffset and endOffset represent the start and offset in the source text, so be careful in adjusting them. When caching a reusable token, clone it. When injecting a cached token into a stream that can be reset, clone it again.
Lucene::Token::Token | ( | ) |
Constructs a Token will null text.
Lucene::Token::Token | ( | int32_t | start, |
int32_t | end | ||
) |
Constructs a Token with null text and start and end offsets.
start | start offset in the source text |
end | end offset in the source text |
Lucene::Token::Token | ( | int32_t | start, |
int32_t | end, | ||
const String & | type | ||
) |
Lucene::Token::Token | ( | int32_t | start, |
int32_t | end, | ||
int32_t | flags | ||
) |
Constructs a Token with null text and start and end offsets plus flags.
start | start offset in the source text |
end | end offset in the source text |
flags | The bits to set for this token |
Lucene::Token::Token | ( | const String & | text, |
int32_t | start, | ||
int32_t | end | ||
) |
Constructs a Token with the given term text, start and end offsets. The type defaults to "word." NOTE: for better indexing speed you should instead use the char[] termBuffer methods to set the term text.
text | term text |
start | start offset in the source text |
end | end offset in the source text |
Lucene::Token::Token | ( | const String & | text, |
int32_t | start, | ||
int32_t | end, | ||
const String & | type | ||
) |
Constructs a Token with the given term text, start and end offsets and type. NOTE: for better indexing speed you should instead use the char[] termBuffer methods to set the term text.
text | term text |
start | start offset in the source text |
end | end offset in the source text |
type | the lexical type of this Token |
Lucene::Token::Token | ( | const String & | text, |
int32_t | start, | ||
int32_t | end, | ||
int32_t | flags | ||
) |
Constructs a Token with the given term text, start and end offsets and flags. NOTE: for better indexing speed you should instead use the char[] termBuffer methods to set the term text.
text | term text |
start | start offset in the source text |
end | end offset in the source text |
flags | The bits to set for this token |
Lucene::Token::Token | ( | CharArray | startTermBuffer, |
int32_t | termBufferOffset, | ||
int32_t | termBufferLength, | ||
int32_t | start, | ||
int32_t | end | ||
) |
Constructs a Token with the given term buffer (offset and length), start and end offsets.
|
virtual |
|
inlinestatic |
|
virtual |
Resets the term text, payload, flags, and positionIncrement, startOffset, endOffset and token type to default.
Implements Lucene::Attribute.
|
protected |
Like clear() but doesn't clear termBuffer/text.
TokenPtr Lucene::Token::clone | ( | CharArray | newTermBuffer, |
int32_t | newTermOffset, | ||
int32_t | newTermLength, | ||
int32_t | newStartOffset, | ||
int32_t | newEndOffset | ||
) |
Makes a clone, but replaces the term buffer and start/end offset in the process. This is more efficient than doing a full clone (and then calling setTermBuffer) because it saves a wasted copy of the old termBuffer.
|
virtual |
Shallow clone. Subclasses must override this if they need to clone any members deeply.
base | clone reference - null when called initially, then set in top virtual override. |
Implements Lucene::Attribute.
|
protected |
Construct Token and initialize values.
|
virtual |
Copies the values from this Attribute into the passed-in target attribute. The target implementation must support all the Attributes this implementation supports.
Implements Lucene::Attribute.
|
static |
|
virtual |
Returns this Token's ending offset, one greater than the position of the last character corresponding to this token in the source text. The length of the token in the source text is (endOffset - startOffset).
|
virtual |
All values used for computation of hashCode()
should be checked here for equality.
see also LuceneObject#equals(Object)
Implements Lucene::Attribute.
|
inlinevirtual |
Reimplemented from Lucene::Attribute.
|
virtual |
Get the bitset for any bits that have been set. This is completely distinct from type()
, although they do share similar purposes. The flags can be used to encode information about the token for use by other TokenFilter
s.
|
virtual |
Returns this Token's payload.
|
virtual |
Returns the position increment of this Token.
|
protected |
Allocates a buffer char[] of at least newSize, without preserving the existing content. Its always used in places that set the content.
newSize | minimum size of the buffer |
|
virtual |
Subclasses must implement this method and should compute a hashCode similar to this:
int32_t hashCode() { int32_t code = startOffset; code = code * 31 + endOffset; return code; }
see also equals(Object)
Implements Lucene::Attribute.
|
protected |
TokenPtr Lucene::Token::reinit | ( | CharArray | newTermBuffer, |
int32_t | newTermOffset, | ||
int32_t | newTermLength, | ||
int32_t | newStartOffset, | ||
int32_t | newEndOffset | ||
) |
Shorthand for calling clear
, setTermBuffer(char[], int, int)
, setStartOffset
, setEndOffset
, setType
on Token::DEFAULT_TYPE.
TokenPtr Lucene::Token::reinit | ( | CharArray | newTermBuffer, |
int32_t | newTermOffset, | ||
int32_t | newTermLength, | ||
int32_t | newStartOffset, | ||
int32_t | newEndOffset, | ||
const String & | newType | ||
) |
Shorthand for calling clear
, setTermBuffer(char[], int, int)
, setStartOffset
, setEndOffset
, setType
.
TokenPtr Lucene::Token::reinit | ( | const String & | newTerm, |
int32_t | newStartOffset, | ||
int32_t | newEndOffset | ||
) |
Shorthand for calling clear
, setTermBuffer(String)
, setStartOffset
, setEndOffset
, setType
on Token::DEFAULT_TYPE.
TokenPtr Lucene::Token::reinit | ( | const String & | newTerm, |
int32_t | newStartOffset, | ||
int32_t | newEndOffset, | ||
const String & | newType | ||
) |
Shorthand for calling clear
, setTermBuffer(String)
, setStartOffset
, setEndOffset
, setType
.
TokenPtr Lucene::Token::reinit | ( | const String & | newTerm, |
int32_t | newTermOffset, | ||
int32_t | newTermLength, | ||
int32_t | newStartOffset, | ||
int32_t | newEndOffset | ||
) |
Shorthand for calling clear
, setTermBuffer(String, int, int)
, setStartOffset
, setEndOffset
, setType
on Token::DEFAULT_TYPE.
TokenPtr Lucene::Token::reinit | ( | const String & | newTerm, |
int32_t | newTermOffset, | ||
int32_t | newTermLength, | ||
int32_t | newStartOffset, | ||
int32_t | newEndOffset, | ||
const String & | newType | ||
) |
Shorthand for calling clear
, setTermBuffer(String)
, setStartOffset
, setEndOffset
, setType
.
void Lucene::Token::reinit | ( | const TokenPtr & | prototype | ) |
Copy the prototype token's fields into this one. Note: Payloads are shared.
void Lucene::Token::reinit | ( | const TokenPtr & | prototype, |
CharArray | newTermBuffer, | ||
int32_t | offset, | ||
int32_t | length | ||
) |
Copy the prototype token's fields into this one, with a different term. Note: Payloads are shared.
void Lucene::Token::reinit | ( | const TokenPtr & | prototype, |
const String & | newTerm | ||
) |
Copy the prototype token's fields into this one, with a different term. Note: Payloads are shared.
|
virtual |
Grows the termBuffer to at least size newSize, preserving the existing content. Note: If the next operation is to change the contents of the term buffer use setTermBuffer(char[], int, int)
, setTermBuffer(String)
, or setTermBuffer(String, int, int)
to optimally combine the resize with the setting of the termBuffer.
newSize | minimum size of the new termBuffer |
|
virtual |
Set the ending offset.
|
virtual |
|
virtual |
Set the starting and ending offset.
|
virtual |
Sets this Token's payload.
|
virtual |
Set the position increment. This determines the position of this token relative to the previous Token in a TokenStream
, used in phrase searching.
The default value is one.
Some common uses for this are:
Set it to zero to put multiple terms in the same position. This is useful if, eg., a word has multiple stems. Searches for phrases including either stem will match. In this case, all but the first stem's increment should be set to zero: the increment of the first instance should be one. Repeating a token with an increment of zero can also be used to boost the scores of matches on that token.
Set it to values greater than one to inhibit exact phrase matches. If, for example, one does not want phrases to match across removed stop words, then one could build a stop word filter that removes stop words and also sets the increment to the number of stop words removed before each non-stop word. Then exact phrase queries will only match when the terms occur with no intervening stop words.
positionIncrement | the distance from the prior term |
|
virtual |
Set the starting offset.
|
virtual |
Copies the contents of buffer into the termBuffer array.
buffer | the buffer to copy |
|
virtual |
Copies the contents of buffer, starting at offset and continuing for length characters, into the termBuffer array.
buffer | the buffer to copy |
offset | the index in the buffer of the first character to copy |
length | the number of characters to copy |
|
virtual |
Copies the contents of buffer, starting at offset for length characters, into the termBuffer array.
buffer | the buffer to copy |
offset | the index in the buffer of the first character to copy |
length | the number of characters to copy |
|
virtual |
Set number of valid characters (length of the term) in the termBuffer array. Use this to truncate the termBuffer or to synchronize with external manipulation of the termBuffer. Note: to grow the size of the array, use resizeTermBuffer(int)
first.
length | the truncated length |
|
virtual |
|
inline |
|
virtual |
Returns this Token's starting offset, the position of the first character corresponding to this token in the source text.
Note that the difference between endOffset() and startOffset() may not be equal to termLength
, as the term text may have been altered by a stemmer or some other filter.
|
virtual |
Returns the Token's term text.
This method has a performance penalty because the text is stored internally in a char[]. If possible, use termBuffer()
and termLength()
directly instead. If you really need a String, use this method, which is nothing more than a convenience call to String(token->termBuffer(), token->termLength())
|
virtual |
Returns the internal termBuffer character array which you can then directly alter. If the array is too small for your token, use resizeTermBuffer(int)
to increase it. After altering the buffer be sure to call setTermLength
to record the number of valid characters that were placed into the termBuffer.
|
virtual |
Optimized implementation of termBuffer.
|
virtual |
Return number of valid characters (length of the term) in the termBuffer array.
|
static |
Convenience factory that returns Token as implementation for the basic attributes.
|
virtual |
Returns a string representation of the object.
Reimplemented from Lucene::LuceneObject.
|
virtual |
Returns this Token's lexical type. Defaults to "word".
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
staticprotected |
|
protected |
|
protected |