KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > highlight > TokenGroup


1 package org.apache.lucene.search.highlight;
2 import org.apache.lucene.analysis.Token;
3
4 /**
5  * One, or several overlapping tokens, along with the score(s) and the scope of the original text
6  * @author MAHarwood
7  */

8 public class TokenGroup
9 {
10     
11     private static final int MAX_NUM_TOKENS_PER_GROUP=50;
12     Token [] tokens=new Token[MAX_NUM_TOKENS_PER_GROUP];
13     float [] scores=new float[MAX_NUM_TOKENS_PER_GROUP];
14     int numTokens=0;
15     int startOffset=0;
16     int endOffset=0;
17     
18
19     void addToken(Token token, float score)
20     {
21         if(numTokens==0)
22         {
23             startOffset=token.startOffset();
24             endOffset=token.endOffset();
25         }
26         else
27         {
28             startOffset=Math.min(startOffset,token.startOffset());
29             endOffset=Math.max(endOffset,token.endOffset());
30         }
31         tokens[numTokens]=token;
32         scores[numTokens]=score;
33         numTokens++;
34     }
35     
36     boolean isDistinct(Token token)
37     {
38         return token.startOffset()>endOffset;
39     }
40     
41     
42     void clear()
43     {
44         numTokens=0;
45     }
46     
47     /**
48      *
49      * @param index a value between 0 and numTokens -1
50      * @return the "n"th token
51      */

52     public Token getToken(int index)
53     {
54         return tokens[index];
55     }
56
57     /**
58      *
59      * @param index a value between 0 and numTokens -1
60      * @return the "n"th score
61      */

62     public float getScore(int index)
63     {
64         return scores[index];
65     }
66
67     /**
68      * @return the end position in the original text
69      * @uml.property name="endOffset"
70      */

71     public int getEndOffset()
72     {
73         return endOffset;
74     }
75
76     /**
77      * @return the number of tokens in this group
78      * @uml.property name="numTokens"
79      */

80     public int getNumTokens()
81     {
82         return numTokens;
83     }
84
85     /**
86      * @return the start position in the original text
87      * @uml.property name="startOffset"
88      */

89     public int getStartOffset()
90     {
91         return startOffset;
92     }
93
94     /**
95      * @return
96      */

97     public float getTotalScore()
98     {
99         float total=0;
100         for (int i = 0; i < numTokens; i++)
101         {
102             total+=scores[i];
103         }
104         return total;
105     }
106
107 }
108
Popular Tags