KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > index > SegmentTermVector


1 package org.apache.lucene.index;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.util.*;
20
21 /**
22  */

23 class SegmentTermVector implements TermFreqVector {
24   private String JavaDoc field;
25   private String JavaDoc terms[];
26   private int termFreqs[];
27   
28   SegmentTermVector(String JavaDoc field, String JavaDoc terms[], int termFreqs[]) {
29     this.field = field;
30     this.terms = terms;
31     this.termFreqs = termFreqs;
32   }
33
34   /**
35    *
36    * @return The number of the field this vector is associated with
37    */

38   public String JavaDoc getField() {
39     return field;
40   }
41
42   public String JavaDoc toString() {
43     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
44     sb.append('{');
45     sb.append(field).append(": ");
46     if(terms != null){
47       for (int i=0; i<terms.length; i++) {
48         if (i>0) sb.append(", ");
49         sb.append(terms[i]).append('/').append(termFreqs[i]);
50       }
51     }
52     sb.append('}');
53     
54     return sb.toString();
55   }
56
57   public int size() {
58     return terms == null ? 0 : terms.length;
59   }
60
61   public String JavaDoc [] getTerms() {
62     return terms;
63   }
64
65   public int[] getTermFrequencies() {
66     return termFreqs;
67   }
68
69   public int indexOf(String JavaDoc termText) {
70     if(terms == null)
71       return -1;
72     int res = Arrays.binarySearch(terms, termText);
73     return res >= 0 ? res : -1;
74   }
75
76   public int[] indexesOf(String JavaDoc [] termNumbers, int start, int len) {
77     // TODO: there must be a more efficient way of doing this.
78
// At least, we could advance the lower bound of the terms array
79
// as we find valid indexes. Also, it might be possible to leverage
80
// this even more by starting in the middle of the termNumbers array
81
// and thus dividing the terms array maybe in half with each found index.
82
int res[] = new int[len];
83
84     for (int i=0; i < len; i++) {
85       res[i] = indexOf(termNumbers[start+ i]);
86     }
87     return res;
88   }
89 }
90
Popular Tags