1 package org.apache.lucene.store.instantiated;
3 import org.apache.lucene.index.TermFreqVector;
5 import java.io.Serializable;
6 import java.util.Arrays;
10 * Copyright 2006 The Apache Software Foundation
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
16 * http://www.apache.org/licenses/LICENSE-2.0
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
26 * Vector space view of a document in an {@link InstantiatedIndexReader}.
28 * @see org.apache.lucene.index.TermFreqVector
30 public class InstantiatedTermFreqVector
31 implements TermFreqVector, Serializable {
33 private static final long serialVersionUID = 1l;
35 private final List<InstantiatedTermDocumentInformation> termDocumentInformations;
36 private final String field;
37 private final String terms[];
38 private final int termFrequencies[];
40 public InstantiatedTermFreqVector(InstantiatedDocument document, String field) {
42 termDocumentInformations = document.getVectorSpace().get(field);
43 terms = new String[termDocumentInformations.size()];
44 termFrequencies = new int[termDocumentInformations.size()];
46 for (int i = 0; i < termDocumentInformations.size(); i++) {
47 InstantiatedTermDocumentInformation termDocumentInformation = termDocumentInformations.get(i);
48 terms[i] = termDocumentInformation.getTerm().text();
49 termFrequencies[i] = termDocumentInformation.getTermPositions().length;
54 * @return The number of the field this vector is associated with
56 public String getField() {
61 public String toString() {
62 StringBuilder sb = new StringBuilder();
64 sb.append(field).append(": ");
66 for (int i = 0; i < terms.length; i++) {
67 if (i > 0) sb.append(", ");
68 sb.append(terms[i]).append('/').append(termFrequencies[i]);
77 return terms == null ? 0 : terms.length;
80 public String[] getTerms() {
84 public int[] getTermFrequencies() {
85 return termFrequencies;
88 public int indexOf(String termText) {
91 int res = Arrays.binarySearch(terms, termText);
92 return res >= 0 ? res : -1;
95 public int[] indexesOf(String[] termNumbers, int start, int len) {
96 // TODO: there must be a more efficient way of doing this.
97 // At least, we could advance the lower bound of the terms array
98 // as we find valid indices. Also, it might be possible to leverage
99 // this even more by starting in the middle of the termNumbers array
100 // and thus dividing the terms array maybe in half with each found index.
101 int res[] = new int[len];
103 for (int i = 0; i < len; i++) {
104 res[i] = indexOf(termNumbers[start + i]);
109 public List<InstantiatedTermDocumentInformation> getTermDocumentInformations() {
110 return termDocumentInformations;