add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / quality / trec / TrecJudge.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.trec;
18
19 import java.io.BufferedReader;
20 import java.io.IOException;
21 import java.io.PrintWriter;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.StringTokenizer;
25
26 import org.apache.lucene.benchmark.quality.Judge;
27 import org.apache.lucene.benchmark.quality.QualityQuery;
28
29 /**
30  * Judge if given document is relevant to given quality query, based on Trec format for judgements.
31  */
32 public class TrecJudge implements Judge {
33
34   HashMap<String,QRelJudgement> judgements;
35   
36   /**
37    * Constructor from a reader.
38    * <p>
39    * Expected input format:
40    * <pre>
41    *     qnum  0   doc-name     is-relevant
42    * </pre> 
43    * Two sample lines:
44    * <pre> 
45    *     19    0   doc303       1
46    *     19    0   doc7295      0
47    * </pre> 
48    * @param reader where judgments are read from.
49    * @throws IOException 
50    */
51   public TrecJudge (BufferedReader reader) throws IOException {
52     judgements = new HashMap<String,QRelJudgement>();
53     QRelJudgement curr = null;
54     String zero = "0";
55     String line;
56     
57     try {
58       while (null!=(line=reader.readLine())) {
59         line = line.trim();
60         if (line.length()==0 || '#'==line.charAt(0)) {
61           continue;
62         }
63         StringTokenizer st = new StringTokenizer(line);
64         String queryID = st.nextToken();
65         st.nextToken();
66         String docName = st.nextToken();
67         boolean relevant = !zero.equals(st.nextToken());
68         assert !st.hasMoreTokens() : "wrong format: "+line+"  next: "+st.nextToken();
69         if (relevant) { // only keep relevant docs
70           if (curr==null || !curr.queryID.equals(queryID)) {
71             curr = judgements.get(queryID);
72             if (curr==null) {
73               curr = new QRelJudgement(queryID);
74               judgements.put(queryID,curr);
75             }
76           }
77           curr.addRelevandDoc(docName);
78         }
79       }
80     } finally {
81       reader.close();
82     }
83   }
84   
85   // inherit javadocs
86   public boolean isRelevant(String docName, QualityQuery query) {
87     QRelJudgement qrj = judgements.get(query.getQueryID());
88     return qrj!=null && qrj.isRelevant(docName);
89   }
90
91   /** single Judgement of a trec quality query */
92   private static class QRelJudgement {
93     private String queryID;
94     private HashMap<String,String> relevantDocs;
95     
96     QRelJudgement(String queryID) {
97       this.queryID = queryID;
98       relevantDocs = new HashMap<String,String>();
99     }
100     
101     public void addRelevandDoc(String docName) {
102       relevantDocs.put(docName,docName);
103     }
104
105     boolean isRelevant(String docName) {
106       return relevantDocs.containsKey(docName);
107     }
108
109     public int maxRecall() {
110       return relevantDocs.size();
111     }
112   }
113
114   // inherit javadocs
115   public boolean validateData(QualityQuery[] qq, PrintWriter logger) {
116     HashMap<String,QRelJudgement> missingQueries = new HashMap<String, QRelJudgement>(judgements);
117     ArrayList<String> missingJudgements = new ArrayList<String>();
118     for (int i=0; i<qq.length; i++) {
119       String id = qq[i].getQueryID();
120       if (missingQueries.containsKey(id)) {
121         missingQueries.remove(id);
122       } else {
123         missingJudgements.add(id);
124       }
125     }
126     boolean isValid = true;
127     if (missingJudgements.size()>0) {
128       isValid = false;
129       if (logger!=null) {
130         logger.println("WARNING: "+missingJudgements.size()+" queries have no judgments! - ");
131         for (int i=0; i<missingJudgements.size(); i++) {
132           logger.println("   "+ missingJudgements.get(i));
133         }
134       }
135     }
136     if (missingQueries.size()>0) {
137       isValid = false;
138       if (logger!=null) {
139         logger.println("WARNING: "+missingQueries.size()+" judgments match no query! - ");
140         for (final String id : missingQueries.keySet()) {
141           logger.println("   "+id);
142         }
143       }
144     }
145     return isValid;
146   }
147
148   // inherit javadocs
149   public int maxRecall(QualityQuery query) {
150     QRelJudgement qrj = judgements.get(query.getQueryID());
151     if (qrj!=null) {
152       return qrj.maxRecall();
153     }
154     return 0;
155   }
156 }