package lucene4ir.predictor.pre;

import lucene4ir.predictor.PreQPPredictor;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Query;

import java.io.IOException;

/**
 * Created by Harry Scells on 28/8/17.
 */
public class QSQPPredictor extends PreQPPredictor {

    public QSQPPredictor(IndexReader ir) {
        super(ir);
    }

    public String name() {
        return "QueryScope";
    }

    @Override
    public double scoreQuery(String qno, Query q) {
        // Number of documents containing at least one of the query terms.
        double Nq = Double.POSITIVE_INFINITY;
        String[] termTuples = q.toString().split(" ");

        for (String termTuple : termTuples) {
            String[] terms = termTuple.split(":");
            if (terms.length == 2) {
                String term = terms[1];
                try {
                    double df = getDF(term);
                    // What is the smallest possible df?
                    if (df < Nq && df > 0) {
                        Nq = df;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return -Math.log((1 + Nq) / (1 + reader.numDocs()));
    }
}

@@ -143,49 +143,130 @@
 ene.
-search.Query;%0A%0Aimport java.io.IOException
+index.Term;%0Aimport org.apache.lucene.search.*;%0A%0Aimport java.io.IOException;%0Aimport java.util.HashSet;%0Aimport java.util.Set
 ;%0A%0A/
@@ -649,44 +649,101 @@
     
-double Nq = Double.POSITIVE_INFINITY
+Set%3CInteger%3E Nq = new HashSet%3C%3E();%0A        IndexSearcher searcher = new IndexSearcher(reader)
 ;%0A  
@@ -1016,131 +1016,274 @@
     
-double df = getDF(term);%0A                    // What is the smallest possible df?%0A                    if (df %3C Nq && df %3E 0
+// Add the docs that contain this term to the set Nq.%0A                    TermQuery query = new TermQuery(new Term(%22all%22, term));%0A                    TopDocs docs = searcher.search(query, reader.numDocs());%0A                    for (ScoreDoc scoreDoc : docs.scoreDocs
 ) %7B%0A
@@ -1312,13 +1312,26 @@
   Nq
- = df
+.add(scoreDoc.doc)
 ;%0A  
@@ -1429,24 +1429,60 @@
 ackTrace();%0A
+                    System.exit(1);%0A
             
@@ -1543,19 +1543,30 @@
 g((1
+.0
  + Nq
+.size()
 ) / (1
+.0
  + r

Fix bug with Query Scope predictor.