pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / byTask / feeds / TrecGov2Parser.java
1 package org.apache.lucene.benchmark.byTask.feeds;
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 java.io.IOException;
21 import java.io.Reader;
22 import java.util.Date;
23
24 /**
25  * Parser for the GOV2 collection format
26  */
27 public class TrecGov2Parser extends TrecDocParser {
28
29   private static final String DATE = "Date: ";
30   private static final String DATE_END = TrecContentSource.NEW_LINE;
31   
32   private static final String DOCHDR = "<DOCHDR>";
33   private static final String TERMINATING_DOCHDR = "</DOCHDR>";
34   private static final int TERMINATING_DOCHDR_LENGTH = TERMINATING_DOCHDR.length();
35
36   @Override
37   public DocData parse(DocData docData, String name, TrecContentSource trecSrc, 
38       StringBuilder docBuf, ParsePathType pathType) throws IOException, InterruptedException {
39     // Set up a (per-thread) reused Reader over the read content, reset it to re-read from docBuf
40     Reader r = trecSrc.getTrecDocReader(docBuf);
41
42     // skip some of the text, optionally set date
43     Date date = null;
44     int h1 = docBuf.indexOf(DOCHDR);
45     if (h1>=0) {
46       int h2 = docBuf.indexOf(TERMINATING_DOCHDR,h1);
47       String dateStr = extract(docBuf, DATE, DATE_END, h2, null);
48       if (dateStr != null) {
49         date = trecSrc.parseDate(dateStr);
50       }
51       r.mark(h2+TERMINATING_DOCHDR_LENGTH);
52     }
53
54     r.reset();
55     HTMLParser htmlParser = trecSrc.getHtmlParser();
56     return htmlParser.parse(docData, name, date, null, r, null);
57   }
58   
59 }