private TermPositionVector createTermPositionVector(String text)
   {
      // term -> TermVectorOffsetInfo[]
      final SortedMap<String, TermVectorOffsetInfo[]> termMap = new TreeMap<String, TermVectorOffsetInfo[]>();
      Reader r = new StringReader(text);
      TokenStream ts = index.getTextAnalyzer().tokenStream("", r);
      try
      {
         while (ts.incrementToken())
         {
            OffsetAttribute offset = ts.getAttribute(OffsetAttribute.class);
            CharTermAttribute term = ts.getAttribute(CharTermAttribute.class);
            String termText = new String(term.buffer(), 0, term.length());
            TermVectorOffsetInfo[] info = termMap.get(termText);
            if (info == null)
            {
               info = new TermVectorOffsetInfo[1];
            }
            else
            {
               TermVectorOffsetInfo[] tmp = info;
               info = new TermVectorOffsetInfo[tmp.length + 1];
               System.arraycopy(tmp, 0, info, 0, tmp.length);
            }
            info[info.length - 1] = new TermVectorOffsetInfo(offset.startOffset(), offset.endOffset());
            termMap.put(termText, info);
         }
         ts.end();
         ts.close();
      }
      catch (IOException e)
      {
         // should never happen, we are reading from a string
         if (LOG.isTraceEnabled())
         {
            LOG.trace("An exception occurred: " + e.getMessage());
         }
      }

      return new TermPositionVector()
      {

         private String[] terms = termMap.keySet().toArray(new String[termMap.size()]);

         public int[] getTermPositions(int index)
         {
            return null;
         }

         public TermVectorOffsetInfo[] getOffsets(int index)
         {
            TermVectorOffsetInfo[] info = TermVectorOffsetInfo.EMPTY_OFFSET_INFO;
            if (index >= 0 && index < terms.length)
            {
               info = termMap.get(terms[index]);
            }
            return info;
         }

         public String getField()
         {
            return "";
         }

         public int size()
         {
            return terms.length;
         }

         public String[] getTerms()
         {
            return terms;
         }

         public int[] getTermFrequencies()
         {
            int[] freqs = new int[terms.length];
            for (int i = 0; i < terms.length; i++)
            {
               freqs[i] = termMap.get(terms[i]).length;
            }
            return freqs;
         }

         public int indexOf(String term)
         {
            int res = Arrays.binarySearch(terms, term);
            return res >= 0 ? res : -1;
         }

         public int[] indexesOf(String[] terms, int start, int len)
         {
            int[] res = new int[len];
            for (int i = 0; i < len; i++)
            {
               res[i] = indexOf(terms[i]);
            }
            return res;
         }
      };
   }
private TermPositionVector createTermPositionVector ( String text ) { final SortedMap < String , TermVectorOffsetInfo [ ] > termMap = new TreeMap < String , TermVectorOffsetInfo [ ] > ( ) ; Reader r = new StringReader ( text ) ; TokenStream ts = index . getTextAnalyzer ( ) . tokenStream ( "" , r ) ; try { while ( ts . incrementToken ( ) ) { OffsetAttribute offset = ts . getAttribute ( OffsetAttribute . class ) ; CharTermAttribute term = ts . getAttribute ( CharTermAttribute . class ) ; String termText = new String ( term . buffer ( ) , 0 , term . length ( ) ) ; TermVectorOffsetInfo [ ] info = termMap . get ( termText ) ; if ( info == null ) { info = new TermVectorOffsetInfo [ 1 ] ; } else { TermVectorOffsetInfo [ ] tmp = info ; info = new TermVectorOffsetInfo [ tmp . length + 1 ] ; System . arraycopy ( tmp , 0 , info , 0 , tmp . length ) ; } info [ info . length - 1 ] = new TermVectorOffsetInfo ( offset . startOffset ( ) , offset . endOffset ( ) ) ; termMap . put ( termText , info ) ; } ts . end ( ) ; ts . close ( ) ; } catch ( IOException e ) { if ( LOG . isTraceEnabled ( ) ) { LOG . trace ( "An exception occurred: " + e . getMessage ( ) ) ; } } return new TermPositionVector ( ) { private String [ ] terms = termMap . keySet ( ) . toArray ( new String [ termMap . size ( ) ] ) ; public int [ ] getTermPositions ( int index ) { return null ; } public TermVectorOffsetInfo [ ] getOffsets ( int index ) { TermVectorOffsetInfo [ ] info = TermVectorOffsetInfo . EMPTY_OFFSET_INFO ; if ( index >= 0 && index < terms . length ) { info = termMap . get ( terms [ index ] ) ; } return info ; } public String getField ( ) { return "" ; } public int size ( ) { return terms . length ; } public String [ ] getTerms ( ) { return terms ; } public int [ ] getTermFrequencies ( ) { int [ ] freqs = new int [ terms . length ] ; for ( int i = 0 ; i < terms . length ; i ++ ) { freqs [ i ] = termMap . get ( terms [ i ] ) . length ; } return freqs ; } public int indexOf ( String term ) { int res = Arrays . binarySearch ( terms , term ) ; return res >= 0 ? res : - 1 ; } public int [ ] indexesOf ( String [ ] terms , int start , int len ) { int [ ] res = new int [ len ] ; for ( int i = 0 ; i < len ; i ++ ) { res [ i ] = indexOf ( terms [ i ] ) ; } return res ; } } ; }
@param text the text.
@return a <code>TermPositionVector</code> for the given text.