~ubuntu-branches/ubuntu/trusty/pylucene/trusty

« back to all changes in this revision

Viewing changes to lucene-java-2.3.1/contrib/benchmark/src/java/org/apache/lucene/benchmark/quality/trec/TrecTopicsReader.java

  • Committer: Package Import Robot
  • Author(s): Dmitry Nezhevenko
  • Date: 2012-04-23 16:43:55 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120423164355-grqtepnwtecdjfk2
Tags: 3.5.0-1
* New maintainer (closes: 670179)
* New upstream release
* Switch to dpkg-source 3.0 (quilt) format
* Switch to machine-readable debian/copyright
* Bump debian/compat to 8, drop debian/pycompat
* Switch from cdbs to dh
* Add watch file
* Build for all supported versions of python2 (closes: 581198, 632240)
* Rename binary package to python-lucene (closes: 581197)
* Add -dbg package

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 res = new ArrayList();
66
 
    StringBuffer sb;
67
 
    try {
68
 
      while (null!=(sb=read(reader,"<top>",null,false,false))) {
69
 
        HashMap fields = new HashMap();
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
 
        sb = read(reader,"<desc>",null,false,false);
80
 
        sb = read(reader,"<narr>",null,false,true);
81
 
        String descripion = sb.toString().trim();
82
 
        // we got a topic!
83
 
        fields.put("title",title);
84
 
        fields.put("description",descripion);
85
 
        QualityQuery topic = new QualityQuery(id,fields);
86
 
        res.add(topic);
87
 
        // skip narrative, get to end of doc
88
 
        read(reader,"</top>",null,false,false);
89
 
      }
90
 
    } finally {
91
 
      reader.close();
92
 
    }
93
 
    // sort result array (by ID) 
94
 
    QualityQuery qq[] = (QualityQuery[]) res.toArray(new QualityQuery[0]);
95
 
    Arrays.sort(qq);
96
 
    return qq;
97
 
  }
98
 
 
99
 
  // read until finding a line that starts with the specified prefix
100
 
  private StringBuffer read (BufferedReader reader, String prefix, StringBuffer sb, boolean collectMatchLine, boolean collectAll) throws IOException {
101
 
    sb = (sb==null ? new StringBuffer() : sb);
102
 
    String sep = "";
103
 
    while (true) {
104
 
      String line = reader.readLine();
105
 
      if (line==null) {
106
 
        return null;
107
 
      }
108
 
      if (line.startsWith(prefix)) {
109
 
        if (collectMatchLine) {
110
 
          sb.append(sep+line);
111
 
          sep = newline;
112
 
        }
113
 
        break;
114
 
      }
115
 
      if (collectAll) {
116
 
        sb.append(sep+line);
117
 
        sep = newline;
118
 
      }
119
 
    }
120
 
    //System.out.println("read: "+sb);
121
 
    return sb;
122
 
  }
123
 
}