01: package org.apache.lucene.index;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * 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 java.util.*;
21:
22: /**
23: */
24: class SegmentTermVector implements TermFreqVector {
25: private String field;
26: private String terms[];
27: private int termFreqs[];
28:
29: SegmentTermVector(String field, String terms[], int termFreqs[]) {
30: this .field = field;
31: this .terms = terms;
32: this .termFreqs = termFreqs;
33: }
34:
35: /**
36: *
37: * @return The number of the field this vector is associated with
38: */
39: public String getField() {
40: return field;
41: }
42:
43: public String toString() {
44: StringBuffer sb = new StringBuffer();
45: sb.append('{');
46: sb.append(field).append(": ");
47: if (terms != null) {
48: for (int i = 0; i < terms.length; i++) {
49: if (i > 0)
50: sb.append(", ");
51: sb.append(terms[i]).append('/').append(termFreqs[i]);
52: }
53: }
54: sb.append('}');
55:
56: return sb.toString();
57: }
58:
59: public int size() {
60: return terms == null ? 0 : terms.length;
61: }
62:
63: public String[] getTerms() {
64: return terms;
65: }
66:
67: public int[] getTermFrequencies() {
68: return termFreqs;
69: }
70:
71: public int indexOf(String termText) {
72: if (terms == null)
73: return -1;
74: int res = Arrays.binarySearch(terms, termText);
75: return res >= 0 ? res : -1;
76: }
77:
78: public int[] indexesOf(String[] termNumbers, int start, int len) {
79: // TODO: there must be a more efficient way of doing this.
80: // At least, we could advance the lower bound of the terms array
81: // as we find valid indexes. Also, it might be possible to leverage
82: // this even more by starting in the middle of the termNumbers array
83: // and thus dividing the terms array maybe in half with each found index.
84: int res[] = new int[len];
85:
86: for (int i = 0; i < len; i++) {
87: res[i] = indexOf(termNumbers[start + i]);
88: }
89: return res;
90: }
91: }
|