| 1 | |
|
| 2 | |
package org.galagosearch.core.parse; |
| 3 | |
|
| 4 | |
import java.io.BufferedReader; |
| 5 | |
import java.io.FileNotFoundException; |
| 6 | |
import java.io.IOException; |
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
public class TrecTextParser implements DocumentStreamParser { |
| 13 | |
BufferedReader reader; |
| 14 | |
|
| 15 | |
|
| 16 | 0 | public TrecTextParser(BufferedReader reader) throws FileNotFoundException, IOException { |
| 17 | 0 | this.reader = reader; |
| 18 | 0 | } |
| 19 | |
|
| 20 | |
public String waitFor(String tag) throws IOException { |
| 21 | |
String line; |
| 22 | |
|
| 23 | 0 | while ((line = reader.readLine()) != null) { |
| 24 | 0 | if (line.startsWith(tag)) { |
| 25 | 0 | return line; |
| 26 | |
} |
| 27 | |
} |
| 28 | |
|
| 29 | 0 | return null; |
| 30 | |
} |
| 31 | |
|
| 32 | |
public String parseDocNumber() throws IOException { |
| 33 | 0 | String allText = waitFor("<DOCNO>"); |
| 34 | |
|
| 35 | 0 | while (allText.contains("</DOCNO>") == false) { |
| 36 | 0 | String line = reader.readLine(); |
| 37 | 0 | if (line == null) { |
| 38 | 0 | break; |
| 39 | |
} |
| 40 | 0 | allText += line; |
| 41 | 0 | } |
| 42 | |
|
| 43 | 0 | int start = allText.indexOf("<DOCNO>") + 7; |
| 44 | 0 | int end = allText.indexOf("</DOCNO>"); |
| 45 | |
|
| 46 | 0 | return new String(allText.substring(start, end).trim()); |
| 47 | |
} |
| 48 | |
|
| 49 | |
public Document nextDocument() throws IOException { |
| 50 | |
String line; |
| 51 | |
|
| 52 | 0 | if (null == waitFor("<DOC>")) { |
| 53 | 0 | return null; |
| 54 | |
} |
| 55 | 0 | String identifier = parseDocNumber(); |
| 56 | 0 | StringBuffer buffer = new StringBuffer(); |
| 57 | |
|
| 58 | 0 | String[] startTags = {"<TEXT>", "<HEADLINE>", "<TITLE>", "<HL>", "<HEAD>", |
| 59 | |
"<TTL>", "<DD>", "<DATE>", "<LP>", "<LEADPARA>" |
| 60 | |
}; |
| 61 | 0 | String[] endTags = {"</TEXT>", "</HEADLINE>", "</TITLE>", "</HL>", "</HEAD>", |
| 62 | |
"</TTL>", "</DD>", "</DATE>", "</LP>", "</LEADPARA>" |
| 63 | |
}; |
| 64 | |
|
| 65 | 0 | int inTag = -1; |
| 66 | |
|
| 67 | 0 | while ((line = reader.readLine()) != null) { |
| 68 | 0 | if (line.startsWith("</DOC>")) { |
| 69 | 0 | break; |
| 70 | |
} |
| 71 | 0 | if (line.startsWith("<")) { |
| 72 | 0 | if (inTag >= 0 && line.startsWith(endTags[inTag])) { |
| 73 | 0 | inTag = -1; |
| 74 | |
|
| 75 | 0 | buffer.append(line); |
| 76 | 0 | buffer.append(' '); |
| 77 | 0 | } else if (inTag < 0) { |
| 78 | 0 | for (int i = 0; i < startTags.length; i++) { |
| 79 | 0 | if (line.startsWith(startTags[i])) { |
| 80 | 0 | inTag = i; |
| 81 | 0 | break; |
| 82 | |
} |
| 83 | |
} |
| 84 | |
} |
| 85 | |
} |
| 86 | |
|
| 87 | 0 | if (inTag >= 0) { |
| 88 | 0 | buffer.append(line); |
| 89 | 0 | buffer.append(' '); |
| 90 | |
} |
| 91 | |
} |
| 92 | |
|
| 93 | 0 | return new Document(identifier, buffer.toString()); |
| 94 | |
} |
| 95 | |
} |