/**
   * Prints the results
   *
   * @param pw PrintWriter the file to write the results to.
   * @param q SearchRequest the search request to get results from.
   */
  public void printResults(PrintWriter pw, SearchRequest q) throws IOException {
    ResultSet set = q.getResultSet();
    int[] docids = set.getDocids();
    double[] scores = set.getScores();
    int minimum = RESULTS_LENGTH;
    // if the minimum number of documents is more than the
    // number of documents in the results, aw.length, then
    // set minimum = aw.length
    if (minimum > set.getResultSize()) minimum = set.getResultSize();
    if (verbose)
      if (set.getResultSize() > 0)
        pw.write("\n\tDisplaying 1-" + set.getResultSize() + " results\n");
      else pw.write("\n\tNo results\n");
    if (set.getResultSize() == 0) return;

    int metaKeyId = 0;
    final int metaKeyCount = metaKeys.length;
    String[][] docNames = new String[metaKeyCount][];
    for (String metaIndexDocumentKey : metaKeys) {
      if (set.hasMetaItems(metaIndexDocumentKey)) {
        docNames[metaKeyId] = set.getMetaItems(metaIndexDocumentKey);
      } else {
        final MetaIndex metaIndex = index.getMetaIndex();
        docNames[metaKeyId] = metaIndex.getItems(metaIndexDocumentKey, docids);
      }
      metaKeyId++;
    }

    StringBuilder sbuffer = new StringBuilder();
    // the results are ordered in asceding order
    // with respect to the score. For example, the
    // document with the highest score has score
    // score[scores.length-1] and its docid is
    // docid[docids.length-1].
    int start = 0;
    int end = minimum;
    for (int i = start; i < end; i++) {
      if (scores[i] <= 0d) continue;
      sbuffer.append(i);
      sbuffer.append(" ");
      for (metaKeyId = 0; metaKeyId < metaKeyCount; metaKeyId++) {
        sbuffer.append(docNames[metaKeyId][i]);
        sbuffer.append(" ");
      }
      sbuffer.append(docids[i]);
      sbuffer.append(" ");
      sbuffer.append(scores[i]);
      sbuffer.append('\n');
    }
    // System.out.println(sbuffer.toString());
    pw.write(sbuffer.toString());
    pw.flush();
    // pw.write("finished outputting\n");
  }
 /** method which extracts the docnos for the prescribed resultset */
 protected String[] obtainDocnos(
     final String metaIndexDocumentKey, final SearchRequest q, final ResultSet set)
     throws IOException {
   String[] docnos;
   if (set.hasMetaItems(metaIndexDocumentKey)) {
     docnos = set.getMetaItems(metaIndexDocumentKey);
   } else {
     final MetaIndex metaIndex = index.getMetaIndex();
     docnos = metaIndex.getItems(metaIndexDocumentKey, set.getDocids());
   }
   return docnos;
 }
  /**
   * Runs the actual query expansion
   *
   * @see
   *     org.terrier.querying.PostProcess#process(org.terrier.querying.Manager,org.terrier.querying.SearchRequest)
   */
  public void process(Manager manager, SearchRequest q) {
    Index index = getIndex(manager);
    lastIndex = index;
    documentIndex = index.getDocumentIndex();
    invertedIndex = index.getInvertedIndex();
    lexicon = index.getLexicon();
    collStats = index.getCollectionStatistics();
    directIndex = index.getDirectIndex();
    metaIndex = index.getMetaIndex();
    if (directIndex == null) {
      logger.error("This index does not have a direct index. Query expansion disabled!!");
      return;
    }
    logger.debug("Starting query expansion post-processing.");
    // get the query expansion model to use
    String qeModel = q.getControl("qemodel");
    if (qeModel == null || qeModel.length() == 0) {
      logger.warn(
          "qemodel control not set for QueryExpansion" + " post process. Using default model Bo1");
      qeModel = "Bo1";
    }
    setQueryExpansionModel(getQueryExpansionModel(qeModel));
    if (logger.isDebugEnabled()) {
      logger.info("query expansion model: " + QEModel.getInfo());
    }
    MatchingQueryTerms queryTerms = ((Request) q).getMatchingQueryTerms();
    if (queryTerms == null) {
      logger.warn("No query terms for this query. Skipping QE");
      return;
    }
    // get the expanded query terms
    try {
      expandQuery(queryTerms, (Request) q);
    } catch (IOException ioe) {
      logger.error("IOException while expanding query, skipping QE", ioe);
      return;
    }
    if (logger.isDebugEnabled()) {
      logger.info("query length after expansion: " + queryTerms.length());
      logger.info("Expanded query: ");
    }
    final String[] newQueryTerms = queryTerms.getTerms();
    StringBuilder newQuery = new StringBuilder();
    for (int i = 0; i < newQueryTerms.length; i++) {
      try {
        if (logger.isDebugEnabled()) {
          logger.info(
              (i + 1)
                  + ": "
                  + newQueryTerms[i]
                  + ", normalisedFrequency: "
                  + Rounding.toString(queryTerms.getTermWeight(newQueryTerms[i]), 4));
        }
        newQuery.append(newQueryTerms[i]);
        newQuery.append('^');
        newQuery.append(Rounding.toString(queryTerms.getTermWeight(newQueryTerms[i]), 9));
        newQuery.append(' ');
      } catch (NullPointerException npe) {
        logger.error("Nullpointer exception occured in Query Expansion dumping of new Query", npe);
      }
    }

    logger.debug("NEWQUERY " + q.getQueryID() + " " + newQuery.toString());
    lastExpandedQuery = newQuery.toString();
    q.setControl("QE.ExpandedQuery", newQuery.toString());
    final boolean no2ndPass =
        Boolean.parseBoolean(ApplicationSetup.getProperty("qe.no.2nd.matching", "false"));
    if (no2ndPass) {
      return;
    }

    // run retrieval process again for the expanded query
    logger.info("Accessing inverted file for expanded query " + q.getQueryID());
    manager.runMatching(q);
  }
Beispiel #4
0
 public void readIndex() throws IOException {
   index = terrierIndexFactory.readIndex(indexId, language);
   documentIndex = index.getDocumentIndex();
   metaIndex = index.getMetaIndex();
 }
 /** {@inheritDoc} */
 public void setIndex(Index index) {
   metaIndex = index.getMetaIndex();
 }