SloppyPhraseScorer.java
01 package gate.creole.annic.apache.lucene.search;
02 
03 /**
04  * Copyright 2004 The Apache Software Foundation
05  *
06  * Licensed under the Apache License, Version 2.0 (the "License");
07  * you may not use this file except in compliance with the License.
08  * You may obtain a copy of the License at
09  *
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 gate.creole.annic.apache.lucene.index.TermPositions;
20 
21 import java.io.IOException;
22 
23 final class SloppyPhraseScorer extends PhraseScorer {
24     private int slop;
25 
26     SloppyPhraseScorer(Weight weight, TermPositions[] tps, Similarity similarity,
27                        int slop, byte[] normsthrows IOException {
28         super(weight, tps, similarity, norms);
29         this.slop = slop;
30     }
31 
32     protected final float phraseFreq() throws IOException {
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 = (PhrasePositionspq.pop();
46             int start = pp.position;
47             int next = ((PhrasePositionspq.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 }