KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.lucene.index.TermPositions;
20
21 import java.io.IOException JavaDoc;
22
23 final class SloppyPhraseScorer extends PhraseScorer {
24     private int slop;
25
26     SloppyPhraseScorer(Weight weight, TermPositions[] tps, int[] positions, Similarity similarity,
27                        int slop, byte[] norms) {
28         super(weight, tps, positions, similarity, norms);
29         this.slop = slop;
30     }
31
32     protected final float phraseFreq() throws IOException JavaDoc {
33         pq.clear();
34         int end = 0;
35         for (PhrasePositions pp = first; pp != null; pp = pp.next) {
36             pp.firstPosition();
37             if (pp.position > end)
38                 end = pp.position;
39             pq.put(pp); // build pq from list
40
}
41
42         float freq = 0.0f;
43         boolean done = false;
44         do {
45             PhrasePositions pp = (PhrasePositions) pq.pop();
46             int start = pp.position;
47             int next = ((PhrasePositions) pq.top()).position;
48             for (int pos = start; pos <= next; pos = pp.position) {
49                 start = pos; // advance pp to min window
50
if (!pp.nextPosition()) {
51                     done = true; // ran out of a term -- done
52
break;
53                 }
54             }
55
56             int matchLength = end - start;
57             if (matchLength <= slop)
58                 freq += getSimilarity().sloppyFreq(matchLength); // score match
59

60             if (pp.position > end)
61                 end = pp.position;
62             pq.put(pp); // restore pq
63
} while (!done);
64
65         return freq;
66     }
67 }
68
Popular Tags