Class Token
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, e.g., to display highlighted query terms in a document browser, or to show matching text fragments in a KWIC (KeyWord In Context) 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 GetPayload() to retrieve the payloads from the index.
NOTE: As of 2.9, Token implements all IAttribute interfaces that are part of core Lucene and can be found in the Lucene.Net.Analysis.TokenAttributes namespace. Even though it is not necessary to use Token anymore, with the new TokenStream API it can be used as convenience class that implements all IAttributes, which is especially useful to easily switch from the old to the new TokenStream API.
Tokenizers and TokenFilters should try to re-use a Token instance when possible for best performance, by implementing the 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 CopyBuffer(Char[], Int32, Int32). To load from a System.String use SetEmpty() followed by Append(String) or Append(String, Int32, Int32). Alternatively you can get the Token's termBuffer by calling either Buffer, if you know that your text is shorter than the capacity of the termBuffer or ResizeBuffer(Int32), if there is any possibility that you may need to grow the buffer. Fill in the characters of your term into this buffer, with System.String.ToCharArray(System.Int32,System.Int32) if loading from a string, or with System.Array.Copy(System.Array,System.Int32,System.Array,System.Int32,System.Int32), and finally call SetLength(Int32) to set the length of the term text. See LUCENE-969 for details.
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.Buffer, 0, source.Length, source.StartOffset, source.EndOffset[, source.Type]);
- 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.
Please note: With Lucene 3.1, the ToString() method had to be changed to match the ICharSequence interface introduced by the interface ICharTermAttribute. this method now only prints the term text, no additional information anymore.
Implements
Inherited Members
Namespace: Lucene.Net.Analysis
Assembly: Lucene.Net.dll
Syntax
public class Token : CharTermAttribute, ICharTermAttribute, ICharSequence, ITermToBytesRefAttribute, ITypeAttribute, IPositionIncrementAttribute, IFlagsAttribute, IOffsetAttribute, IPayloadAttribute, IPositionLengthAttribute, IAttribute
Constructors
| Improve this Doc View SourceToken()
Constructs a Token will null text.
Declaration
public Token()
Token(Char[], Int32, Int32, Int32, Int32)
Constructs a Token with the given term buffer (offset & length), start and end offsets
Declaration
public Token(char[] startTermBuffer, int termBufferOffset, int termBufferLength, int start, int end)
Parameters
Type | Name | Description |
---|---|---|
System.Char[] | startTermBuffer | buffer containing term text |
System.Int32 | termBufferOffset | the index in the buffer of the first character |
System.Int32 | termBufferLength | number of valid characters in the buffer |
System.Int32 | start | start offset in the source text |
System.Int32 | end | end offset in the source text |
Token(Int32, Int32)
Constructs a Token with null text and start & end offsets.
Declaration
public Token(int start, int end)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | start | start offset in the source text |
System.Int32 | end | end offset in the source text |
Token(Int32, Int32, Int32)
Constructs a Token with null text and start & end offsets plus flags. NOTE: flags is EXPERIMENTAL.
Declaration
public Token(int start, int end, int flags)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | start | start offset in the source text |
System.Int32 | end | end offset in the source text |
System.Int32 | flags | The bits to set for this token |
Token(Int32, Int32, String)
Declaration
public Token(int start, int end, string typ)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | start | start offset in the source text |
System.Int32 | end | end offset in the source text |
System.String | typ | the lexical type of this Token |
Token(String, Int32, Int32)
Constructs a Token with the given term text, and start & 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.
Declaration
public Token(string text, int start, int end)
Parameters
Type | Name | Description |
---|---|---|
System.String | text | term text |
System.Int32 | start | start offset in the source text |
System.Int32 | end | end offset in the source text |
Token(String, Int32, Int32, Int32)
Constructs a Token with the given text, start and end offsets, & type. NOTE: for better indexing speed you should instead use the char[] termBuffer methods to set the term text.
Declaration
public Token(string text, int start, int end, int flags)
Parameters
Type | Name | Description |
---|---|---|
System.String | text | term text |
System.Int32 | start | start offset in the source text |
System.Int32 | end | end offset in the source text |
System.Int32 | flags | token type bits |
Token(String, Int32, Int32, String)
Constructs a Token with the given text, start and end offsets, & type. NOTE: for better indexing speed you should instead use the char[] termBuffer methods to set the term text.
Declaration
public Token(string text, int start, int end, string typ)
Parameters
Type | Name | Description |
---|---|---|
System.String | text | term text |
System.Int32 | start | start offset in the source text |
System.Int32 | end | end offset in the source text |
System.String | typ | token type |
Fields
| Improve this Doc View SourceTOKEN_ATTRIBUTE_FACTORY
Convenience factory that returns Token as implementation for the basic attributes and return the default impl (with "Impl" appended) for all other attributes. @since 3.0
Declaration
public static readonly AttributeSource.AttributeFactory TOKEN_ATTRIBUTE_FACTORY
Field Value
Type | Description |
---|---|
AttributeSource.AttributeFactory |
Properties
| Improve this Doc View SourceEndOffset
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).
Declaration
public int EndOffset { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
See Also
| Improve this Doc View SourceFlags
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 TokenFilters.
Declaration
public virtual int Flags { get; set; }
Property Value
Type | Description |
---|---|
System.Int32 |
See Also
| Improve this Doc View SourcePayload
Gets or Sets this Token's payload.
Declaration
public virtual BytesRef Payload { get; set; }
Property Value
Type | Description |
---|---|
BytesRef |
See Also
| Improve this Doc View SourcePositionIncrement
Gets or Sets the position increment (the distance from the prior term). The default value is one.
Declaration
public virtual int PositionIncrement { get; set; }
Property Value
Type | Description |
---|---|
System.Int32 |
Exceptions
Type | Condition |
---|---|
System.ArgumentException | if value is set to a negative value. |
See Also
| Improve this Doc View SourcePositionLength
Gets or Sets the position length of this Token (how many positions this token spans).
The default value is one.
Declaration
public virtual int PositionLength { get; set; }
Property Value
Type | Description |
---|---|
System.Int32 |
Exceptions
Type | Condition |
---|---|
System.ArgumentException | if value is set to zero or negative. |
See Also
| Improve this Doc View SourceStartOffset
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 termText.Length, as the term text may have been altered by a stemmer or some other filter.
Declaration
public int StartOffset { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
See Also
| Improve this Doc View SourceType
Gets or Sets this Token's lexical type. Defaults to "word".
Declaration
public string Type { get; set; }
Property Value
Type | Description |
---|---|
System.String |
Methods
| Improve this Doc View SourceClear()
Resets the term text, payload, flags, and positionIncrement, startOffset, endOffset and token type to default.
Declaration
public override void Clear()
Overrides
| Improve this Doc View SourceClone()
Declaration
public override object Clone()
Returns
Type | Description |
---|---|
System.Object |
Overrides
| Improve this Doc View SourceClone(Char[], Int32, Int32, Int32, Int32)
Makes a clone, but replaces the term buffer & start/end offset in the process. This is more efficient than doing a full clone (and then calling CopyBuffer(Char[], Int32, Int32)) because it saves a wasted copy of the old termBuffer.
Declaration
public virtual Token Clone(char[] newTermBuffer, int newTermOffset, int newTermLength, int newStartOffset, int newEndOffset)
Parameters
Type | Name | Description |
---|---|---|
System.Char[] | newTermBuffer | |
System.Int32 | newTermOffset | |
System.Int32 | newTermLength | |
System.Int32 | newStartOffset | |
System.Int32 | newEndOffset |
Returns
Type | Description |
---|---|
Token |
CopyTo(IAttribute)
Declaration
public override void CopyTo(IAttribute target)
Parameters
Type | Name | Description |
---|---|---|
IAttribute | target |
Overrides
| Improve this Doc View SourceEquals(Object)
Declaration
public override bool Equals(object obj)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj |
Returns
Type | Description |
---|---|
System.Boolean |
Overrides
| Improve this Doc View SourceGetHashCode()
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
System.Int32 |
Overrides
| Improve this Doc View SourceReflectWith(IAttributeReflector)
Declaration
public override void ReflectWith(IAttributeReflector reflector)
Parameters
Type | Name | Description |
---|---|---|
IAttributeReflector | reflector |
Overrides
| Improve this Doc View SourceReinit(Token)
Copy the prototype token's fields into this one. Note: Payloads are shared.
Declaration
public virtual void Reinit(Token prototype)
Parameters
Type | Name | Description |
---|---|---|
Token | prototype | source Token to copy fields from |
Reinit(Token, Char[], Int32, Int32)
Copy the prototype token's fields into this one, with a different term. Note: Payloads are shared.
Declaration
public virtual void Reinit(Token prototype, char[] newTermBuffer, int offset, int length)
Parameters
Type | Name | Description |
---|---|---|
Token | prototype | existing Token |
System.Char[] | newTermBuffer | buffer containing new term text |
System.Int32 | offset | the index in the buffer of the first character |
System.Int32 | length | number of valid characters in the buffer |
Reinit(Token, String)
Copy the prototype token's fields into this one, with a different term. Note: Payloads are shared.
Declaration
public virtual void Reinit(Token prototype, string newTerm)
Parameters
Type | Name | Description |
---|---|---|
Token | prototype | existing Token |
System.String | newTerm | new term text |
Reinit(Char[], Int32, Int32, Int32, Int32)
Shorthand for calling Clear(), CopyBuffer(Char[], Int32, Int32), SetOffset(Int32, Int32), Type (set) on DEFAULT_TYPE
Declaration
public virtual Token Reinit(char[] newTermBuffer, int newTermOffset, int newTermLength, int newStartOffset, int newEndOffset)
Parameters
Type | Name | Description |
---|---|---|
System.Char[] | newTermBuffer | |
System.Int32 | newTermOffset | |
System.Int32 | newTermLength | |
System.Int32 | newStartOffset | |
System.Int32 | newEndOffset |
Returns
Type | Description |
---|---|
Token | this Token instance |
Reinit(Char[], Int32, Int32, Int32, Int32, String)
Shorthand for calling Clear(), CopyBuffer(Char[], Int32, Int32), SetOffset(Int32, Int32), Type (set)
Declaration
public virtual Token Reinit(char[] newTermBuffer, int newTermOffset, int newTermLength, int newStartOffset, int newEndOffset, string newType)
Parameters
Type | Name | Description |
---|---|---|
System.Char[] | newTermBuffer | |
System.Int32 | newTermOffset | |
System.Int32 | newTermLength | |
System.Int32 | newStartOffset | |
System.Int32 | newEndOffset | |
System.String | newType |
Returns
Type | Description |
---|---|
Token | this Token instance |
Reinit(String, Int32, Int32)
Shorthand for calling Clear(), Append(String), SetOffset(Int32, Int32), Type (set) on DEFAULT_TYPE
Declaration
public virtual Token Reinit(string newTerm, int newStartOffset, int newEndOffset)
Parameters
Type | Name | Description |
---|---|---|
System.String | newTerm | |
System.Int32 | newStartOffset | |
System.Int32 | newEndOffset |
Returns
Type | Description |
---|---|
Token | this Token instance |
Reinit(String, Int32, Int32, Int32, Int32)
Shorthand for calling Clear(), Append(String, Int32, Int32), SetOffset(Int32, Int32), Type (set) on DEFAULT_TYPE
Declaration
public virtual Token Reinit(string newTerm, int newTermOffset, int newTermLength, int newStartOffset, int newEndOffset)
Parameters
Type | Name | Description |
---|---|---|
System.String | newTerm | |
System.Int32 | newTermOffset | |
System.Int32 | newTermLength | |
System.Int32 | newStartOffset | |
System.Int32 | newEndOffset |
Returns
Type | Description |
---|---|
Token | this Token instance |
Reinit(String, Int32, Int32, Int32, Int32, String)
Shorthand for calling Clear(), Append(String, Int32, Int32), SetOffset(Int32, Int32), Type (set)
Declaration
public virtual Token Reinit(string newTerm, int newTermOffset, int newTermLength, int newStartOffset, int newEndOffset, string newType)
Parameters
Type | Name | Description |
---|---|---|
System.String | newTerm | |
System.Int32 | newTermOffset | |
System.Int32 | newTermLength | |
System.Int32 | newStartOffset | |
System.Int32 | newEndOffset | |
System.String | newType |
Returns
Type | Description |
---|---|
Token | this Token instance |
Reinit(String, Int32, Int32, String)
Shorthand for calling Clear(), Append(String), SetOffset(Int32, Int32), Type (set)
Declaration
public virtual Token Reinit(string newTerm, int newStartOffset, int newEndOffset, string newType)
Parameters
Type | Name | Description |
---|---|---|
System.String | newTerm | |
System.Int32 | newStartOffset | |
System.Int32 | newEndOffset | |
System.String | newType |
Returns
Type | Description |
---|---|
Token | this Token instance |
SetOffset(Int32, Int32)
Set the starting and ending offset.
Declaration
public virtual void SetOffset(int startOffset, int endOffset)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | startOffset | |
System.Int32 | endOffset |
Exceptions
Type | Condition |
---|---|
System.ArgumentException | If |