1 package org.apache.lucene.search; 2 3 18 19 import java.io.IOException ; 20 21 import org.apache.lucene.index.*; 22 23 abstract class PhraseScorer extends Scorer { 24 private Weight weight; 25 protected byte[] norms; 26 protected float value; 27 28 private boolean firstTime = true; 29 private boolean more = true; 30 protected PhraseQueue pq; 31 protected PhrasePositions first, last; 32 33 private float freq; 34 35 36 PhraseScorer(Weight weight, TermPositions[] tps, int[] positions, Similarity similarity, 37 byte[] norms) { 38 super(similarity); 39 this.norms = norms; 40 this.weight = weight; 41 this.value = weight.getValue(); 42 43 for (int i = 0; i < tps.length; i++) { 45 PhrasePositions pp = new PhrasePositions(tps[i], positions[i]); 46 if (last != null) { last.next = pp; 48 } else 49 first = pp; 50 last = pp; 51 } 52 53 pq = new PhraseQueue(tps.length); 55 } 56 57 public int doc() { return first.doc; } 58 59 public boolean next() throws IOException { 60 if (firstTime) { 61 init(); 62 firstTime = false; 63 } else if (more) { 64 more = last.next(); } 66 return doNext(); 67 } 68 69 private boolean doNext() throws IOException { 71 while (more) { 72 while (more && first.doc < last.doc) { more = first.skipTo(last.doc); firstToLast(); } 76 77 if (more) { 78 freq = phraseFreq(); if (freq == 0.0f) more = last.next(); else 83 return true; } 85 } 86 return false; } 88 89 public float score() throws IOException { 90 float raw = getSimilarity().tf(freq) * value; return raw * Similarity.decodeNorm(norms[first.doc]); } 94 95 public boolean skipTo(int target) throws IOException { 96 for (PhrasePositions pp = first; more && pp != null; pp = pp.next) { 97 more = pp.skipTo(target); 98 } 99 if (more) 100 sort(); return doNext(); 102 } 103 104 protected abstract float phraseFreq() throws IOException ; 105 106 private void init() throws IOException { 107 for (PhrasePositions pp = first; more && pp != null; pp = pp.next) 108 more = pp.next(); 109 if(more) 110 sort(); 111 } 112 113 private void sort() { 114 pq.clear(); 115 for (PhrasePositions pp = first; pp != null; pp = pp.next) 116 pq.put(pp); 117 pqToList(); 118 } 119 120 protected final void pqToList() { 121 last = first = null; 122 while (pq.top() != null) { 123 PhrasePositions pp = (PhrasePositions) pq.pop(); 124 if (last != null) { last.next = pp; 126 } else 127 first = pp; 128 last = pp; 129 pp.next = null; 130 } 131 } 132 133 protected final void firstToLast() { 134 last.next = first; last = first; 136 first = first.next; 137 last.next = null; 138 } 139 140 public Explanation explain(final int doc) throws IOException { 141 Explanation tfExplanation = new Explanation(); 142 143 while (next() && doc() < doc) {} 144 145 float phraseFreq = (doc() == doc) ? freq : 0.0f; 146 tfExplanation.setValue(getSimilarity().tf(phraseFreq)); 147 tfExplanation.setDescription("tf(phraseFreq=" + phraseFreq + ")"); 148 149 return tfExplanation; 150 } 151 152 public String toString() { return "scorer(" + weight + ")"; } 153 154 } 155 | Popular Tags |