TermScorer.java
001 package gate.creole.annic.apache.lucene.search;
002 
003 /**
004  * Copyright 2004 The Apache Software Foundation
005  *
006  * Licensed under the Apache License, Version 2.0 (the "License");
007  * you may not use this file except in compliance with the License.
008  * You may obtain a copy of the License at
009  *
010  *     http://www.apache.org/licenses/LICENSE-2.0
011  *
012  * Unless required by applicable law or agreed to in writing, software
013  * distributed under the License is distributed on an "AS IS" BASIS,
014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015  * See the License for the specific language governing permissions and
016  * limitations under the License.
017  */
018 
019 import java.io.IOException;
020 
021 import gate.creole.annic.apache.lucene.index.TermDocs;
022 import gate.creole.annic.apache.lucene.index.Term;
023 
024 final class TermScorer extends Scorer {
025   private Weight weight;
026 
027   private TermDocs termDocs;
028 
029   private byte[] norms;
030 
031   private float weightValue;
032 
033   private int doc;
034 
035   private Term term;
036 
037   private final int[] docs = new int[32]// buffered doc numbers
038 
039   private final int[] freqs = new int[32]// buffered term freqs
040 
041   private int pointer;
042 
043   private int pointerMax;
044 
045   private static final int SCORE_CACHE_SIZE = 32;
046 
047   private float[] scoreCache = new float[SCORE_CACHE_SIZE];
048 
049   TermScorer(Weight weight, TermDocs td, Similarity similarity, byte[] norms,
050           Term termthrows IOException {
051     super(similarity);
052     this.weight = weight;
053     this.termDocs = td;
054     this.norms = norms;
055     this.weightValue = weight.getValue();
056     this.term = term;
057 
058     for(int i = 0; i < SCORE_CACHE_SIZE; i++)
059       scoreCache[i= getSimilarity().tf(i* weightValue;
060   }
061 
062   public int doc() {
063     return doc;
064   }
065 
066   public boolean next(Searcher searcherthrows IOException {
067     this.searcher = searcher;
068     pointer++;
069     if(pointer >= pointerMax) {
070       pointerMax = termDocs.read(docs, freqs)// refill buffer
071       if(pointerMax != 0) {
072         pointer = 0;
073       }
074       else {
075         termDocs.close()// close stream
076         doc = Integer.MAX_VALUE; // set to sentinel value
077         return false;
078       }
079     }
080     doc = docs[pointer];
081     return true;
082   }
083 
084   public float score(Searcher searcherthrows IOException {
085     this.searcher = searcher;
086     int f = freqs[pointer];
087     float raw = // compute tf(f)*weight
088     f < SCORE_CACHE_SIZE // check cache
089             ? scoreCache[f// cache hit
090             : getSimilarity().tf(f* weightValue; // cache miss
091 
092     /* Niraj */
093     // here before returning this score we will check if it is
094     // greater than 0.0f
095     float score = raw * Similarity.decodeNorm(norms[doc])// normalize
096                                                             // for field
097     if(score > 0.0f) {
098       // here 0 means TermQuery
099       // annic query parser in no case is going to generate any
100       // null value for term type and term text (it is not possible)
101       // if it generates it, it is result of some other type of query
102       // (e.g. specifying
103       // a corpus to search in and therefore should not be set as first
104       // term position
105       if(term.type() != null && this.searcher instanceof IndexSearcher) {
106         // we need to add this into the IndexSercher
107         java.util.ArrayList termInfo = new java.util.ArrayList();
108         termInfo.add(term.text());
109         termInfo.add(term.type());
110         ((IndexSearcher)this.searcher).setFirstTermPositions(0, doc(), termInfo, 1, f);
111       }
112     }
113     return score;
114   }
115 
116   public boolean skipTo(int targetthrows IOException {
117     // first scan in cache
118     for(pointer++; pointer < pointerMax; pointer++) {
119       if(!(target > docs[pointer])) {
120         doc = docs[pointer];
121         return true;
122       }
123     }
124 
125     // not found in cache, seek underlying stream
126     boolean result = termDocs.skipTo(target);
127     if(result) {
128       pointerMax = 1;
129       pointer = 0;
130       docs[pointer= doc = termDocs.doc();
131       freqs[pointer= termDocs.freq();
132     }
133     else {
134       doc = Integer.MAX_VALUE;
135     }
136     return result;
137   }
138 
139   public Explanation explain(int docthrows IOException {
140     TermQuery query = (TermQuery)weight.getQuery();
141     Explanation tfExplanation = new Explanation();
142     int tf = 0;
143     while(pointer < pointerMax) {
144       if(docs[pointer== doctf = freqs[pointer];
145       pointer++;
146     }
147     if(tf == 0) {
148       while(termDocs.next()) {
149         if(termDocs.doc() == doc) {
150           tf = termDocs.freq();
151         }
152       }
153     }
154     termDocs.close();
155     tfExplanation.setValue(getSimilarity().tf(tf));
156     tfExplanation.setDescription("tf(termFreq(" + query.getTerm() ")=" + tf
157             ")");
158 
159     return tfExplanation;
160   }
161 
162   public String toString() {
163     return "scorer(" + weight + ")";
164   }
165 
166 }