1 package org.apache.lucene.search; 2 3 18 19 import java.io.IOException ; 20 import java.io.Serializable ; 21 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 25 import org.apache.lucene.index.Term; 26 27 import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.document.Field; import org.apache.lucene.util.SmallFloat; 31 32 88 public abstract class Similarity implements Serializable { 89 90 private static Similarity defaultImpl = new DefaultSimilarity(); 91 92 98 public static void setDefault(Similarity similarity) { 99 Similarity.defaultImpl = similarity; 100 } 101 102 110 public static Similarity getDefault() { 111 return Similarity.defaultImpl; 112 } 113 114 115 private static final float[] NORM_TABLE = new float[256]; 116 117 static { 118 for (int i = 0; i < 256; i++) 119 NORM_TABLE[i] = SmallFloat.byte315ToFloat((byte)i); 120 } 121 122 125 public static float decodeNorm(byte b) { 126 return NORM_TABLE[b & 0xFF]; } 128 129 132 public static float[] getNormDecoder() { 133 return NORM_TABLE; 134 } 135 136 157 public abstract float lengthNorm(String fieldName, int numTokens); 158 159 169 public abstract float queryNorm(float sumOfSquaredWeights); 170 171 185 public static byte encodeNorm(float f) { 186 return SmallFloat.floatToByte315(f); 187 } 188 189 190 205 public float tf(int freq) { 206 return tf((float)freq); 207 } 208 209 222 public abstract float sloppyFreq(int distance); 223 224 237 public abstract float tf(float freq); 238 239 254 public float idf(Term term, Searcher searcher) throws IOException { 255 return idf(searcher.docFreq(term), searcher.maxDoc()); 256 } 257 258 267 public float idf(Collection terms, Searcher searcher) throws IOException { 268 float idf = 0.0f; 269 Iterator i = terms.iterator(); 270 while (i.hasNext()) { 271 idf += idf((Term)i.next(), searcher); 272 } 273 return idf; 274 } 275 276 289 public abstract float idf(int docFreq, int numDocs); 290 291 303 public abstract float coord(int overlap, int maxOverlap); 304 } 305 | Popular Tags |