pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / benchmark / src / test / org / apache / lucene / benchmark / quality / TestQualityRun.java
1 package org.apache.lucene.benchmark.quality;
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 org.apache.lucene.benchmark.BenchmarkTestCase;
21 import org.apache.lucene.benchmark.quality.trec.TrecJudge;
22 import org.apache.lucene.benchmark.quality.trec.TrecTopicsReader;
23 import org.apache.lucene.benchmark.quality.utils.SimpleQQParser;
24 import org.apache.lucene.benchmark.quality.utils.SubmissionReport;
25 import org.apache.lucene.index.IndexReader;
26 import org.apache.lucene.search.IndexSearcher;
27 import org.apache.lucene.store.Directory;
28
29 import java.io.BufferedReader;
30 import java.io.File;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.io.PrintWriter;
34
35 /**
36  * Test that quality run does its job.
37  * <p>
38  * NOTE: if the default scoring or StandardAnalyzer is changed, then
39  * this test will not work correctly, as it does not dynamically
40  * generate its test trec topics/qrels!
41  */
42 public class TestQualityRun extends BenchmarkTestCase {
43   
44   @Override
45   public void setUp() throws Exception {
46     super.setUp();
47     copyToWorkDir("reuters.578.lines.txt.bz2");
48   }
49
50   public void testTrecQuality() throws Exception {
51     // first create the partial reuters index
52     createReutersIndex();
53     
54     int maxResults = 1000;
55     String docNameField = "doctitle"; // orig docID is in the linedoc format title 
56     
57     PrintWriter logger = VERBOSE ? new PrintWriter(System.out,true) : null;
58    
59     // prepare topics
60     InputStream topics = getClass().getResourceAsStream("trecTopics.txt");
61     TrecTopicsReader qReader = new TrecTopicsReader();
62     QualityQuery qqs[] = qReader.readQueries(new BufferedReader(new InputStreamReader(topics, "UTF-8")));
63     
64     // prepare judge
65     InputStream qrels = getClass().getResourceAsStream("trecQRels.txt");
66     Judge judge = new TrecJudge(new BufferedReader(new InputStreamReader(qrels, "UTF-8")));
67     
68     // validate topics & judgments match each other
69     judge.validateData(qqs, logger);
70     
71     Directory dir = newFSDirectory(new File(getWorkDir(),"index"));
72     IndexReader r = IndexReader.open(dir, true);
73     IndexSearcher searcher = newSearcher(r);
74
75     QualityQueryParser qqParser = new SimpleQQParser("title","body");
76     QualityBenchmark qrun = new QualityBenchmark(qqs, qqParser, searcher, docNameField);
77     
78     SubmissionReport submitLog = VERBOSE ? new SubmissionReport(logger, "TestRun") : null;
79     qrun.setMaxResults(maxResults);
80     QualityStats stats[] = qrun.execute(judge, submitLog, logger);
81     
82     // --------- verify by the way judgments were altered for this test:
83     // for some queries, depending on m = qnum % 8
84     // m==0: avg_precision and recall are hurt, by marking fake docs as relevant
85     // m==1: precision_at_n and avg_precision are hurt, by unmarking relevant docs
86     // m==2: all precision, precision_at_n and recall are hurt.
87     // m>=3: these queries remain perfect
88     for (int i = 0; i < stats.length; i++) {
89       QualityStats s = stats[i];
90       switch (i%8) {
91
92       case 0:
93         assertTrue("avg-p should be hurt: "+s.getAvp(), 1.0 > s.getAvp());
94         assertTrue("recall should be hurt: "+s.getRecall(), 1.0 > s.getRecall());
95         for (int j = 1; j <= QualityStats.MAX_POINTS; j++) {
96           assertEquals("p_at_"+j+" should be perfect: "+s.getPrecisionAt(j), 1.0, s.getPrecisionAt(j), 1E-2);
97         }
98         break;
99       
100       case 1:
101         assertTrue("avg-p should be hurt", 1.0 > s.getAvp());
102         assertEquals("recall should be perfect: "+s.getRecall(), 1.0, s.getRecall(), 1E-2);
103         for (int j = 1; j <= QualityStats.MAX_POINTS; j++) {
104           assertTrue("p_at_"+j+" should be hurt: "+s.getPrecisionAt(j), 1.0 > s.getPrecisionAt(j));
105         }
106         break;
107
108       case 2:
109         assertTrue("avg-p should be hurt: "+s.getAvp(), 1.0 > s.getAvp());
110         assertTrue("recall should be hurt: "+s.getRecall(), 1.0 > s.getRecall());
111         for (int j = 1; j <= QualityStats.MAX_POINTS; j++) {
112           assertTrue("p_at_"+j+" should be hurt: "+s.getPrecisionAt(j), 1.0 > s.getPrecisionAt(j));
113         }
114         break;
115
116       default: {
117         assertEquals("avg-p should be perfect: "+s.getAvp(), 1.0, s.getAvp(), 1E-2);
118         assertEquals("recall should be perfect: "+s.getRecall(), 1.0, s.getRecall(), 1E-2);
119         for (int j = 1; j <= QualityStats.MAX_POINTS; j++) {
120           assertEquals("p_at_"+j+" should be perfect: "+s.getPrecisionAt(j), 1.0, s.getPrecisionAt(j), 1E-2);
121         }
122       }
123       
124       }
125     }
126     
127     QualityStats avg = QualityStats.average(stats);
128     if (logger!=null) {
129       avg.log("Average statistis:",1,logger,"  ");
130     }
131     
132     assertTrue("mean avg-p should be hurt: "+avg.getAvp(), 1.0 > avg.getAvp());
133     assertTrue("avg recall should be hurt: "+avg.getRecall(), 1.0 > avg.getRecall());
134     for (int j = 1; j <= QualityStats.MAX_POINTS; j++) {
135       assertTrue("avg p_at_"+j+" should be hurt: "+avg.getPrecisionAt(j), 1.0 > avg.getPrecisionAt(j));
136     }
137     
138     searcher.close();
139     r.close();
140     dir.close();
141   }
142   
143   public void testTrecTopicsReader() throws Exception {    
144     // prepare topics
145     InputStream topicsFile = getClass().getResourceAsStream("trecTopics.txt");
146     TrecTopicsReader qReader = new TrecTopicsReader();
147     QualityQuery qqs[] = qReader.readQueries(
148         new BufferedReader(new InputStreamReader(topicsFile, "UTF-8")));
149     
150     assertEquals(20, qqs.length);
151     
152     QualityQuery qq = qqs[0];
153     assertEquals("statement months  total 1987", qq.getValue("title"));
154     assertEquals("Topic 0 Description Line 1 Topic 0 Description Line 2", 
155         qq.getValue("description"));
156     assertEquals("Topic 0 Narrative Line 1 Topic 0 Narrative Line 2", 
157         qq.getValue("narrative"));
158     
159     qq = qqs[1];
160     assertEquals("agreed 15  against five", qq.getValue("title"));
161     assertEquals("Topic 1 Description Line 1 Topic 1 Description Line 2", 
162         qq.getValue("description"));
163     assertEquals("Topic 1 Narrative Line 1 Topic 1 Narrative Line 2", 
164         qq.getValue("narrative"));
165     
166     qq = qqs[19];
167     assertEquals("20 while  common week", qq.getValue("title"));
168     assertEquals("Topic 19 Description Line 1 Topic 19 Description Line 2", 
169         qq.getValue("description"));
170     assertEquals("Topic 19 Narrative Line 1 Topic 19 Narrative Line 2", 
171         qq.getValue("narrative"));
172   }
173
174   // use benchmark logic to create the mini Reuters index
175   private void createReutersIndex() throws Exception {
176     // 1. alg definition
177     String algLines[] = {
178         "# ----- properties ",
179         "content.source=org.apache.lucene.benchmark.byTask.feeds.LineDocSource",
180         "analyzer=org.apache.lucene.analysis.standard.ClassicAnalyzer",
181         "docs.file=" + getWorkDirResourcePath("reuters.578.lines.txt.bz2"),
182         "content.source.log.step=2500",
183         "doc.term.vector=false",
184         "content.source.forever=false",
185         "directory=FSDirectory",
186         "doc.stored=true",
187         "doc.tokenized=true",
188         "# ----- alg ",
189         "ResetSystemErase",
190         "CreateIndex",
191         "{ AddDoc } : *",
192         "CloseIndex",
193     };
194     
195     // 2. execute the algorithm  (required in every "logic" test)
196     execBenchmark(algLines);
197   }
198 }