001: package org.apache.lucene.search.highlight;
002:
003: /**
004: * Copyright 2002-2004 The Apache Software Foundation
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018: import org.apache.lucene.analysis.Token;
019:
020: /**
021: * One, or several overlapping tokens, along with the score(s) and the
022: * scope of the original text
023: * @author MAHarwood
024: */
025: public class TokenGroup {
026:
027: private static final int MAX_NUM_TOKENS_PER_GROUP = 50;
028: Token[] tokens = new Token[MAX_NUM_TOKENS_PER_GROUP];
029: float[] scores = new float[MAX_NUM_TOKENS_PER_GROUP];
030: int numTokens = 0;
031: int startOffset = 0;
032: int endOffset = 0;
033:
034: void addToken(Token token, float score) {
035: if (numTokens < MAX_NUM_TOKENS_PER_GROUP) {
036: if (numTokens == 0) {
037: startOffset = token.startOffset();
038: endOffset = token.endOffset();
039: } else {
040: startOffset = Math
041: .min(startOffset, token.startOffset());
042: endOffset = Math.max(endOffset, token.endOffset());
043: }
044: tokens[numTokens] = token;
045: scores[numTokens] = score;
046: numTokens++;
047: }
048: }
049:
050: boolean isDistinct(Token token) {
051: return token.startOffset() >= endOffset;
052: }
053:
054: void clear() {
055: numTokens = 0;
056: }
057:
058: /**
059: *
060: * @param index a value between 0 and numTokens -1
061: * @return the "n"th token
062: */
063: public Token getToken(int index) {
064: return tokens[index];
065: }
066:
067: /**
068: *
069: * @param index a value between 0 and numTokens -1
070: * @return the "n"th score
071: */
072: public float getScore(int index) {
073: return scores[index];
074: }
075:
076: /**
077: * @return the end position in the original text
078: */
079: public int getEndOffset() {
080: return endOffset;
081: }
082:
083: /**
084: * @return the number of tokens in this group
085: */
086: public int getNumTokens() {
087: return numTokens;
088: }
089:
090: /**
091: * @return the start position in the original text
092: */
093: public int getStartOffset() {
094: return startOffset;
095: }
096:
097: /**
098: * @return all tokens' scores summed up
099: */
100: public float getTotalScore() {
101: float total = 0;
102: for (int i = 0; i < numTokens; i++) {
103: total += scores[i];
104: }
105: return total;
106: }
107: }
|