~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/utils/ExtractReuters.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
 
package org.apache.lucene.benchmark.utils;
2
 
/**
3
 
 * Copyright 2005 The Apache Software Foundation
4
 
 *
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
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
 
 
18
 
 
19
 
import java.io.BufferedReader;
20
 
import java.io.File;
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;
27
 
 
28
 
 
29
 
/**
30
 
 * Split the Reuters SGML documents into Simple Text files containing: Title, Date, Dateline, Body
31
 
 */
32
 
public class ExtractReuters
33
 
{
34
 
    private File reutersDir;
35
 
    private File outputDir;
36
 
    private static final String LINE_SEPARATOR = System.getProperty("line.separator");
37
 
 
38
 
    public ExtractReuters(File reutersDir, File outputDir)
39
 
    {
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++)
45
 
        {
46
 
            files[i].delete();
47
 
        }
48
 
 
49
 
    }
50
 
 
51
 
    public void extract()
52
 
    {
53
 
        File [] sgmFiles = reutersDir.listFiles(new FileFilter()
54
 
        {
55
 
            public boolean accept(File file)
56
 
            {
57
 
                return file.getName().endsWith(".sgm");
58
 
            }
59
 
        });
60
 
        if (sgmFiles != null && sgmFiles.length > 0)
61
 
        {
62
 
            for (int i = 0; i < sgmFiles.length; i++)
63
 
            {
64
 
                File sgmFile = sgmFiles[i];
65
 
                extractFile(sgmFile);
66
 
            }
67
 
        }
68
 
        else
69
 
        {
70
 
            System.err.println("No .sgm files in " + reutersDir);
71
 
        }
72
 
    }
73
 
 
74
 
    Pattern EXTRACTION_PATTERN = Pattern.compile("<TITLE>(.*?)</TITLE>|<DATE>(.*?)</DATE>|<BODY>(.*?)</BODY>");
75
 
 
76
 
    private static String[] META_CHARS
77
 
            = {"&", "<", ">", "\"", "'"};
78
 
 
79
 
    private static String[] META_CHARS_SERIALIZATIONS
80
 
            = {"&amp;", "&lt;", "&gt;", "&quot;", "&apos;"};
81
 
 
82
 
    /**
83
 
     * Override if you wish to change what is extracted
84
 
     *
85
 
     * @param sgmFile
86
 
     */
87
 
    protected void extractFile(File sgmFile)
88
 
    {
89
 
        try
90
 
        {
91
 
            BufferedReader reader = new BufferedReader(new FileReader(sgmFile));
92
 
 
93
 
            StringBuffer buffer = new StringBuffer(1024);
94
 
            StringBuffer outBuffer = new StringBuffer(1024);
95
 
 
96
 
            String line = null;
97
 
            int index = -1;
98
 
            int docNumber = 0;
99
 
            while ((line = reader.readLine()) != null)
100
 
            {
101
 
                //when we see a closing reuters tag, flush the file
102
 
 
103
 
                if ((index = line.indexOf("</REUTERS")) == -1)
104
 
                {
105
 
                    //Replace the SGM escape sequences
106
 
 
107
 
                    buffer.append(line).append(' ');//accumulate the strings for now, then apply regular expression to get the pieces,
108
 
                }
109
 
                else
110
 
                {
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())
114
 
                    {
115
 
                        for (int i = 1; i <= matcher.groupCount(); i++)
116
 
                        {
117
 
                            if (matcher.group(i) != null)
118
 
                            {
119
 
                                outBuffer.append(matcher.group(i));
120
 
                            }
121
 
                        }
122
 
                        outBuffer.append(LINE_SEPARATOR).append(LINE_SEPARATOR);
123
 
                    }
124
 
                    String out = outBuffer.toString();
125
 
                    for (int i = 0; i < META_CHARS_SERIALIZATIONS.length; i++)
126
 
                    {
127
 
                        out = out.replaceAll(META_CHARS_SERIALIZATIONS[i], META_CHARS[i]);
128
 
                    }
129
 
                    File outFile = new File(outputDir, sgmFile.getName() + "-" + (docNumber++) + ".txt");
130
 
                    //System.out.println("Writing " + outFile);
131
 
                    FileWriter writer = new FileWriter(outFile);
132
 
                    writer.write(out);
133
 
                    writer.close();
134
 
                    outBuffer.setLength(0);
135
 
                    buffer.setLength(0);
136
 
                }
137
 
            }
138
 
            reader.close();
139
 
        }
140
 
 
141
 
        catch (
142
 
                IOException e
143
 
                )
144
 
 
145
 
        {
146
 
            throw new RuntimeException(e);
147
 
        }
148
 
    }
149
 
 
150
 
 
151
 
    public static void main(String[] args)
152
 
    {
153
 
        if (args.length != 2)
154
 
        {
155
 
            printUsage();
156
 
        }
157
 
        File reutersDir = new File(args[0]);
158
 
 
159
 
        if (reutersDir.exists())
160
 
        {
161
 
            File outputDir = new File(args[1]);
162
 
            outputDir.mkdirs();
163
 
            ExtractReuters extractor = new ExtractReuters(reutersDir, outputDir);
164
 
            extractor.extract();
165
 
        }
166
 
        else
167
 
        {
168
 
            printUsage();
169
 
        }
170
 
    }
171
 
 
172
 
    private static void printUsage()
173
 
    {
174
 
        System.err.println("Usage: java -cp <...> org.apache.lucene.benchmark.utils.ExtractReuters <Path to Reuters SGM files> <Output Path>");
175
 
    }
176
 
}