You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucene.apache.org by eh...@apache.org on 2003/09/11 03:25:47 UTC
cvs commit: jakarta-lucene/src/java/org/apache/lucene/search PhraseScorer.java SloppyPhraseScorer.java
ehatcher 2003/09/10 18:25:47
Modified: src/java/org/apache/lucene/search PhraseScorer.java
SloppyPhraseScorer.java
Log:
reformat spacing
Revision Changes Path
1.8 +78 -76 jakarta-lucene/src/java/org/apache/lucene/search/PhraseScorer.java
Index: PhraseScorer.java
===================================================================
RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/search/PhraseScorer.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- PhraseScorer.java 29 Jan 2003 17:18:55 -0000 1.7
+++ PhraseScorer.java 11 Sep 2003 01:25:47 -0000 1.8
@@ -60,87 +60,89 @@
import org.apache.lucene.index.*;
abstract class PhraseScorer extends Scorer {
- private Weight weight;
- protected byte[] norms;
- protected float value;
-
- protected PhraseQueue pq;
- protected PhrasePositions first, last;
-
- private float freq;
-
- PhraseScorer(Weight weight, TermPositions[] tps, Similarity similarity,
- byte[] norms) throws IOException {
- super(similarity);
- this.norms = norms;
- this.weight = weight;
- this.value = weight.getValue();
-
- // use PQ to build a sorted list of PhrasePositions
- pq = new PhraseQueue(tps.length);
- for (int i = 0; i < tps.length; i++)
- pq.put(new PhrasePositions(tps[i], i));
- pqToList();
- }
-
- public final void score(HitCollector results, int end) throws IOException {
- Similarity similarity = getSimilarity();
- while (last.doc < end) { // find doc w/ all the terms
- while (first.doc < last.doc) { // scan forward in first
- do {
- first.next();
- } while (first.doc < last.doc);
- firstToLast();
- if (last.doc >= end)
- return;
- }
-
- // found doc with all terms
- freq = phraseFreq(); // check for phrase
-
- if (freq > 0.0) {
- float score = similarity.tf(freq)*value; // compute score
- score *= Similarity.decodeNorm(norms[first.doc]); // normalize
- results.collect(first.doc, score); // add to results
- }
- last.next(); // resume scanning
+ private Weight weight;
+ protected byte[] norms;
+ protected float value;
+
+ protected PhraseQueue pq;
+ protected PhrasePositions first, last;
+
+ private float freq;
+
+ PhraseScorer(Weight weight, TermPositions[] tps, Similarity similarity,
+ byte[] norms) throws IOException {
+ super(similarity);
+ this.norms = norms;
+ this.weight = weight;
+ this.value = weight.getValue();
+
+ // use PQ to build a sorted list of PhrasePositions
+ pq = new PhraseQueue(tps.length);
+ for (int i = 0; i < tps.length; i++) {
+ pq.put(new PhrasePositions(tps[i], i));
+ }
+ pqToList();
}
- }
- protected abstract float phraseFreq() throws IOException;
+ public final void score(HitCollector results, int end) throws IOException {
+ Similarity similarity = getSimilarity();
+ while (last.doc < end) { // find doc w/ all the terms
+ while (first.doc < last.doc) { // scan forward in first
+ do {
+ first.next();
+ } while (first.doc < last.doc);
+ firstToLast();
+ if (last.doc >= end)
+ return;
+ }
+
+ // found doc with all terms
+ freq = phraseFreq(); // check for phrase
+
+ if (freq > 0.0) {
+ float score = similarity.tf(freq) * value; // compute score
+ score *= Similarity.decodeNorm(norms[first.doc]); // normalize
+ results.collect(first.doc, score); // add to results
+ }
+ last.next(); // resume scanning
+ }
+ }
+
+ protected abstract float phraseFreq() throws IOException;
+
+ protected final void pqToList() {
+ last = first = null;
+ while (pq.top() != null) {
+ PhrasePositions pp = (PhrasePositions) pq.pop();
+ if (last != null) { // add next to end of list
+ last.next = pp;
+ } else
+ first = pp;
+ last = pp;
+ pp.next = null;
+ }
+ }
- protected final void pqToList() {
- last = first = null;
- while (pq.top() != null) {
- PhrasePositions pp = (PhrasePositions)pq.pop();
- if (last != null) { // add next to end of list
- last.next = pp;
- } else
- first = pp;
- last = pp;
- pp.next = null;
+ protected final void firstToLast() {
+ last.next = first; // move first to end of list
+ last = first;
+ first = first.next;
+ last.next = null;
}
- }
- protected final void firstToLast() {
- last.next = first; // move first to end of list
- last = first;
- first = first.next;
- last.next = null;
- }
-
- public Explanation explain(final int doc) throws IOException {
- Explanation tfExplanation = new Explanation();
-
- score(new HitCollector() {
- public final void collect(int d, float score) {}
- }, doc+1);
-
- float phraseFreq = (first.doc == doc) ? freq : 0.0f;
- tfExplanation.setValue(getSimilarity().tf(phraseFreq));
- tfExplanation.setDescription("tf(phraseFreq=" + phraseFreq + ")");
+ public Explanation explain(final int doc) throws IOException {
+ Explanation tfExplanation = new Explanation();
+
+ score(new HitCollector() {
+ public final void collect(int d, float score) {
+ }
+ }, doc + 1);
+
+ float phraseFreq = (first.doc == doc) ? freq : 0.0f;
+ tfExplanation.setValue(getSimilarity().tf(phraseFreq));
+ tfExplanation.setDescription("tf(phraseFreq=" + phraseFreq + ")");
- return tfExplanation;
- }
+ return tfExplanation;
+ }
}
1.5 +42 -43 jakarta-lucene/src/java/org/apache/lucene/search/SloppyPhraseScorer.java
Index: SloppyPhraseScorer.java
===================================================================
RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/search/SloppyPhraseScorer.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- SloppyPhraseScorer.java 29 Jan 2003 17:18:55 -0000 1.4
+++ SloppyPhraseScorer.java 11 Sep 2003 01:25:47 -0000 1.5
@@ -54,53 +54,52 @@
* <http://www.apache.org/>.
*/
-import java.io.IOException;
+import org.apache.lucene.index.TermPositions;
-import org.apache.lucene.util.*;
-import org.apache.lucene.index.*;
+import java.io.IOException;
final class SloppyPhraseScorer extends PhraseScorer {
- private int slop;
+ private int slop;
- SloppyPhraseScorer(Weight weight, TermPositions[] tps, Similarity similarity,
- int slop, byte[] norms) throws IOException {
- super(weight, tps, similarity, norms);
- this.slop = slop;
- }
-
- protected final float phraseFreq() throws IOException {
- pq.clear();
- int end = 0;
- for (PhrasePositions pp = first; pp != null; pp = pp.next) {
- pp.firstPosition();
- if (pp.position > end)
- end = pp.position;
- pq.put(pp); // build pq from list
+ SloppyPhraseScorer(Weight weight, TermPositions[] tps, Similarity similarity,
+ int slop, byte[] norms) throws IOException {
+ super(weight, tps, similarity, norms);
+ this.slop = slop;
}
- float freq = 0.0f;
- boolean done = false;
- do {
- PhrasePositions pp = (PhrasePositions)pq.pop();
- int start = pp.position;
- int next = ((PhrasePositions)pq.top()).position;
- for (int pos = start; pos <= next; pos = pp.position) {
- start = pos; // advance pp to min window
- if (!pp.nextPosition()) {
- done = true; // ran out of a term -- done
- break;
- }
- }
-
- int matchLength = end - start;
- if (matchLength <= slop)
- freq += getSimilarity().sloppyFreq(matchLength); // score match
-
- if (pp.position > end)
- end = pp.position;
- pq.put(pp); // restore pq
- } while (!done);
-
- return freq;
- }
+ protected final float phraseFreq() throws IOException {
+ pq.clear();
+ int end = 0;
+ for (PhrasePositions pp = first; pp != null; pp = pp.next) {
+ pp.firstPosition();
+ if (pp.position > end)
+ end = pp.position;
+ pq.put(pp); // build pq from list
+ }
+
+ float freq = 0.0f;
+ boolean done = false;
+ do {
+ PhrasePositions pp = (PhrasePositions) pq.pop();
+ int start = pp.position;
+ int next = ((PhrasePositions) pq.top()).position;
+ for (int pos = start; pos <= next; pos = pp.position) {
+ start = pos; // advance pp to min window
+ if (!pp.nextPosition()) {
+ done = true; // ran out of a term -- done
+ break;
+ }
+ }
+
+ int matchLength = end - start;
+ if (matchLength <= slop)
+ freq += getSimilarity().sloppyFreq(matchLength); // score match
+
+ if (pp.position > end)
+ end = pp.position;
+ pq.put(pp); // restore pq
+ } while (!done);
+
+ return freq;
+ }
}