BindingScorer.java

  1. /*
  2.  *  BindingScorer.java
  3.  *
  4.  *  Copyright (c) 2007-2011, The University of Sheffield.
  5.  *
  6.  *  This file is part of GATE Mímir (see http://gate.ac.uk/family/mimir.html),
  7.  *  and is free software, licenced under the GNU Lesser General Public License,
  8.  *  Version 3, June 2007 (also included with this distribution as file
  9.  *  LICENCE-LGPL3.html).
  10.  *
  11.  *  Valentin Tablan, 14 September 2011
  12.  *
  13.  *  $Id: BindingScorer.java 16667 2013-04-29 16:35:35Z valyt $
  14.  */
  15. package gate.mimir.search.score;

  16. import gate.mimir.search.query.Binding;
  17. import gate.mimir.search.query.QueryExecutor;
  18. import it.unimi.di.big.mg4j.index.Index;
  19. import it.unimi.di.big.mg4j.search.DocumentIterator;
  20. import it.unimi.di.big.mg4j.search.score.AbstractWeightedScorer;

  21. import java.io.IOException;

  22. public class BindingScorer  extends AbstractWeightedScorer implements MimirScorer {
  23.   public BindingScorer() {
  24.     this(16, 0.9);
  25.   }
  26.  
  27.  
  28.   public BindingScorer(int h, double alpha) {
  29.     super();
  30.     this.h = h;
  31.     this.alpha = alpha;
  32.   }

  33.   @Override
  34.   public double score(Index index) throws IOException {
  35.     return score();
  36.   }
  37.  
  38.   @Override
  39.   public double score() throws IOException {
  40.     double score= 0.0;
  41.     Binding aHit = nextHit();
  42.     while(aHit != null) {
  43.       int length = aHit.getLength();
  44.       score += length < h ? 1 : Math.pow((double)h / length,  alpha);
  45.       aHit = nextHit();
  46.     }
  47.     return score;
  48.   }
  49.  
  50.   @Override
  51.   public boolean usesIntervals() {
  52.     return true;
  53.   }

  54.   @Override
  55.   public BindingScorer copy() {
  56.     return new BindingScorer();
  57.   }

  58.   @Override
  59.   public void wrap(DocumentIterator documentIterator) throws IOException {
  60.     super.wrap(documentIterator);
  61.     this.underlyingExecutor = (QueryExecutor)documentIterator;
  62.   }

  63.   protected QueryExecutor underlyingExecutor;
  64.  
  65.   protected int h;
  66.  
  67.   protected double alpha;

  68.   public long nextDocument(long greaterThan) throws IOException {
  69.     return underlyingExecutor.nextDocument(greaterThan);
  70.   }

  71.   public Binding nextHit() throws IOException {
  72.     return underlyingExecutor.nextHit();
  73.   }


  74. }