01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.lucene.benchmark.quality.utils;
17:
18: import java.io.IOException;
19: import java.io.PrintWriter;
20: import java.text.NumberFormat;
21:
22: import org.apache.lucene.benchmark.quality.QualityQuery;
23: import org.apache.lucene.search.ScoreDoc;
24: import org.apache.lucene.search.Searcher;
25: import org.apache.lucene.search.TopDocs;
26:
27: /**
28: * Create a log ready for submission.
29: * Extend this class and override
30: * {@link #report(QualityQuery, TopDocs, String, Searcher)}
31: * to create different reports.
32: */
33: public class SubmissionReport {
34:
35: private NumberFormat nf;
36: private PrintWriter logger;
37: private String name;
38:
39: /**
40: * Constructor for SubmissionReport.
41: * @param logger if null, no submission data is created.
42: * @param name name of this run.
43: */
44: public SubmissionReport(PrintWriter logger, String name) {
45: this .logger = logger;
46: this .name = name;
47: nf = NumberFormat.getInstance();
48: nf.setMaximumFractionDigits(4);
49: nf.setMinimumFractionDigits(4);
50: }
51:
52: /**
53: * Report a search result for a certain quality query.
54: * @param qq quality query for which the results are reported.
55: * @param td search results for the query.
56: * @param docNameField stored field used for fetching the result doc name.
57: * @param searcher index access for fetching doc name.
58: * @throws IOException in case of a problem.
59: */
60: public void report(QualityQuery qq, TopDocs td,
61: String docNameField, Searcher searcher) throws IOException {
62: if (logger == null) {
63: return;
64: }
65: ScoreDoc sd[] = td.scoreDocs;
66: String sep = " \t ";
67: DocNameExtractor xt = new DocNameExtractor(docNameField);
68: for (int i = 0; i < sd.length; i++) {
69: String docName = xt.docName(searcher, sd[i].doc);
70: logger.println(qq.getQueryID() + sep + "Q0" + sep
71: + format(docName, 20) + sep + format("" + i, 7)
72: + sep + nf.format(sd[i].score) + sep + name);
73: }
74: }
75:
76: public void flush() {
77: if (logger != null) {
78: logger.flush();
79: }
80: }
81:
82: private static String padd = " ";
83:
84: private String format(String s, int minLen) {
85: s = (s == null ? "" : s);
86: int n = Math.max(minLen, s.length());
87: return (s + padd).substring(0, n);
88: }
89: }
|