1 package org.apache.lucene.search; 2 3 18 19 import org.apache.lucene.index.IndexReader; 20 import org.apache.lucene.index.Term; 21 22 import java.io.IOException ; 23 24 30 public final class FuzzyTermEnum extends FilteredTermEnum { 31 32 36 private static final int TYPICAL_LONGEST_WORD_IN_INDEX = 19; 37 38 41 private int[][] d; 42 43 private float similarity; 44 private boolean endEnum = false; 45 46 private Term searchTerm = null; 47 private final String field; 48 private final String text; 49 private final String prefix; 50 51 private final float minimumSimilarity; 52 private final float scale_factor; 53 private final int[] maxDistances = new int[TYPICAL_LONGEST_WORD_IN_INDEX]; 54 55 66 public FuzzyTermEnum(IndexReader reader, Term term) throws IOException { 67 this(reader, term, FuzzyQuery.defaultMinSimilarity, FuzzyQuery.defaultPrefixLength); 68 } 69 70 82 public FuzzyTermEnum(IndexReader reader, Term term, float minSimilarity) throws IOException { 83 this(reader, term, minSimilarity, FuzzyQuery.defaultPrefixLength); 84 } 85 86 100 public FuzzyTermEnum(IndexReader reader, Term term, final float minSimilarity, final int prefixLength) throws IOException { 101 super(); 102 103 if (minSimilarity >= 1.0f) 104 throw new IllegalArgumentException ("minimumSimilarity cannot be greater than or equal to 1"); 105 else if (minSimilarity < 0.0f) 106 throw new IllegalArgumentException ("minimumSimilarity cannot be less than 0"); 107 if(prefixLength < 0) 108 throw new IllegalArgumentException ("prefixLength cannot be less than 0"); 109 110 this.minimumSimilarity = minSimilarity; 111 this.scale_factor = 1.0f / (1.0f - minimumSimilarity); 112 this.searchTerm = term; 113 this.field = searchTerm.field(); 114 115 final int fullSearchTermLength = searchTerm.text().length(); 118 final int realPrefixLength = prefixLength > fullSearchTermLength ? fullSearchTermLength : prefixLength; 119 120 this.text = searchTerm.text().substring(realPrefixLength); 121 this.prefix = searchTerm.text().substring(0, realPrefixLength); 122 123 initializeMaxDistances(); 124 this.d = initDistanceArray(); 125 126 setEnum(reader.terms(new Term(searchTerm.field(), prefix))); 127 } 128 129 133 protected final boolean termCompare(Term term) { 134 if (field == term.field() && term.text().startsWith(prefix)) { 135 final String target = term.text().substring(prefix.length()); 136 this.similarity = similarity(target); 137 return (similarity > minimumSimilarity); 138 } 139 endEnum = true; 140 return false; 141 } 142 143 public final float difference() { 144 return (float)((similarity - minimumSimilarity) * scale_factor); 145 } 146 147 public final boolean endEnum() { 148 return endEnum; 149 } 150 151 154 155 158 private static final int min(int a, int b, int c) { 159 final int t = (a < b) ? a : b; 160 return (t < c) ? t : c; 161 } 162 163 private final int[][] initDistanceArray(){ 164 return new int[this.text.length() + 1][TYPICAL_LONGEST_WORD_IN_INDEX]; 165 } 166 167 204 private synchronized final float similarity(final String target) { 205 final int m = target.length(); 206 final int n = text.length(); 207 if (n == 0) { 208 return prefix.length() == 0 ? 0.0f : 1.0f - ((float) m / prefix.length()); 211 } 212 if (m == 0) { 213 return prefix.length() == 0 ? 0.0f : 1.0f - ((float) n / prefix.length()); 214 } 215 216 final int maxDistance = getMaxDistance(m); 217 218 if (maxDistance < Math.abs(m-n)) { 219 return 0.0f; 227 } 228 229 if (d[0].length <= m) { 231 growDistanceArray(m); 232 } 233 234 for (int i = 0; i <= n; i++) d[i][0] = i; 236 for (int j = 0; j <= m; j++) d[0][j] = j; 237 238 for (int i = 1; i <= n; i++) { 240 int bestPossibleEditDistance = m; 241 final char s_i = text.charAt(i - 1); 242 for (int j = 1; j <= m; j++) { 243 if (s_i != target.charAt(j-1)) { 244 d[i][j] = min(d[i-1][j], d[i][j-1], d[i-1][j-1])+1; 245 } 246 else { 247 d[i][j] = min(d[i-1][j]+1, d[i][j-1]+1, d[i-1][j-1]); 248 } 249 bestPossibleEditDistance = Math.min(bestPossibleEditDistance, d[i][j]); 250 } 251 252 256 if (i > maxDistance && bestPossibleEditDistance > maxDistance) { return 0.0f; 260 } 261 } 262 263 return 1.0f - ((float)d[n][m] / (float) (prefix.length() + Math.min(n, m))); 269 } 270 271 275 private void growDistanceArray(int m) { 276 for (int i = 0; i < d.length; i++) { 277 d[i] = new int[m+1]; 278 } 279 } 280 281 288 private final int getMaxDistance(int m) { 289 return (m < maxDistances.length) ? maxDistances[m] : calculateMaxDistance(m); 290 } 291 292 private void initializeMaxDistances() { 293 for (int i = 0; i < maxDistances.length; i++) { 294 maxDistances[i] = calculateMaxDistance(i); 295 } 296 } 297 298 private int calculateMaxDistance(int m) { 299 return (int) ((1-minimumSimilarity) * (Math.min(text.length(), m) + prefix.length())); 300 } 301 302 public void close() throws IOException { 303 super.close(); } 305 306 } 307 | Popular Tags |