pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / quality / trec / Trec1MQReader.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.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.HashMap;
24
25 import org.apache.lucene.benchmark.quality.QualityQuery;
26
27 /**
28  * Read topics of TREC 1MQ track.
29  * <p>
30  * Expects this topic format -
31  * <pre>
32  *   qnum:qtext
33  * </pre>
34  * Comment lines starting with '#' are ignored.
35  * <p>
36  * All topics will have a single name value pair.
37  */
38 public class Trec1MQReader {
39
40   private String name;
41   
42   /**
43    *  Constructor for Trec's 1MQ TopicsReader
44    *  @param name name of name-value pair to set for all queries.
45    */
46   public Trec1MQReader(String name) {
47     super();
48     this.name = name;
49   }
50
51   /**
52    * Read quality queries from trec 1MQ format topics file.
53    * @param reader where queries are read from.
54    * @return the result quality queries.
55    * @throws IOException if cannot read the queries.
56    */
57   public QualityQuery[] readQueries(BufferedReader reader) throws IOException {
58     ArrayList<QualityQuery> res = new ArrayList<QualityQuery>();
59     String line;
60     try {
61       while (null!=(line=reader.readLine())) {
62         line = line.trim();
63         if (line.startsWith("#")) {
64           continue;
65         }
66         // id
67         int k = line.indexOf(":");
68         String id = line.substring(0,k).trim();
69         // qtext
70         String qtext = line.substring(k+1).trim();
71         // we got a topic!
72         HashMap<String,String> fields = new HashMap<String,String>();
73         fields.put(name,qtext);
74         //System.out.println("id: "+id+" qtext: "+qtext+"  line: "+line);
75         QualityQuery topic = new QualityQuery(id,fields);
76         res.add(topic);
77       }
78     } finally {
79       reader.close();
80     }
81     // sort result array (by ID) 
82     QualityQuery qq[] = res.toArray(new QualityQuery[0]);
83     Arrays.sort(qq);
84     return qq;
85   }
86
87 }