add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / quality / utils / SubmissionReport.java
1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     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  */
17 package org.apache.lucene.benchmark.quality.utils;
18
19 import java.io.IOException;
20 import java.io.PrintWriter;
21 import java.text.NumberFormat;
22
23 import org.apache.lucene.benchmark.quality.QualityQuery;
24 import org.apache.lucene.search.ScoreDoc;
25 import org.apache.lucene.search.Searcher;
26 import org.apache.lucene.search.TopDocs;
27
28 /**
29  * Create a log ready for submission.
30  * Extend this class and override
31  * {@link #report(QualityQuery, TopDocs, String, Searcher)}
32  * to create different reports. 
33  */
34 public class SubmissionReport {
35
36   private NumberFormat nf;
37   private PrintWriter logger;
38   private String name;
39   
40   /**
41    * Constructor for SubmissionReport.
42    * @param logger if null, no submission data is created. 
43    * @param name name of this run.
44    */
45   public SubmissionReport (PrintWriter logger, String name) {
46     this.logger = logger;
47     this.name = name;
48     nf = NumberFormat.getInstance();
49     nf.setMaximumFractionDigits(4);
50     nf.setMinimumFractionDigits(4);
51   }
52   
53   /**
54    * Report a search result for a certain quality query.
55    * @param qq quality query for which the results are reported.
56    * @param td search results for the query.
57    * @param docNameField stored field used for fetching the result doc name.  
58    * @param searcher index access for fetching doc name.
59    * @throws IOException in case of a problem.
60    */
61   public void report(QualityQuery qq, TopDocs td, 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(
71           qq.getQueryID()       + sep +
72           "Q0"                   + sep +
73           format(docName,20)    + sep +
74           format(""+i,7)        + sep +
75           nf.format(sd[i].score) + sep +
76           name
77           );
78     }
79   }
80
81   public void flush() {
82     if (logger!=null) {
83       logger.flush();
84     }
85   }
86   
87   private static String padd = "                                    ";
88   private String format(String s, int minLen) {
89     s = (s==null ? "" : s);
90     int n = Math.max(minLen,s.length());
91     return (s+padd).substring(0,n);
92   }
93 }