pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / highlighter / src / java / org / apache / lucene / search / highlight / TokenGroup.java
1 package org.apache.lucene.search.highlight;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import org.apache.lucene.analysis.Token;
21 import org.apache.lucene.analysis.TokenStream;
22 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
23 import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
24
25 /**
26  * One, or several overlapping tokens, along with the score(s) and the scope of
27  * the original text
28  */
29 public class TokenGroup {
30
31   private static final int MAX_NUM_TOKENS_PER_GROUP = 50;
32   Token [] tokens=new Token[MAX_NUM_TOKENS_PER_GROUP];
33   float[] scores = new float[MAX_NUM_TOKENS_PER_GROUP];
34   int numTokens = 0;
35   int startOffset = 0;
36   int endOffset = 0;
37   float tot;
38   int matchStartOffset, matchEndOffset;
39
40   private OffsetAttribute offsetAtt;
41   private CharTermAttribute termAtt;
42
43   public TokenGroup(TokenStream tokenStream) {
44     offsetAtt = tokenStream.addAttribute(OffsetAttribute.class);
45     termAtt = tokenStream.addAttribute(CharTermAttribute.class);
46   }
47
48   void addToken(float score) {
49     if (numTokens < MAX_NUM_TOKENS_PER_GROUP) {
50       int termStartOffset = offsetAtt.startOffset();
51       int termEndOffset = offsetAtt.endOffset();
52       if (numTokens == 0) {
53         startOffset = matchStartOffset = termStartOffset;
54         endOffset = matchEndOffset = termEndOffset;
55         tot += score;
56       } else {
57         startOffset = Math.min(startOffset, termStartOffset);
58         endOffset = Math.max(endOffset, termEndOffset);
59         if (score > 0) {
60           if (tot == 0) {
61             matchStartOffset = offsetAtt.startOffset();
62             matchEndOffset = offsetAtt.endOffset();
63           } else {
64             matchStartOffset = Math.min(matchStartOffset, termStartOffset);
65             matchEndOffset = Math.max(matchEndOffset, termEndOffset);
66           }
67           tot += score;
68         }
69       }
70       Token token = new Token(termStartOffset, termEndOffset);
71       token.setEmpty().append(termAtt);
72       tokens[numTokens] = token;
73       scores[numTokens] = score;
74       numTokens++;
75     }
76   }
77
78   boolean isDistinct() {
79     return offsetAtt.startOffset() >= endOffset;
80   }
81
82   void clear() {
83     numTokens = 0;
84     tot = 0;
85   }
86   
87   /* 
88   * @param index a value between 0 and numTokens -1
89   * @return the "n"th token
90   */
91  public Token getToken(int index)
92  {
93      return tokens[index];
94  }
95
96   /**
97    * 
98    * @param index a value between 0 and numTokens -1
99    * @return the "n"th score
100    */
101   public float getScore(int index) {
102     return scores[index];
103   }
104
105   /**
106    * @return the end position in the original text
107    */
108   public int getEndOffset() {
109     return endOffset;
110   }
111
112   /**
113    * @return the number of tokens in this group
114    */
115   public int getNumTokens() {
116     return numTokens;
117   }
118
119   /**
120    * @return the start position in the original text
121    */
122   public int getStartOffset() {
123     return startOffset;
124   }
125
126   /**
127    * @return all tokens' scores summed up
128    */
129   public float getTotalScore() {
130     return tot;
131   }
132 }