1
package org.apache.lucene.benchmark.utils;
3
* Copyright 2005 The Apache Software Foundation
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
* you may not use this file except in compliance with the License.
7
* You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
19
import java.io.BufferedReader;
21
import java.io.FileFilter;
22
import java.io.FileReader;
23
import java.io.FileWriter;
24
import java.io.IOException;
25
import java.util.regex.Matcher;
26
import java.util.regex.Pattern;
30
* Split the Reuters SGML documents into Simple Text files containing: Title, Date, Dateline, Body
32
public class ExtractReuters
34
private File reutersDir;
35
private File outputDir;
36
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
38
public ExtractReuters(File reutersDir, File outputDir)
40
this.reutersDir = reutersDir;
41
this.outputDir = outputDir;
42
System.out.println("Deleting all files in " + outputDir);
43
File [] files = outputDir.listFiles();
44
for (int i = 0; i < files.length; i++)
53
File [] sgmFiles = reutersDir.listFiles(new FileFilter()
55
public boolean accept(File file)
57
return file.getName().endsWith(".sgm");
60
if (sgmFiles != null && sgmFiles.length > 0)
62
for (int i = 0; i < sgmFiles.length; i++)
64
File sgmFile = sgmFiles[i];
70
System.err.println("No .sgm files in " + reutersDir);
74
Pattern EXTRACTION_PATTERN = Pattern.compile("<TITLE>(.*?)</TITLE>|<DATE>(.*?)</DATE>|<BODY>(.*?)</BODY>");
76
private static String[] META_CHARS
77
= {"&", "<", ">", "\"", "'"};
79
private static String[] META_CHARS_SERIALIZATIONS
80
= {"&", "<", ">", """, "'"};
83
* Override if you wish to change what is extracted
87
protected void extractFile(File sgmFile)
91
BufferedReader reader = new BufferedReader(new FileReader(sgmFile));
93
StringBuffer buffer = new StringBuffer(1024);
94
StringBuffer outBuffer = new StringBuffer(1024);
99
while ((line = reader.readLine()) != null)
101
//when we see a closing reuters tag, flush the file
103
if ((index = line.indexOf("</REUTERS")) == -1)
105
//Replace the SGM escape sequences
107
buffer.append(line).append(' ');//accumulate the strings for now, then apply regular expression to get the pieces,
111
//Extract the relevant pieces and write to a file in the output dir
112
Matcher matcher = EXTRACTION_PATTERN.matcher(buffer);
113
while (matcher.find())
115
for (int i = 1; i <= matcher.groupCount(); i++)
117
if (matcher.group(i) != null)
119
outBuffer.append(matcher.group(i));
122
outBuffer.append(LINE_SEPARATOR).append(LINE_SEPARATOR);
124
String out = outBuffer.toString();
125
for (int i = 0; i < META_CHARS_SERIALIZATIONS.length; i++)
127
out = out.replaceAll(META_CHARS_SERIALIZATIONS[i], META_CHARS[i]);
129
File outFile = new File(outputDir, sgmFile.getName() + "-" + (docNumber++) + ".txt");
130
//System.out.println("Writing " + outFile);
131
FileWriter writer = new FileWriter(outFile);
134
outBuffer.setLength(0);
146
throw new RuntimeException(e);
151
public static void main(String[] args)
153
if (args.length != 2)
157
File reutersDir = new File(args[0]);
159
if (reutersDir.exists())
161
File outputDir = new File(args[1]);
163
ExtractReuters extractor = new ExtractReuters(reutersDir, outputDir);
172
private static void printUsage()
174
System.err.println("Usage: java -cp <...> org.apache.lucene.benchmark.utils.ExtractReuters <Path to Reuters SGM files> <Output Path>");