KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > spans > SpanScorer


1 package org.apache.lucene.search.spans;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.IOException JavaDoc;
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
71     float raw = getSimilarity().tf(freq) * value; // raw score
72
return raw * Similarity.decodeNorm(norms[doc]); // normalize
73
}
74
75   public boolean skipTo(int target) throws IOException JavaDoc {
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 JavaDoc {
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