Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
../../../../img/srcFileCovDistChart6.png 74% of files have more coverage
49   137   21   4.9
22   94   0.43   10
10     2.1  
1    
 
  QueryTermVector       Line # 32 49 21 59.3% 0.5925926
 
  (1)
 
1    package org.apache.lucene.search;
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 org.apache.lucene.analysis.Analyzer;
20    import org.apache.lucene.analysis.Token;
21    import org.apache.lucene.analysis.TokenStream;
22    import org.apache.lucene.index.TermFreqVector;
23   
24    import java.io.IOException;
25    import java.io.StringReader;
26    import java.util.*;
27   
28    /**
29    *
30    *
31    **/
 
32    public class QueryTermVector implements TermFreqVector {
33    private String [] terms = new String[0];
34    private int [] termFreqs = new int[0];
35   
 
36  0 toggle public String getField() { return null; }
37   
38    /**
39    *
40    * @param queryTerms The original list of terms from the query, can contain duplicates
41    */
 
42  2 toggle public QueryTermVector(String [] queryTerms) {
43   
44  2 processTerms(queryTerms);
45    }
46   
 
47  1 toggle public QueryTermVector(String queryString, Analyzer analyzer) {
48  1 if (analyzer != null)
49    {
50  1 TokenStream stream = analyzer.tokenStream("", new StringReader(queryString));
51  1 if (stream != null)
52    {
53  1 Token next = null;
54  1 List terms = new ArrayList();
55  1 try {
56  ? while ((next = stream.next()) != null)
57    {
58  9 terms.add(next.termText());
59    }
60  1 processTerms((String[])terms.toArray(new String[terms.size()]));
61    } catch (IOException e) {
62    }
63    }
64    }
65    }
66   
 
67  3 toggle private void processTerms(String[] queryTerms) {
68  3 if (queryTerms != null) {
69  2 Arrays.sort(queryTerms);
70  2 Map tmpSet = new HashMap(queryTerms.length);
71    //filter out duplicates
72  2 List tmpList = new ArrayList(queryTerms.length);
73  2 List tmpFreqs = new ArrayList(queryTerms.length);
74  2 int j = 0;
75  20 for (int i = 0; i < queryTerms.length; i++) {
76  18 String term = queryTerms[i];
77  18 Integer position = (Integer)tmpSet.get(term);
78  18 if (position == null) {
79  8 tmpSet.put(term, new Integer(j++));
80  8 tmpList.add(term);
81  8 tmpFreqs.add(new Integer(1));
82    }
83    else {
84  10 Integer integer = (Integer)tmpFreqs.get(position.intValue());
85  10 tmpFreqs.set(position.intValue(), new Integer(integer.intValue() + 1));
86    }
87    }
88  2 terms = (String[])tmpList.toArray(terms);
89    //termFreqs = (int[])tmpFreqs.toArray(termFreqs);
90  2 termFreqs = new int[tmpFreqs.size()];
91  2 int i = 0;
92  10 for (Iterator iter = tmpFreqs.iterator(); iter.hasNext();) {
93  8 Integer integer = (Integer) iter.next();
94  8 termFreqs[i++] = integer.intValue();
95    }
96    }
97    }
98   
 
99  0 toggle public final String toString() {
100  0 StringBuffer sb = new StringBuffer();
101  0 sb.append('{');
102  0 for (int i=0; i<terms.length; i++) {
103  0 if (i>0) sb.append(", ");
104  0 sb.append(terms[i]).append('/').append(termFreqs[i]);
105    }
106  0 sb.append('}');
107  0 return sb.toString();
108    }
109   
110   
 
111  0 toggle public int size() {
112  0 return terms.length;
113    }
114   
 
115  3 toggle public String[] getTerms() {
116  3 return terms;
117    }
118   
 
119  2 toggle public int[] getTermFrequencies() {
120  2 return termFreqs;
121    }
122   
 
123  0 toggle public int indexOf(String term) {
124  0 int res = Arrays.binarySearch(terms, term);
125  0 return res >= 0 ? res : -1;
126    }
127   
 
128  0 toggle public int[] indexesOf(String[] terms, int start, int len) {
129  0 int res[] = new int[len];
130   
131  0 for (int i=0; i < len; i++) {
132  0 res[i] = indexOf(terms[i]);
133    }
134  0 return res;
135    }
136   
137    }