ExactPhraseScorer.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 java.io.IOException;
20 import gate.creole.annic.apache.lucene.index.*;
21 
22 final class ExactPhraseScorer extends PhraseScorer {
23 
24   int patternSize = 0;
25 
26   ExactPhraseScorer(Weight weight, TermPositions[] tps, Similarity similarity,
27           byte[] normsthrows IOException {
28     super(weight, tps, similarity, norms);
29   }
30 
31   ExactPhraseScorer(Weight weight, TermPositions[] tps,
32           java.util.Vector positions, int totalTerms, Similarity similarity,
33           byte[] norms, Searcher searcherthrows IOException {
34     super(weight, tps, positions, similarity, norms, searcher);
35     patternSize = totalTerms;
36   }
37 
38   protected final float phraseFreq() throws IOException {
39     // sort list with pq
40     for(PhrasePositions pp = first; pp != null; pp = pp.next) {
41       pp.firstPosition();
42       pq.put(pp)// build pq from list
43     }
44     pqToList()// rebuild list from pq
45 
46     int freq = 0;
47 
48     /* Niraj */
49     java.util.ArrayList firstPositions = new java.util.ArrayList();
50     /* End */
51 
52     do // find position w/ all terms
53       while(first.position < last.position) { // scan forward in first
54         do {
55           if(!first.nextPosition()) {
56             /* Niraj */
57             // here 1 represents the ExactPhraseQuery
58             if(searcher instanceof IndexSearcher) {
59               ((IndexSearcher)searcher).setFirstTermPositions(1, first.doc,
60                       firstPositions, patternSize, freq);
61             }
62             /* End */
63             return (float)freq;
64           }
65           
66         while(first.position < last.position);
67         firstToLast();
68       }
69       firstPositions.add(new Integer(first.position));
70       freq++; // all equal: a match
71     while(last.nextPosition());
72 
73     /* Niraj */
74     if(searcher instanceof IndexSearcher)
75     ((IndexSearcher)searcher).setFirstTermPositions(1, first.doc,
76             firstPositions, patternSize, freq);
77     /* End */
78     return (float)freq;
79   }
80 }