Home » lucene-3.0.1-src » org.apache » lucene » search » spans » [javadoc | source]

    1   package org.apache.lucene.search.spans;
    2   
    3   /**
    4    * Licensed to the Apache Software Foundation (ASF) under one or more
    5    * contributor license agreements.  See the NOTICE file distributed with
    6    * this work for additional information regarding copyright ownership.
    7    * The ASF licenses this file to You under the Apache License, Version 2.0
    8    * (the "License"); you may not use this file except in compliance with
    9    * the License.  You may obtain a copy of the License at
   10    *
   11    *     http://www.apache.org/licenses/LICENSE-2.0
   12    *
   13    * Unless required by applicable law or agreed to in writing, software
   14    * distributed under the License is distributed on an "AS IS" BASIS,
   15    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   16    * See the License for the specific language governing permissions and
   17    * limitations under the License.
   18    */
   19   
   20   import java.io.IOException;
   21   
   22   import org.apache.lucene.search.Explanation;
   23   import org.apache.lucene.search.Weight;
   24   import org.apache.lucene.search.Scorer;
   25   import org.apache.lucene.search.Similarity;
   26   
   27   /**
   28    * Public for extension only.
   29    */
   30   public class SpanScorer extends Scorer {
   31     protected Spans spans;
   32     protected Weight weight;
   33     protected byte[] norms;
   34     protected float value;
   35   
   36     protected boolean more = true;
   37   
   38     protected int doc;
   39     protected float freq;
   40   
   41     protected SpanScorer(Spans spans, Weight weight, Similarity similarity, byte[] norms)
   42     throws IOException {
   43       super(similarity);
   44       this.spans = spans;
   45       this.norms = norms;
   46       this.weight = weight;
   47       this.value = weight.getValue();
   48       if (this.spans.next()) {
   49         doc = -1;
   50       } else {
   51         doc = NO_MORE_DOCS;
   52         more = false;
   53       }
   54     }
   55   
   56     @Override
   57     public int nextDoc() throws IOException {
   58       if (!setFreqCurrentDoc()) {
   59         doc = NO_MORE_DOCS;
   60       }
   61       return doc;
   62     }
   63   
   64     @Override
   65     public int advance(int target) throws IOException {
   66       if (!more) {
   67         return doc = NO_MORE_DOCS;
   68       }
   69       if (spans.doc() < target) { // setFreqCurrentDoc() leaves spans.doc() ahead
   70         more = spans.skipTo(target);
   71       }
   72       if (!setFreqCurrentDoc()) {
   73         doc = NO_MORE_DOCS;
   74       }
   75       return doc;
   76     }
   77     
   78     protected boolean setFreqCurrentDoc() throws IOException {
   79       if (!more) {
   80         return false;
   81       }
   82       doc = spans.doc();
   83       freq = 0.0f;
   84       do {
   85         int matchLength = spans.end() - spans.start();
   86         freq += getSimilarity().sloppyFreq(matchLength);
   87         more = spans.next();
   88       } while (more && (doc == spans.doc()));
   89       return true;
   90     }
   91   
   92     @Override
   93     public int docID() { return doc; }
   94   
   95     @Override
   96     public float score() throws IOException {
   97       float raw = getSimilarity().tf(freq) * value; // raw score
   98       return norms == null? raw : raw * Similarity.decodeNorm(norms[doc]); // normalize
   99     }
  100   
  101     /** This method is no longer an official member of {@link Scorer},
  102      * but it is needed by SpanWeight to build an explanation. */
  103     protected Explanation explain(final int doc) throws IOException {
  104       Explanation tfExplanation = new Explanation();
  105   
  106       int expDoc = advance(doc);
  107   
  108       float phraseFreq = (expDoc == doc) ? freq : 0.0f;
  109       tfExplanation.setValue(getSimilarity().tf(phraseFreq));
  110       tfExplanation.setDescription("tf(phraseFreq=" + phraseFreq + ")");
  111   
  112       return tfExplanation;
  113     }
  114   
  115   }

Home » lucene-3.0.1-src » org.apache » lucene » search » spans » [javadoc | source]