pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / quality / trec / QueryDriver.java
1 package org.apache.lucene.benchmark.quality.trec;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.BufferedReader;
21 import java.io.File;
22 import java.io.FileReader;
23 import java.io.PrintWriter;
24 import java.util.HashSet;
25 import java.util.Set;
26
27 import org.apache.lucene.benchmark.quality.Judge;
28 import org.apache.lucene.benchmark.quality.QualityBenchmark;
29 import org.apache.lucene.benchmark.quality.QualityQuery;
30 import org.apache.lucene.benchmark.quality.QualityQueryParser;
31 import org.apache.lucene.benchmark.quality.QualityStats;
32 import org.apache.lucene.benchmark.quality.utils.SimpleQQParser;
33 import org.apache.lucene.benchmark.quality.utils.SubmissionReport;
34 import org.apache.lucene.index.IndexReader;
35 import org.apache.lucene.search.IndexSearcher;
36 import org.apache.lucene.store.FSDirectory;
37
38 /**
39  *
40  *
41  **/
42 public class QueryDriver {
43   public static void main(String[] args) throws Exception {
44     if (args.length < 4 || args.length > 5) {
45       System.err.println("Usage: QueryDriver <topicsFile> <qrelsFile> <submissionFile> <indexDir> [querySpec]");
46       System.err.println("topicsFile: input file containing queries");
47       System.err.println("qrelsFile: input file containing relevance judgements");
48       System.err.println("submissionFile: output submission file for trec_eval");
49       System.err.println("indexDir: index directory");
50       System.err.println("querySpec: string composed of fields to use in query consisting of T=title,D=description,N=narrative:");
51       System.err.println("\texample: TD (query on Title + Description). The default is T (title only)");
52       System.exit(1);
53     }
54     
55     File topicsFile = new File(args[0]);
56     File qrelsFile = new File(args[1]);
57     SubmissionReport submitLog = new SubmissionReport(new PrintWriter(args[2]), "lucene");
58     FSDirectory dir = FSDirectory.open(new File(args[3]));
59     IndexReader r = IndexReader.open(dir, true);
60     IndexSearcher searcher = new IndexSearcher(r);
61     String fieldSpec = args.length == 5 ? args[4] : "T"; // default to Title-only if not specified.
62
63     int maxResults = 1000;
64     String docNameField = "docname";
65
66     PrintWriter logger = new PrintWriter(System.out, true);
67
68     // use trec utilities to read trec topics into quality queries
69     TrecTopicsReader qReader = new TrecTopicsReader();
70     QualityQuery qqs[] = qReader.readQueries(new BufferedReader(new FileReader(topicsFile)));
71
72     // prepare judge, with trec utilities that read from a QRels file
73     Judge judge = new TrecJudge(new BufferedReader(new FileReader(qrelsFile)));
74
75     // validate topics & judgments match each other
76     judge.validateData(qqs, logger);
77
78     Set<String> fieldSet = new HashSet<String>();
79     if (fieldSpec.indexOf('T') >= 0) fieldSet.add("title");
80     if (fieldSpec.indexOf('D') >= 0) fieldSet.add("description");
81     if (fieldSpec.indexOf('N') >= 0) fieldSet.add("narrative");
82     
83     // set the parsing of quality queries into Lucene queries.
84     QualityQueryParser qqParser = new SimpleQQParser(fieldSet.toArray(new String[0]), "body");
85
86     // run the benchmark
87     QualityBenchmark qrun = new QualityBenchmark(qqs, qqParser, searcher, docNameField);
88     qrun.setMaxResults(maxResults);
89     QualityStats stats[] = qrun.execute(judge, submitLog, logger);
90
91     // print an avarage sum of the results
92     QualityStats avg = QualityStats.average(stats);
93     avg.log("SUMMARY", 2, logger, "  ");
94     
95     searcher.close();
96     r.close();
97     dir.close();
98   }
99   
100 }