FileDocCategorySizeDatePackage
Token.javaAPI DocApache Lucene 2.1.05329Wed Feb 14 10:46:38 GMT 2007org.apache.lucene.analysis

Token

public class Token extends Object implements Cloneable
A Token is an occurence 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 an interned 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".

Fields Summary
String
termText
int
startOffset
int
endOffset
String
type
private int
positionIncrement
Constructors Summary
public Token(String text, int start, int end)
Constructs a Token with the given term text, and start & end offsets. The type defaults to "word."


                          
         
    termText = text;
    startOffset = start;
    endOffset = end;
  
public Token(String text, int start, int end, String typ)
Constructs a Token with the given text, start and end offsets, & type.

    termText = text;
    startOffset = start;
    endOffset = end;
    type = typ;
  
Methods Summary
public java.lang.Objectclone()

    try {
      return super.clone();
    } catch (CloneNotSupportedException e) {
      throw new RuntimeException(e); // shouldn't happen since we implement Cloneable
    }
  
public final intendOffset()
Returns this Token's ending offset, one greater than the position of the last character corresponding to this token in the source text.

 return endOffset; 
public intgetPositionIncrement()
Returns the position increment of this Token.

see
#setPositionIncrement

 return positionIncrement; 
public voidsetPositionIncrement(int positionIncrement)
Set the position increment. This determines the position of this token relative to the previous Token in a {@link 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, e.g., 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.

see
org.apache.lucene.index.TermPositions

    if (positionIncrement < 0)
      throw new IllegalArgumentException
        ("Increment must be zero or greater: " + positionIncrement);
    this.positionIncrement = positionIncrement;
  
public voidsetTermText(java.lang.String text)
Sets the Token's term text.

    termText = text;
  
public final intstartOffset()
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.

 return startOffset; 
public final java.lang.StringtermText()
Returns the Token's term text.

 return termText; 
public java.lang.StringtoString()

    StringBuffer sb = new StringBuffer();
    sb.append("(" + termText + "," + startOffset + "," + endOffset);
    if (!type.equals("word"))
      sb.append(",type="+type);
    if (positionIncrement != 1)
      sb.append(",posIncr="+positionIncrement);
    sb.append(")");
    return sb.toString();
  
public final java.lang.Stringtype()
Returns this Token's lexical type. Defaults to "word".

 return type;