pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / quality / trec / TrecTopicsReader.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 TREC topics.
29  * <p>
30  * Expects this topic format -
31  * <pre>
32  *   &lt;top&gt;
33  *   &lt;num&gt; Number: nnn
34  *     
35  *   &lt;title&gt; title of the topic
36  *     
37  *   &lt;desc&gt; Description:
38  *   description of the topic
39  *     
40  *   &lt;narr&gt; Narrative:
41  *   "story" composed by assessors.
42  *    
43  *   &lt;/top&gt;
44  * </pre>
45  * Comment lines starting with '#' are ignored.
46  */
47 public class TrecTopicsReader {
48
49   private static final String newline = System.getProperty("line.separator");
50   
51   /**
52    *  Constructor for Trec's TopicsReader
53    */
54   public TrecTopicsReader() {
55     super();
56   }
57
58   /**
59    * Read quality queries from trec format topics file.
60    * @param reader where queries are read from.
61    * @return the result quality queries.
62    * @throws IOException if cannot read the queries.
63    */
64   public QualityQuery[] readQueries(BufferedReader reader) throws IOException {
65     ArrayList<QualityQuery> res = new ArrayList<QualityQuery>();
66     StringBuilder sb;
67     try {
68       while (null!=(sb=read(reader,"<top>",null,false,false))) {
69         HashMap<String,String> fields = new HashMap<String,String>();
70         // id
71         sb = read(reader,"<num>",null,true,false);
72         int k = sb.indexOf(":");
73         String id = sb.substring(k+1).trim();
74         // title
75         sb = read(reader,"<title>",null,true,false);
76         k = sb.indexOf(">");
77         String title = sb.substring(k+1).trim();
78         // description
79         read(reader,"<desc>",null,false,false);
80         sb.setLength(0);
81         String line = null;
82         while ((line = reader.readLine()) != null) {
83           if (line.startsWith("<narr>"))
84             break;
85           if (sb.length() > 0) sb.append(' ');
86           sb.append(line);
87         }
88         String description = sb.toString().trim();
89         // narrative
90         sb.setLength(0);
91         while ((line = reader.readLine()) != null) {
92           if (line.startsWith("</top>"))
93             break;
94           if (sb.length() > 0) sb.append(' ');
95           sb.append(line);
96         }
97         String narrative = sb.toString().trim();
98         // we got a topic!
99         fields.put("title",title);
100         fields.put("description",description);
101         fields.put("narrative", narrative);
102         QualityQuery topic = new QualityQuery(id,fields);
103         res.add(topic);
104       }
105     } finally {
106       reader.close();
107     }
108     // sort result array (by ID) 
109     QualityQuery qq[] = res.toArray(new QualityQuery[0]);
110     Arrays.sort(qq);
111     return qq;
112   }
113
114   // read until finding a line that starts with the specified prefix
115   private StringBuilder read (BufferedReader reader, String prefix, StringBuilder sb, boolean collectMatchLine, boolean collectAll) throws IOException {
116     sb = (sb==null ? new StringBuilder() : sb);
117     String sep = "";
118     while (true) {
119       String line = reader.readLine();
120       if (line==null) {
121         return null;
122       }
123       if (line.startsWith(prefix)) {
124         if (collectMatchLine) {
125           sb.append(sep+line);
126           sep = newline;
127         }
128         break;
129       }
130       if (collectAll) {
131         sb.append(sep+line);
132         sep = newline;
133       }
134     }
135     //System.out.println("read: "+sb);
136     return sb;
137   }
138 }