1 package org.apache.lucene.search; 2 3 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 32 public class QueryTermVector implements TermFreqVector { 33 private String [] terms = new String [0]; 34 private int [] termFreqs = new int[0]; 35 36 public String getField() { return null; } 37 38 42 public QueryTermVector(String [] queryTerms) { 43 44 processTerms(queryTerms); 45 } 46 47 public QueryTermVector(String queryString, Analyzer analyzer) { 48 if (analyzer != null) 49 { 50 TokenStream stream = analyzer.tokenStream("", new StringReader (queryString)); 51 if (stream != null) 52 { 53 Token next = null; 54 List terms = new ArrayList(); 55 try { 56 while ((next = stream.next()) != null) 57 { 58 terms.add(next.termText()); 59 } 60 processTerms((String [])terms.toArray(new String [terms.size()])); 61 } catch (IOException e) { 62 } 63 } 64 } 65 } 66 67 private void processTerms(String [] queryTerms) { 68 if (queryTerms != null) { 69 Arrays.sort(queryTerms); 70 Map tmpSet = new HashMap(queryTerms.length); 71 List tmpList = new ArrayList(queryTerms.length); 73 List tmpFreqs = new ArrayList(queryTerms.length); 74 int j = 0; 75 for (int i = 0; i < queryTerms.length; i++) { 76 String term = queryTerms[i]; 77 Integer position = (Integer )tmpSet.get(term); 78 if (position == null) { 79 tmpSet.put(term, new Integer (j++)); 80 tmpList.add(term); 81 tmpFreqs.add(new Integer (1)); 82 } 83 else { 84 Integer integer = (Integer )tmpFreqs.get(position.intValue()); 85 tmpFreqs.set(position.intValue(), new Integer (integer.intValue() + 1)); 86 } 87 } 88 terms = (String [])tmpList.toArray(terms); 89 termFreqs = new int[tmpFreqs.size()]; 91 int i = 0; 92 for (Iterator iter = tmpFreqs.iterator(); iter.hasNext();) { 93 Integer integer = (Integer ) iter.next(); 94 termFreqs[i++] = integer.intValue(); 95 } 96 } 97 } 98 99 public final String toString() { 100 StringBuffer sb = new StringBuffer (); 101 sb.append('{'); 102 for (int i=0; i<terms.length; i++) { 103 if (i>0) sb.append(", "); 104 sb.append(terms[i]).append('/').append(termFreqs[i]); 105 } 106 sb.append('}'); 107 return sb.toString(); 108 } 109 110 111 public int size() { 112 return terms.length; 113 } 114 115 public String [] getTerms() { 116 return terms; 117 } 118 119 public int[] getTermFrequencies() { 120 return termFreqs; 121 } 122 123 public int indexOf(String term) { 124 int res = Arrays.binarySearch(terms, term); 125 return res >= 0 ? res : -1; 126 } 127 128 public int[] indexesOf(String [] terms, int start, int len) { 129 int res[] = new int[len]; 130 131 for (int i=0; i < len; i++) { 132 res[i] = indexOf(terms[i]); 133 } 134 return res; 135 } 136 137 } 138 | Popular Tags |