01: package net.sf.snowball;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * 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.lang.reflect.Method;
21: import java.io.Reader;
22: import java.io.Writer;
23: import java.io.BufferedReader;
24: import java.io.BufferedWriter;
25: import java.io.FileInputStream;
26: import java.io.InputStreamReader;
27: import java.io.OutputStreamWriter;
28: import java.io.OutputStream;
29: import java.io.FileOutputStream;
30:
31: public class TestApp {
32: public static void main(String[] args) throws Throwable {
33:
34: if (args.length < 2) {
35: exitWithUsage();
36: }
37:
38: Class stemClass = Class.forName("net.sf.snowball.ext."
39: + args[0] + "Stemmer");
40: SnowballProgram stemmer = (SnowballProgram) stemClass
41: .newInstance();
42: Method stemMethod = stemClass.getMethod("stem", new Class[0]);
43:
44: Reader reader;
45: reader = new InputStreamReader(new FileInputStream(args[1]));
46: reader = new BufferedReader(reader);
47:
48: StringBuffer input = new StringBuffer();
49:
50: OutputStream outstream = System.out;
51:
52: if (args.length > 2 && args[2].equals("-o")) {
53: outstream = new FileOutputStream(args[3]);
54: } else if (args.length > 2) {
55: exitWithUsage();
56: }
57:
58: Writer output = new OutputStreamWriter(outstream);
59: output = new BufferedWriter(output);
60:
61: int repeat = 1;
62: if (args.length > 4) {
63: repeat = Integer.parseInt(args[4]);
64: }
65:
66: Object[] emptyArgs = new Object[0];
67: int character;
68: while ((character = reader.read()) != -1) {
69: char ch = (char) character;
70: if (Character.isWhitespace(ch)) {
71: if (input.length() > 0) {
72: stemmer.setCurrent(input.toString());
73: for (int i = repeat; i != 0; i--) {
74: stemMethod.invoke(stemmer, emptyArgs);
75: }
76: output.write(stemmer.getCurrent());
77: output.write('\n');
78: input.delete(0, input.length());
79: }
80: } else {
81: input.append(Character.toLowerCase(ch));
82: }
83: }
84: output.flush();
85: }
86:
87: private static void exitWithUsage() {
88: System.err
89: .println("Usage: TestApp <stemmer name> <input file> [-o <output file>]");
90: System.exit(1);
91: }
92: }
|