You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by rm...@apache.org on 2011/09/09 17:54:30 UTC
svn commit: r1167257 - in /lucene/dev/branches/flexscoring/lucene/src:
java/org/apache/lucene/search/similarities/BM25Similarity.java
test-framework/org/apache/lucene/search/CheckHits.java
Author: rmuir
Date: Fri Sep 9 15:54:30 2011
New Revision: 1167257
URL: http://svn.apache.org/viewvc?rev=1167257&view=rev
Log:
add detailed explanations for bm25
Modified:
lucene/dev/branches/flexscoring/lucene/src/java/org/apache/lucene/search/similarities/BM25Similarity.java
lucene/dev/branches/flexscoring/lucene/src/test-framework/org/apache/lucene/search/CheckHits.java
Modified: lucene/dev/branches/flexscoring/lucene/src/java/org/apache/lucene/search/similarities/BM25Similarity.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/flexscoring/lucene/src/java/org/apache/lucene/search/similarities/BM25Similarity.java?rev=1167257&r1=1167256&r2=1167257&view=diff
==============================================================================
--- lucene/dev/branches/flexscoring/lucene/src/java/org/apache/lucene/search/similarities/BM25Similarity.java (original)
+++ lucene/dev/branches/flexscoring/lucene/src/java/org/apache/lucene/search/similarities/BM25Similarity.java Fri Sep 9 15:54:30 2011
@@ -23,6 +23,7 @@ import org.apache.lucene.index.FieldInve
import org.apache.lucene.index.MultiFields;
import org.apache.lucene.index.IndexReader.AtomicReaderContext;
import org.apache.lucene.index.Terms;
+import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.SmallFloat;
@@ -129,14 +130,31 @@ public class BM25Similarity extends Simi
return encodeNormValue(state.getBoost(), numTerms);
}
- @Override
- public final Stats computeStats(IndexSearcher searcher, String fieldName, float queryBoost, TermContext... termStats) throws IOException {
- float value = 0.0f;
+ public Explanation idfExplain(TermContext stats, final IndexSearcher searcher) throws IOException {
+ final int df = stats.docFreq();
final int max = searcher.maxDoc();
-
- for (final TermContext stat : termStats ) {
- value += idf(stat.docFreq(), max);
+ final float idf = idf(df, max);
+ return new Explanation(idf, "idf(docFreq=" + df + ", maxDocs=" + max + ")");
+ }
+
+ public Explanation idfExplain(final TermContext stats[], IndexSearcher searcher) throws IOException {
+ final int max = searcher.maxDoc();
+ float idf = 0.0f;
+ final Explanation exp = new Explanation();
+ exp.setDescription("idf(), sum of:");
+ for (final TermContext stat : stats ) {
+ final int df = stat.docFreq();
+ final float termIdf = idf(df, max);
+ exp.addDetail(new Explanation(termIdf, "idf(docFreq=" + df + ", maxDocs=" + max + ")"));
+ idf += termIdf;
}
+ exp.setValue(idf);
+ return exp;
+ }
+
+ @Override
+ public final Stats computeStats(IndexSearcher searcher, String fieldName, float queryBoost, TermContext... termStats) throws IOException {
+ Explanation idf = termStats.length == 1 ? idfExplain(termStats[0], searcher) : idfExplain(termStats, searcher);
float avgdl = avgFieldLength(searcher, fieldName);
@@ -145,7 +163,7 @@ public class BM25Similarity extends Simi
for (int i = 0; i < cache.length; i++) {
cache[i] = k1 * ((1 - b) + b * decodeNormValue((byte)i) / avgdl);
}
- return new BM25Stats(value, queryBoost, avgdl, cache);
+ return new BM25Stats(idf, queryBoost, avgdl, cache);
}
@Override
@@ -162,12 +180,14 @@ public class BM25Similarity extends Simi
}
private class ExactBM25DocScorer extends ExactDocScorer {
+ private final BM25Stats stats;
private final float weightValue;
private final byte[] norms;
private final float[] cache;
ExactBM25DocScorer(BM25Stats stats, byte norms[]) {
assert norms != null;
+ this.stats = stats;
this.weightValue = stats.weight * (k1 + 1); // boost * idf * (k1 + 1)
this.cache = stats.cache;
this.norms = norms;
@@ -177,15 +197,22 @@ public class BM25Similarity extends Simi
public float score(int doc, int freq) {
return weightValue * freq / (freq + cache[norms[doc] & 0xFF]);
}
+
+ @Override
+ public Explanation explain(int doc, Explanation freq) {
+ return explainScore(doc, freq, stats, norms);
+ }
}
/** there are no norms, we act as if b=0 */
private class ExactBM25DocScorerNoNorms extends ExactDocScorer {
+ private final BM25Stats stats;
private final float weightValue;
private static final int SCORE_CACHE_SIZE = 32;
private float[] scoreCache = new float[SCORE_CACHE_SIZE];
ExactBM25DocScorerNoNorms(BM25Stats stats) {
+ this.stats = stats;
this.weightValue = stats.weight * (k1 + 1); // boost * idf * (k1 + 1)
for (int i = 0; i < SCORE_CACHE_SIZE; i++)
scoreCache[i] = weightValue * i / (i + k1);
@@ -198,14 +225,21 @@ public class BM25Similarity extends Simi
? scoreCache[freq] // cache hit
: weightValue * freq / (freq + k1); // cache miss
}
+
+ @Override
+ public Explanation explain(int doc, Explanation freq) {
+ return explainScore(doc, freq, stats, null);
+ }
}
private class SloppyBM25DocScorer extends SloppyDocScorer {
+ private final BM25Stats stats;
private final float weightValue; // boost * idf * (k1 + 1)
private final byte[] norms;
private final float[] cache;
SloppyBM25DocScorer(BM25Stats stats, byte norms[]) {
+ this.stats = stats;
this.weightValue = stats.weight * (k1 + 1);
this.cache = stats.cache;
this.norms = norms;
@@ -217,6 +251,11 @@ public class BM25Similarity extends Simi
float norm = norms == null ? k1 : cache[norms[doc] & 0xFF];
return weightValue * freq / (freq + norm);
}
+
+ @Override
+ public Explanation explain(int doc, Explanation freq) {
+ return explainScore(doc, freq, stats, norms);
+ }
@Override
public float computeSlopFactor(int distance) {
@@ -232,7 +271,7 @@ public class BM25Similarity extends Simi
/** Collection statistics for the BM25 model. */
private static class BM25Stats extends Stats {
/** BM25's idf */
- private final float idf;
+ private final Explanation idf;
/** The average document length. */
private final float avgdl;
/** query's inner boost */
@@ -242,7 +281,7 @@ public class BM25Similarity extends Simi
/** precomputed norm[256] with k1 * ((1 - b) + b * dl / avgdl) */
private final float cache[];
- BM25Stats(float idf, float queryBoost, float avgdl, float cache[]) {
+ BM25Stats(Explanation idf, float queryBoost, float avgdl, float cache[]) {
this.idf = idf;
this.queryBoost = queryBoost;
this.avgdl = avgdl;
@@ -252,16 +291,45 @@ public class BM25Similarity extends Simi
@Override
public float getValueForNormalization() {
// we return a TF-IDF like normalization to be nice, but we don't actually normalize ourselves.
- final float queryWeight = idf * queryBoost;
+ final float queryWeight = idf.getValue() * queryBoost;
return queryWeight * queryWeight;
}
@Override
public void normalize(float queryNorm, float topLevelBoost) {
// we don't normalize with queryNorm at all, we just capture the top-level boost
- this.weight = idf * queryBoost * topLevelBoost;
+ this.weight = idf.getValue() * queryBoost * topLevelBoost;
}
}
+
+ private Explanation explainScore(int doc, Explanation freq, BM25Stats stats, byte[] norms) {
+ Explanation result = new Explanation();
+ result.setDescription("score(doc="+doc+",freq="+freq+"), product of:");
+
+ Explanation boostExpl = new Explanation(stats.queryBoost, "boost");
+ if (stats.queryBoost != 1.0f)
+ result.addDetail(boostExpl);
+
+ result.addDetail(stats.idf);
+
+ Explanation tfNormExpl = new Explanation();
+ tfNormExpl.setDescription("tfNorm, computed from:");
+ tfNormExpl.addDetail(freq);
+ tfNormExpl.addDetail(new Explanation(k1, "parameter k1"));
+ if (norms == null) {
+ tfNormExpl.addDetail(new Explanation(0, "parameter b (norms omitted for field)"));
+ tfNormExpl.setValue((freq.getValue() * (k1 + 1)) / (freq.getValue() + k1));
+ } else {
+ float doclen = decodeNormValue(norms[doc]);
+ tfNormExpl.addDetail(new Explanation(b, "parameter b"));
+ tfNormExpl.addDetail(new Explanation(stats.avgdl, "avgFieldLength"));
+ tfNormExpl.addDetail(new Explanation(doclen, "fieldLength"));
+ tfNormExpl.setValue((freq.getValue() * (k1 + 1)) / (freq.getValue() + k1 * (1 - b + b * doclen/stats.avgdl)));
+ }
+ result.addDetail(tfNormExpl);
+ result.setValue(boostExpl.getValue() * stats.idf.getValue() * tfNormExpl.getValue());
+ return result;
+ }
@Override
public String toString() {
Modified: lucene/dev/branches/flexscoring/lucene/src/test-framework/org/apache/lucene/search/CheckHits.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/flexscoring/lucene/src/test-framework/org/apache/lucene/search/CheckHits.java?rev=1167257&r1=1167256&r2=1167257&view=diff
==============================================================================
--- lucene/dev/branches/flexscoring/lucene/src/test-framework/org/apache/lucene/search/CheckHits.java (original)
+++ lucene/dev/branches/flexscoring/lucene/src/test-framework/org/apache/lucene/search/CheckHits.java Fri Sep 9 15:54:30 2011
@@ -36,7 +36,7 @@ public class CheckHits {
* different order of operations from the actual scoring method ...
* this allows for a small amount of variation
*/
- public static float EXPLAIN_SCORE_TOLERANCE_DELTA = 0.002f;
+ public static float EXPLAIN_SCORE_TOLERANCE_DELTA = 0.02f;
/**
* Tests that all documents up to maxDoc which are *not* in the