Apache Lucene open-source search software
reformat spacing
Erik Hatcher798fc0f0ef9a
diff --git a/src/java/org/apache/lucene/search/PhraseScorer.java b/src/java/org/apache/lucene/search/PhraseScorer.javaindex c49131fa760..00ae443b970 100644--- a/src/java/org/apache/lucene/search/PhraseScorer.java+++ b/src/java/org/apache/lucene/search/PhraseScorer.java@@ -60,87 +60,89 @@ import org.apache.lucene.util.*;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;-- 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;++ 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 final void firstToLast() {- last.next = first; // move first to end of list- last = first;- first = first.next;- last.next = null;- }+ 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;+ }+ }- public Explanation explain(final int doc) throws IOException {- Explanation tfExplanation = new Explanation();+ protected final void firstToLast() {+ last.next = first; // move first to end of list+ last = first;+ first = first.next;+ last.next = null;+ }- score(new HitCollector() {- public final void collect(int d, float score) {}- }, doc+1);+ public Explanation explain(final int doc) throws IOException {+ Explanation tfExplanation = new Explanation();- float phraseFreq = (first.doc == doc) ? freq : 0.0f;- tfExplanation.setValue(getSimilarity().tf(phraseFreq));- tfExplanation.setDescription("tf(phraseFreq=" + phraseFreq + ")");+ score(new HitCollector() {+ public final void collect(int d, float score) {+ }+ }, doc + 1);- return tfExplanation;- }+ float phraseFreq = (first.doc == doc) ? freq : 0.0f;+ tfExplanation.setValue(getSimilarity().tf(phraseFreq));+ tfExplanation.setDescription("tf(phraseFreq=" + phraseFreq + ")");++ return tfExplanation;+ }}diff --git a/src/java/org/apache/lucene/search/SloppyPhraseScorer.java b/src/java/org/apache/lucene/search/SloppyPhraseScorer.javaindex 28975ea624a..eea02607c2f 100644--- a/src/java/org/apache/lucene/search/SloppyPhraseScorer.java+++ b/src/java/org/apache/lucene/search/SloppyPhraseScorer.java@@ -54,53 +54,52 @@ package org.apache.lucene.search;* <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;-- SloppyPhraseScorer(Weight weight, TermPositions[] tps, Similarity similarity,- int slop, byte[] norms) throws IOException {- super(weight, tps, similarity, norms);- this.slop = slop;- }+ private int 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;- }- }+ 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+ }- int matchLength = end - start;- if (matchLength <= slop)- freq += getSimilarity().sloppyFreq(matchLength); // score match+ 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;+ }+ }- if (pp.position > end)- end = pp.position;- pq.put(pp); // restore pq- } while (!done);-- return freq;- }+ 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;+ }}