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