home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lucene example source code file (TokenStreamFromTermPositionVector.java)

This example Lucene source code file (TokenStreamFromTermPositionVector.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Lucene tags/keywords

arraylist, comparator, comparator, io, ioexception, ioexception, iterator, override, string, termvectoroffsetinfo, token, token, tokenstream, tokenstreamfromtermpositionvector, tokenstreamfromtermpositionvector, util

The Lucene TokenStreamFromTermPositionVector.java source code

package org.apache.lucene.search.highlight;

 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
import org.apache.lucene.index.TermPositionVector;
import org.apache.lucene.index.TermVectorOffsetInfo;
import org.apache.lucene.util.CollectionUtil;

public final class TokenStreamFromTermPositionVector extends TokenStream {

  private final List<Token> positionedTokens = new ArrayList();

  private Iterator<Token> tokensAtCurrentPosition;

  private CharTermAttribute termAttribute;

  private PositionIncrementAttribute positionIncrementAttribute;

  private OffsetAttribute offsetAttribute;

   * Constructor.
   * @param termPositionVector TermPositionVector that contains the data for
   *        creating the TokenStream. Must have positions and offsets.
  public TokenStreamFromTermPositionVector(
      final TermPositionVector termPositionVector) {
    termAttribute = addAttribute(CharTermAttribute.class);
    positionIncrementAttribute = addAttribute(PositionIncrementAttribute.class);
    offsetAttribute = addAttribute(OffsetAttribute.class);
    final String[] terms = termPositionVector.getTerms();
    for (int i = 0; i < terms.length; i++) {
      final TermVectorOffsetInfo[] offsets = termPositionVector.getOffsets(i);
      final int[] termPositions = termPositionVector.getTermPositions(i);
      for (int j = 0; j < termPositions.length; j++) {
        Token token;
        if (offsets != null) {
          token = new Token(terms[i].toCharArray(), 0, terms[i].length(),
              offsets[j].getStartOffset(), offsets[j].getEndOffset());
        } else {
          token = new Token();
        // Yes - this is the position, not the increment! This is for
        // sorting. This value
        // will be corrected before use.
    CollectionUtil.mergeSort(this.positionedTokens, tokenComparator);
    int lastPosition = -1;
    for (final Token token : this.positionedTokens) {
      int thisPosition = token.getPositionIncrement();
      token.setPositionIncrement(thisPosition - lastPosition);
      lastPosition = thisPosition;
    this.tokensAtCurrentPosition = this.positionedTokens.iterator();

  private static final Comparator<Token> tokenComparator = new Comparator() {
    public int compare(final Token o1, final Token o2) {
      return o1.getPositionIncrement() - o2.getPositionIncrement();
  public boolean incrementToken() throws IOException {
    if (this.tokensAtCurrentPosition.hasNext()) {
      final Token next = this.tokensAtCurrentPosition.next();
      offsetAttribute.setOffset(next.startOffset(), next.endOffset());
      return true;
    return false;

  public void reset() throws IOException {
    this.tokensAtCurrentPosition = this.positionedTokens.iterator();

Other Lucene examples (source code examples)

Here is a short list of links related to this Lucene TokenStreamFromTermPositionVector.java source code file:

my book on functional programming


new blog posts


Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.