Show / Hide Table of Contents

    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 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 if loading from a string, or with , 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]);
    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.

    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.

    Inheritance
    System.Object
    Attribute
    CharTermAttribute
    Token
    Implements
    ICharTermAttribute
    ICharSequence
    ITermToBytesRefAttribute
    ITypeAttribute
    IPositionIncrementAttribute
    IFlagsAttribute
    IOffsetAttribute
    IPayloadAttribute
    IPositionLengthAttribute
    IAttribute
    Inherited Members
    CharTermAttribute.CopyBuffer(Char[], Int32, Int32)
    CharTermAttribute.Buffer
    CharTermAttribute.ResizeBuffer(Int32)
    CharTermAttribute.Length
    CharTermAttribute.SetLength(Int32)
    CharTermAttribute.SetEmpty()
    CharTermAttribute.FillBytesRef()
    CharTermAttribute.BytesRef
    CharTermAttribute.Item[Int32]
    CharTermAttribute.SubSequence(Int32, Int32)
    CharTermAttribute.Append(String, Int32, Int32)
    CharTermAttribute.Append(Char)
    CharTermAttribute.Append(Char[])
    CharTermAttribute.Append(Char[], Int32, Int32)
    CharTermAttribute.Append(String)
    CharTermAttribute.Append(StringBuilder)
    CharTermAttribute.Append(StringBuilder, Int32, Int32)
    CharTermAttribute.Append(ICharTermAttribute)
    CharTermAttribute.Append(ICharSequence)
    CharTermAttribute.Append(ICharSequence, Int32, Int32)
    CharTermAttribute.ToString()
    Attribute.ReflectAsString(Boolean)
    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 Source

    Token()

    Constructs a Token will null text.

    Declaration
    public Token()
    | Improve this Doc View Source

    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

    | Improve this Doc View Source

    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

    | Improve this Doc View Source

    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

    | Improve this Doc View Source

    Token(Int32, Int32, String)

    Constructs a Token with null text and start & end offsets plus the Token type.

    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

    | Improve this Doc View Source

    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

    | Improve this Doc View Source

    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

    | Improve this Doc View Source

    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 Source

    TOKEN_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 Source

    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).

    Declaration
    public int EndOffset { get; }
    Property Value
    Type Description
    System.Int32
    See Also
    SetOffset(Int32, Int32)
    IOffsetAttribute
    | Improve this Doc View Source

    Flags

    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
    IFlagsAttribute
    | Improve this Doc View Source

    Payload

    Gets or Sets this Token's payload.

    Declaration
    public virtual BytesRef Payload { get; set; }
    Property Value
    Type Description
    BytesRef
    See Also
    IPayloadAttribute
    | Improve this Doc View Source

    PositionIncrement

    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
    See Also
    IPositionIncrementAttribute
    | Improve this Doc View Source

    PositionLength

    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
    See Also
    IPositionLengthAttribute
    | Improve this Doc View Source

    StartOffset

    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
    SetOffset(Int32, Int32)
    IOffsetAttribute
    | Improve this Doc View Source

    Type

    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 Source

    Clear()

    Resets the term text, payload, flags, and positionIncrement, startOffset, endOffset and token type to default.

    Declaration
    public override void Clear()
    Overrides
    CharTermAttribute.Clear()
    | Improve this Doc View Source

    Clone()

    Declaration
    public override object Clone()
    Returns
    Type Description
    System.Object
    Overrides
    CharTermAttribute.Clone()
    | Improve this Doc View Source

    Clone(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
    | Improve this Doc View Source

    CopyTo(IAttribute)

    Declaration
    public override void CopyTo(IAttribute target)
    Parameters
    Type Name Description
    IAttribute target
    Overrides
    CharTermAttribute.CopyTo(IAttribute)
    | Improve this Doc View Source

    Equals(Object)

    Declaration
    public override bool Equals(object obj)
    Parameters
    Type Name Description
    System.Object obj
    Returns
    Type Description
    System.Boolean
    Overrides
    CharTermAttribute.Equals(Object)
    | Improve this Doc View Source

    GetHashCode()

    Declaration
    public override int GetHashCode()
    Returns
    Type Description
    System.Int32
    Overrides
    CharTermAttribute.GetHashCode()
    | Improve this Doc View Source

    ReflectWith(IAttributeReflector)

    Declaration
    public override void ReflectWith(IAttributeReflector reflector)
    Parameters
    Type Name Description
    IAttributeReflector reflector
    Overrides
    CharTermAttribute.ReflectWith(IAttributeReflector)
    | Improve this Doc View Source

    Reinit(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

    | Improve this Doc View Source

    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

    | Improve this Doc View Source

    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

    | Improve this Doc View Source

    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

    | Improve this Doc View Source

    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

    | Improve this Doc View Source

    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

    | Improve this Doc View Source

    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

    | Improve this Doc View Source

    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

    | Improve this Doc View Source

    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

    | Improve this Doc View Source

    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
    See Also
    StartOffset
    EndOffset
    IOffsetAttribute

    Implements

    ICharTermAttribute
    ICharSequence
    ITermToBytesRefAttribute
    ITypeAttribute
    IPositionIncrementAttribute
    IFlagsAttribute
    IOffsetAttribute
    IPayloadAttribute
    IPositionLengthAttribute
    IAttribute
    • Improve this Doc
    • View Source
    Back to top Copyright © 2020 Licensed to the Apache Software Foundation (ASF)