pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / quality / utils / QualityQueriesFinder.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.File;
20 import java.io.IOException;
21
22 import org.apache.lucene.index.IndexReader;
23 import org.apache.lucene.index.Term;
24 import org.apache.lucene.index.TermEnum;
25 import org.apache.lucene.store.Directory;
26 import org.apache.lucene.store.FSDirectory;
27 import org.apache.lucene.util.PriorityQueue;
28
29 /**
30  * Suggest Quality queries based on an index contents.
31  * Utility class, used for making quality test benchmarks.
32  */
33 public class QualityQueriesFinder {
34
35   private static final String newline = System.getProperty("line.separator");
36   private Directory dir;
37   
38   /**
39    * Constructor over a directory containing the index.
40    * @param dir directory containing the index we search for the quality test. 
41    */
42   private QualityQueriesFinder(Directory dir) {
43     this.dir = dir;
44   }
45
46   /**
47    * @param args {index-dir}
48    * @throws IOException  if cannot access the index.
49    */
50   public static void main(String[] args) throws IOException {
51     if (args.length<1) {
52       System.err.println("Usage: java QualityQueriesFinder <index-dir>");
53       System.exit(1);
54     }
55     QualityQueriesFinder qqf = new QualityQueriesFinder(FSDirectory.open(new File(args[0])));
56     String q[] = qqf.bestQueries("body",20);
57     for (int i=0; i<q.length; i++) {
58       System.out.println(newline+formatQueryAsTrecTopic(i,q[i],null,null));
59     }
60   }
61
62   private String [] bestQueries(String field,int numQueries) throws IOException {
63     String words[] = bestTerms("body",4*numQueries);
64     int n = words.length;
65     int m = n/4;
66     String res[] = new String[m];
67     for (int i=0; i<res.length; i++) {
68       res[i] = words[i] + " " + words[m+i]+ "  " + words[n-1-m-i]  + " " + words[n-1-i];
69       //System.out.println("query["+i+"]:  "+res[i]);
70     }
71     return res;
72   }
73   
74   private static String formatQueryAsTrecTopic (int qnum, String title, String description, String narrative) {
75     return 
76       "<top>" + newline +
77       "<num> Number: " + qnum             + newline + newline + 
78       "<title> " + (title==null?"":title) + newline + newline + 
79       "<desc> Description:"               + newline +
80       (description==null?"":description)  + newline + newline +
81       "<narr> Narrative:"                 + newline +
82       (narrative==null?"":narrative)      + newline + newline +
83       "</top>";
84   }
85   
86   private String [] bestTerms(String field,int numTerms) throws IOException {
87     PriorityQueue<TermDf> pq = new TermsDfQueue(numTerms);
88     IndexReader ir = IndexReader.open(dir, true);
89     try {
90       int threshold = ir.maxDoc() / 10; // ignore words too common.
91       TermEnum terms = ir.terms(new Term(field,""));
92       while (terms.next()) {
93         if (!field.equals(terms.term().field())) {
94           break;
95         }
96         int df = terms.docFreq();
97         if (df<threshold) {
98           String ttxt = terms.term().text();
99           pq.insertWithOverflow(new TermDf(ttxt,df));
100         }
101       }
102     } finally {
103       ir.close();
104     }
105     String res[] = new String[pq.size()];
106     int i = 0;
107     while (pq.size()>0) {
108       TermDf tdf = pq.pop(); 
109       res[i++] = tdf.word;
110       System.out.println(i+".   word:  "+tdf.df+"   "+tdf.word);
111     }
112     return res;
113   }
114
115   private static class TermDf {
116     String word;
117     int df;
118     TermDf (String word, int freq) {
119       this.word = word;
120       this.df = freq;
121     }
122   }
123   
124   private static class TermsDfQueue extends PriorityQueue<TermDf> {
125     TermsDfQueue (int maxSize) {
126       initialize(maxSize);
127     }
128     @Override
129     protected boolean lessThan(TermDf tf1, TermDf tf2) {
130       return tf1.df < tf2.df;
131     }
132   }
133   
134 }