KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > PhraseScorer


1 package org.apache.lucene.search;
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.index.*;
22
23 abstract class PhraseScorer extends Scorer {
24   private Weight weight;
25   protected byte[] norms;
26   protected float value;
27
28   private boolean firstTime = true;
29   private boolean more = true;
30   protected PhraseQueue pq;
31   protected PhrasePositions first, last;
32
33   private float freq;
34
35
36   PhraseScorer(Weight weight, TermPositions[] tps, int[] positions, Similarity similarity,
37                byte[] norms) {
38     super(similarity);
39     this.norms = norms;
40     this.weight = weight;
41     this.value = weight.getValue();
42
43     // convert tps to a list
44
for (int i = 0; i < tps.length; i++) {
45       PhrasePositions pp = new PhrasePositions(tps[i], positions[i]);
46       if (last != null) { // add next to end of list
47
last.next = pp;
48       } else
49         first = pp;
50       last = pp;
51     }
52
53     pq = new PhraseQueue(tps.length); // construct empty pq
54

55   }
56
57   public int doc() { return first.doc; }
58
59   public boolean next() throws IOException JavaDoc {
60     if (firstTime) {
61       init();
62       firstTime = false;
63     } else if (more) {
64       more = last.next(); // trigger further scanning
65
}
66     return doNext();
67   }
68   
69   // next without initial increment
70
private boolean doNext() throws IOException JavaDoc {
71     while (more) {
72       while (more && first.doc < last.doc) { // find doc w/ all the terms
73
more = first.skipTo(last.doc); // skip first upto last
74
firstToLast(); // and move it to the end
75
}
76
77       if (more) {
78         // found a doc with all of the terms
79
freq = phraseFreq(); // check for phrase
80
if (freq == 0.0f) // no match
81
more = last.next(); // trigger further scanning
82
else
83           return true; // found a match
84
}
85     }
86     return false; // no more matches
87
}
88
89   public float score() throws IOException JavaDoc {
90     //System.out.println("scoring " + first.doc);
91
float raw = getSimilarity().tf(freq) * value; // raw score
92
return raw * Similarity.decodeNorm(norms[first.doc]); // normalize
93
}
94
95   public boolean skipTo(int target) throws IOException JavaDoc {
96     for (PhrasePositions pp = first; more && pp != null; pp = pp.next) {
97       more = pp.skipTo(target);
98     }
99     if (more)
100       sort(); // re-sort
101
return doNext();
102   }
103
104   protected abstract float phraseFreq() throws IOException JavaDoc;
105
106   private void init() throws IOException JavaDoc {
107     for (PhrasePositions pp = first; more && pp != null; pp = pp.next)
108       more = pp.next();
109     if(more)
110       sort();
111   }
112   
113   private void sort() {
114     pq.clear();
115     for (PhrasePositions pp = first; pp != null; pp = pp.next)
116       pq.put(pp);
117     pqToList();
118   }
119
120   protected final void pqToList() {
121     last = first = null;
122     while (pq.top() != null) {
123       PhrasePositions pp = (PhrasePositions) pq.pop();
124       if (last != null) { // add next to end of list
125
last.next = pp;
126       } else
127         first = pp;
128       last = pp;
129       pp.next = null;
130     }
131   }
132
133   protected final void firstToLast() {
134     last.next = first; // move first to end of list
135
last = first;
136     first = first.next;
137     last.next = null;
138   }
139
140   public Explanation explain(final int doc) throws IOException JavaDoc {
141     Explanation tfExplanation = new Explanation();
142
143     while (next() && doc() < doc) {}
144
145     float phraseFreq = (doc() == doc) ? freq : 0.0f;
146     tfExplanation.setValue(getSimilarity().tf(phraseFreq));
147     tfExplanation.setDescription("tf(phraseFreq=" + phraseFreq + ")");
148
149     return tfExplanation;
150   }
151
152   public String JavaDoc toString() { return "scorer(" + weight + ")"; }
153
154 }
155
Popular Tags