~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/contrib/analyzers/common/src/java/org/tartarus/snowball/TestApp.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 
3
 
Copyright (c) 2001, Dr Martin Porter
4
 
Copyright (c) 2002, Richard Boulton
5
 
All rights reserved.
6
 
 
7
 
Redistribution and use in source and binary forms, with or without
8
 
modification, are permitted provided that the following conditions are met:
9
 
 
10
 
    * Redistributions of source code must retain the above copyright notice,
11
 
    * this list of conditions and the following disclaimer.
12
 
    * Redistributions in binary form must reproduce the above copyright
13
 
    * notice, this list of conditions and the following disclaimer in the
14
 
    * documentation and/or other materials provided with the distribution.
15
 
    * Neither the name of the copyright holders nor the names of its contributors
16
 
    * may be used to endorse or promote products derived from this software
17
 
    * without specific prior written permission.
18
 
 
19
 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
 
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
23
 
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
 
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
 
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
 
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
 
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
 
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
 
 
30
 
 */
31
 
 
32
 
package org.tartarus.snowball;
33
 
 
34
 
import java.lang.reflect.Method;
35
 
import java.io.Reader;
36
 
import java.io.Writer;
37
 
import java.io.BufferedReader;
38
 
import java.io.BufferedWriter;
39
 
import java.io.FileInputStream;
40
 
import java.io.InputStreamReader;
41
 
import java.io.OutputStreamWriter;
42
 
import java.io.OutputStream;
43
 
import java.io.FileOutputStream;
44
 
 
45
 
public class TestApp {
46
 
    private static void usage()
47
 
    {
48
 
        System.err.println("Usage: TestApp <algorithm> <input file> [-o <output file>]");
49
 
    }
50
 
 
51
 
    public static void main(String [] args) throws Throwable {
52
 
        if (args.length < 2) {
53
 
            usage();
54
 
            return;
55
 
        }
56
 
 
57
 
        Class<? extends SnowballProgram> stemClass = Class.forName("org.tartarus.snowball.ext." +
58
 
                                        args[0] + "Stemmer").asSubclass(SnowballProgram.class);
59
 
        SnowballProgram stemmer = stemClass.newInstance();
60
 
        Method stemMethod = stemClass.getMethod("stem", new Class[0]);
61
 
 
62
 
        Reader reader;
63
 
        reader = new InputStreamReader(new FileInputStream(args[1]));
64
 
        reader = new BufferedReader(reader);
65
 
 
66
 
        StringBuffer input = new StringBuffer();
67
 
 
68
 
        OutputStream outstream;
69
 
 
70
 
        if (args.length > 2) {
71
 
            if (args.length == 4 && args[2].equals("-o")) {
72
 
                outstream = new FileOutputStream(args[3]);
73
 
            } else {
74
 
                usage();
75
 
                return;
76
 
            }
77
 
        } else {
78
 
            outstream = System.out;
79
 
        }
80
 
        Writer output = new OutputStreamWriter(outstream);
81
 
        output = new BufferedWriter(output);
82
 
 
83
 
        int repeat = 1;
84
 
        if (args.length > 4) {
85
 
            repeat = Integer.parseInt(args[4]);
86
 
        }
87
 
 
88
 
        Object [] emptyArgs = new Object[0];
89
 
        int character;
90
 
        while ((character = reader.read()) != -1) {
91
 
            char ch = (char) character;
92
 
            if (Character.isWhitespace(ch)) {
93
 
                if (input.length() > 0) {
94
 
                    stemmer.setCurrent(input.toString());
95
 
                    for (int i = repeat; i != 0; i--) {
96
 
                        stemMethod.invoke(stemmer, emptyArgs);
97
 
                    }
98
 
                    output.write(stemmer.getCurrent());
99
 
                    output.write('\n');
100
 
                    input.delete(0, input.length());
101
 
                }
102
 
            } else {
103
 
                input.append(Character.toLowerCase(ch));
104
 
            }
105
 
        }
106
 
        output.flush();
107
 
    }
108
 
}