pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / search / spans / SpanScorer.java
1 package org.apache.lucene.search.spans;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.IOException;
21
22 import org.apache.lucene.search.Explanation;
23 import org.apache.lucene.search.Weight;
24 import org.apache.lucene.search.Scorer;
25 import org.apache.lucene.search.Similarity;
26
27 /**
28  * Public for extension only.
29  */
30 public class SpanScorer extends Scorer {
31   protected Spans spans;
32   protected byte[] norms;
33   protected float value;
34
35   protected boolean more = true;
36
37   protected int doc;
38   protected float freq;
39
40   protected SpanScorer(Spans spans, Weight weight, Similarity similarity, byte[] norms)
41   throws IOException {
42     super(similarity, weight);
43     this.spans = spans;
44     this.norms = norms;
45     this.value = weight.getValue();
46     if (this.spans.next()) {
47       doc = -1;
48     } else {
49       doc = NO_MORE_DOCS;
50       more = false;
51     }
52   }
53
54   @Override
55   public int nextDoc() throws IOException {
56     if (!setFreqCurrentDoc()) {
57       doc = NO_MORE_DOCS;
58     }
59     return doc;
60   }
61
62   @Override
63   public int advance(int target) throws IOException {
64     if (!more) {
65       return doc = NO_MORE_DOCS;
66     }
67     if (spans.doc() < target) { // setFreqCurrentDoc() leaves spans.doc() ahead
68       more = spans.skipTo(target);
69     }
70     if (!setFreqCurrentDoc()) {
71       doc = NO_MORE_DOCS;
72     }
73     return doc;
74   }
75   
76   protected boolean setFreqCurrentDoc() throws IOException {
77     if (!more) {
78       return false;
79     }
80     doc = spans.doc();
81     freq = 0.0f;
82     do {
83       int matchLength = spans.end() - spans.start();
84       freq += getSimilarity().sloppyFreq(matchLength);
85       more = spans.next();
86     } while (more && (doc == spans.doc()));
87     return true;
88   }
89
90   @Override
91   public int docID() { return doc; }
92
93   @Override
94   public float score() throws IOException {
95     float raw = getSimilarity().tf(freq) * value; // raw score
96     return norms == null? raw : raw * getSimilarity().decodeNormValue(norms[doc]); // normalize
97   }
98   
99   @Override
100   public float freq() throws IOException {
101     return freq;
102   }
103
104   /** This method is no longer an official member of {@link Scorer},
105    * but it is needed by SpanWeight to build an explanation. */
106   protected Explanation explain(final int doc) throws IOException {
107     Explanation tfExplanation = new Explanation();
108
109     int expDoc = advance(doc);
110
111     float phraseFreq = (expDoc == doc) ? freq : 0.0f;
112     tfExplanation.setValue(getSimilarity().tf(phraseFreq));
113     tfExplanation.setDescription("tf(phraseFreq=" + phraseFreq + ")");
114
115     return tfExplanation;
116   }
117
118 }