1 package org.apache.lucene.search.spans; 2 3 18 19 import java.io.IOException ; 20 21 import org.apache.lucene.search.Weight; 22 import org.apache.lucene.search.Scorer; 23 import org.apache.lucene.search.Explanation; 24 import org.apache.lucene.search.Similarity; 25 26 27 class SpanScorer extends Scorer { 28 private Spans spans; 29 private Weight weight; 30 private byte[] norms; 31 private float value; 32 33 private boolean firstTime = true; 34 private boolean more = true; 35 36 private int doc; 37 private float freq; 38 39 SpanScorer(Spans spans, Weight weight, Similarity similarity, byte[] norms) 40 throws IOException { 41 super(similarity); 42 this.spans = spans; 43 this.norms = norms; 44 this.weight = weight; 45 this.value = weight.getValue(); 46 } 47 48 public boolean next() throws IOException { 49 if (firstTime) { 50 more = spans.next(); 51 firstTime = false; 52 } 53 54 if (!more) return false; 55 56 freq = 0.0f; 57 doc = spans.doc(); 58 59 while (more && doc == spans.doc()) { 60 int matchLength = spans.end() - spans.start(); 61 freq += getSimilarity().sloppyFreq(matchLength); 62 more = spans.next(); 63 } 64 65 return more || freq != 0.0f; 66 } 67 68 public int doc() { return doc; } 69 70 public float score() throws IOException { 71 float raw = getSimilarity().tf(freq) * value; return raw * Similarity.decodeNorm(norms[doc]); } 74 75 public boolean skipTo(int target) throws IOException { 76 more = spans.skipTo(target); 77 78 if (!more) return false; 79 80 freq = 0.0f; 81 doc = spans.doc(); 82 83 while (more && spans.doc() == target) { 84 freq += getSimilarity().sloppyFreq(spans.end() - spans.start()); 85 more = spans.next(); 86 } 87 88 return more || freq != 0.0f; 89 } 90 91 public Explanation explain(final int doc) throws IOException { 92 Explanation tfExplanation = new Explanation(); 93 94 skipTo(doc); 95 96 float phraseFreq = (doc() == doc) ? freq : 0.0f; 97 tfExplanation.setValue(getSimilarity().tf(phraseFreq)); 98 tfExplanation.setDescription("tf(phraseFreq=" + phraseFreq + ")"); 99 100 return tfExplanation; 101 } 102 103 } 104 | Popular Tags |