01 package gate.creole.annic.apache.lucene.index;
02 import java.io.IOException;
03 import java.util.*;
04
05 /**
06 */
07 class SegmentTermVector implements TermFreqVector {
08 private String field;
09 private String terms[];
10 private int termFreqs[];
11
12 SegmentTermVector(String field, String terms[], int termFreqs[]) {
13 this.field = field;
14 this.terms = terms;
15 this.termFreqs = termFreqs;
16 }
17
18 /**
19 *
20 * @return The number of the field this vector is associated with
21 */
22 public String getField() {
23 return field;
24 }
25
26 public String toString() {
27 StringBuffer sb = new StringBuffer();
28 sb.append('{');
29 sb.append(field).append(": ");
30 for (int i=0; i<terms.length; i++) {
31 if (i>0) sb.append(", ");
32 sb.append(terms[i]).append('/').append(termFreqs[i]);
33 }
34 sb.append('}');
35 return sb.toString();
36 }
37
38 public int size() {
39 return terms == null ? 0 : terms.length;
40 }
41
42 public String [] getTerms() {
43 return terms;
44 }
45
46 public int[] getTermFrequencies() {
47 return termFreqs;
48 }
49
50 public int indexOf(String termText) {
51 int res = Arrays.binarySearch(terms, termText);
52 return res >= 0 ? res : -1;
53 }
54
55 public int[] indexesOf(String [] termNumbers, int start, int len) {
56 // TODO: there must be a more efficient way of doing this.
57 // At least, we could advance the lower bound of the terms array
58 // as we find valid indexes. Also, it might be possible to leverage
59 // this even more by starting in the middle of the termNumbers array
60 // and thus dividing the terms array maybe in half with each found index.
61 int res[] = new int[len];
62
63 for (int i=0; i < len; i++) {
64 res[i] = indexOf(termNumbers[i]);
65 }
66 return res;
67 }
68 }
|