mirror of https://github.com/apache/lucene.git
reformat spacing
git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150016 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
b058f63c30
commit
798fc0f0ef
|
@ -60,87 +60,89 @@ import org.apache.lucene.util.*;
|
||||||
import org.apache.lucene.index.*;
|
import org.apache.lucene.index.*;
|
||||||
|
|
||||||
abstract class PhraseScorer extends Scorer {
|
abstract class PhraseScorer extends Scorer {
|
||||||
private Weight weight;
|
private Weight weight;
|
||||||
protected byte[] norms;
|
protected byte[] norms;
|
||||||
protected float value;
|
protected float value;
|
||||||
|
|
||||||
protected PhraseQueue pq;
|
protected PhraseQueue pq;
|
||||||
protected PhrasePositions first, last;
|
protected PhrasePositions first, last;
|
||||||
|
|
||||||
private float freq;
|
private float freq;
|
||||||
|
|
||||||
PhraseScorer(Weight weight, TermPositions[] tps, Similarity similarity,
|
PhraseScorer(Weight weight, TermPositions[] tps, Similarity similarity,
|
||||||
byte[] norms) throws IOException {
|
byte[] norms) throws IOException {
|
||||||
super(similarity);
|
super(similarity);
|
||||||
this.norms = norms;
|
this.norms = norms;
|
||||||
this.weight = weight;
|
this.weight = weight;
|
||||||
this.value = weight.getValue();
|
this.value = weight.getValue();
|
||||||
|
|
||||||
// use PQ to build a sorted list of PhrasePositions
|
// use PQ to build a sorted list of PhrasePositions
|
||||||
pq = new PhraseQueue(tps.length);
|
pq = new PhraseQueue(tps.length);
|
||||||
for (int i = 0; i < tps.length; i++)
|
for (int i = 0; i < tps.length; i++) {
|
||||||
pq.put(new PhrasePositions(tps[i], i));
|
pq.put(new PhrasePositions(tps[i], i));
|
||||||
pqToList();
|
}
|
||||||
}
|
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
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
||||||
|
}
|
||||||
|
|
||||||
protected final void pqToList() {
|
// found doc with all terms
|
||||||
last = first = null;
|
freq = phraseFreq(); // check for phrase
|
||||||
while (pq.top() != null) {
|
|
||||||
PhrasePositions pp = (PhrasePositions)pq.pop();
|
if (freq > 0.0) {
|
||||||
if (last != null) { // add next to end of list
|
float score = similarity.tf(freq) * value; // compute score
|
||||||
last.next = pp;
|
score *= Similarity.decodeNorm(norms[first.doc]); // normalize
|
||||||
} else
|
results.collect(first.doc, score); // add to results
|
||||||
first = pp;
|
}
|
||||||
last = pp;
|
last.next(); // resume scanning
|
||||||
pp.next = null;
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
protected final void firstToLast() {
|
protected abstract float phraseFreq() throws IOException;
|
||||||
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 {
|
protected final void pqToList() {
|
||||||
Explanation tfExplanation = new Explanation();
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
score(new HitCollector() {
|
protected final void firstToLast() {
|
||||||
public final void collect(int d, float score) {}
|
last.next = first; // move first to end of list
|
||||||
}, doc+1);
|
last = first;
|
||||||
|
first = first.next;
|
||||||
|
last.next = null;
|
||||||
|
}
|
||||||
|
|
||||||
float phraseFreq = (first.doc == doc) ? freq : 0.0f;
|
public Explanation explain(final int doc) throws IOException {
|
||||||
tfExplanation.setValue(getSimilarity().tf(phraseFreq));
|
Explanation tfExplanation = new Explanation();
|
||||||
tfExplanation.setDescription("tf(phraseFreq=" + phraseFreq + ")");
|
|
||||||
|
|
||||||
return tfExplanation;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,53 +54,52 @@ package org.apache.lucene.search;
|
||||||
* <http://www.apache.org/>.
|
* <http://www.apache.org/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import org.apache.lucene.index.TermPositions;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.apache.lucene.util.*;
|
|
||||||
import org.apache.lucene.index.*;
|
|
||||||
|
|
||||||
final class SloppyPhraseScorer extends PhraseScorer {
|
final class SloppyPhraseScorer extends PhraseScorer {
|
||||||
private int slop;
|
private int slop;
|
||||||
|
|
||||||
SloppyPhraseScorer(Weight weight, TermPositions[] tps, Similarity similarity,
|
SloppyPhraseScorer(Weight weight, TermPositions[] tps, Similarity similarity,
|
||||||
int slop, byte[] norms) throws IOException {
|
int slop, byte[] norms) throws IOException {
|
||||||
super(weight, tps, similarity, norms);
|
super(weight, tps, similarity, norms);
|
||||||
this.slop = slop;
|
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float freq = 0.0f;
|
protected final float phraseFreq() throws IOException {
|
||||||
boolean done = false;
|
pq.clear();
|
||||||
do {
|
int end = 0;
|
||||||
PhrasePositions pp = (PhrasePositions)pq.pop();
|
for (PhrasePositions pp = first; pp != null; pp = pp.next) {
|
||||||
int start = pp.position;
|
pp.firstPosition();
|
||||||
int next = ((PhrasePositions)pq.top()).position;
|
if (pp.position > end)
|
||||||
for (int pos = start; pos <= next; pos = pp.position) {
|
end = pp.position;
|
||||||
start = pos; // advance pp to min window
|
pq.put(pp); // build pq from list
|
||||||
if (!pp.nextPosition()) {
|
}
|
||||||
done = true; // ran out of a term -- done
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int matchLength = end - start;
|
float freq = 0.0f;
|
||||||
if (matchLength <= slop)
|
boolean done = false;
|
||||||
freq += getSimilarity().sloppyFreq(matchLength); // score match
|
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)
|
int matchLength = end - start;
|
||||||
end = pp.position;
|
if (matchLength <= slop)
|
||||||
pq.put(pp); // restore pq
|
freq += getSimilarity().sloppyFreq(matchLength); // score match
|
||||||
} while (!done);
|
|
||||||
|
if (pp.position > end)
|
||||||
return freq;
|
end = pp.position;
|
||||||
}
|
pq.put(pp); // restore pq
|
||||||
|
} while (!done);
|
||||||
|
|
||||||
|
return freq;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue