source: branches/MetisMQI/src/main/java/weka/core/stemmers/Stemming.java

Last change on this file was 29, checked in by gnappo, 15 years ago

Taggata versione per la demo e aggiunto branch.

File size: 5.3 KB
Line 
1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 * Stemming.java
19 * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.core.stemmers;
24
25import weka.core.Option;
26import weka.core.OptionHandler;
27import weka.core.RevisionHandler;
28import weka.core.RevisionUtils;
29import weka.core.Utils;
30
31import java.io.BufferedReader;
32import java.io.BufferedWriter;
33import java.io.FileInputStream;
34import java.io.FileOutputStream;
35import java.io.InputStreamReader;
36import java.io.OutputStreamWriter;
37import java.io.Reader;
38import java.io.Writer;
39import java.util.Enumeration;
40import java.util.Vector;
41
42/**
43 * A helper class for using the stemmers. Run with option '-h' to list
44 * all the available options.
45 *
46 * @author  FracPete (fracpete at waikato dot ac dot nz)
47 * @version $Revision: 5953 $
48 */
49public class Stemming
50  implements RevisionHandler {
51
52  /**
53   * lists all the options on the command line
54   *
55   * @param stemmer     the stemmer to list the parameters for
56   * @return            the option string
57   */
58  protected static String makeOptionsString(Stemmer stemmer) {
59    Vector<Option>          options;
60    Enumeration     enm;
61    StringBuffer    result;
62
63    options = new Vector<Option>();
64   
65    // general options
66    options.add(
67        new Option(
68          "\tDisplays this help.",
69          "h", 0, "-h"));
70
71    options.add(
72        new Option(
73          "\tThe file to process.",
74          "i", 1, "-i <input-file>"));
75
76    options.add(
77        new Option(
78          "\tThe file to output the processed data to (default stdout).",
79          "o", 1, "-o <output-file>"));
80
81    options.add(
82        new Option(
83          "\tUses lowercase strings.",
84          "l", 0, "-l"));
85
86    // stemmer options?
87    if (stemmer instanceof OptionHandler) {
88      enm = ((OptionHandler) stemmer).listOptions();
89      while (enm.hasMoreElements())
90        options.add((Option)enm.nextElement());
91    }
92
93    // print options
94    result = new StringBuffer();
95    result.append("\nStemmer options:\n\n");
96    enm = options.elements();
97    while (enm.hasMoreElements()) {
98      Option option = (Option) enm.nextElement();
99      result.append(option.synopsis() + "\n");
100      result.append(option.description() + "\n");
101    }
102
103    return result.toString();
104  }
105 
106  /**
107   * Applies the given stemmer according to the given options. '-h' lists
108   * all the available options for the given stemmer.
109   *
110   * @param stemmer     the stemmer to use
111   * @param options     the options for the stemmer
112   * @throws Exception  if something goes wrong
113   */
114  public static void useStemmer(Stemmer stemmer, String[] options) 
115    throws Exception {
116
117    Reader          reader;
118    StringBuffer    input;
119    Writer          output;
120    String          tmpStr;
121    boolean         lowerCase;
122   
123    // help?
124    if (Utils.getFlag('h', options)) {
125      System.out.println(makeOptionsString(stemmer));
126      return;
127    }
128
129    // input file
130    tmpStr = Utils.getOption('i', options);
131    if (tmpStr.length() == 0)
132      throw new IllegalArgumentException(
133          "No input file defined!" + makeOptionsString(stemmer));
134    else
135      reader = new BufferedReader(
136                  new InputStreamReader(new FileInputStream(tmpStr)));
137
138    input = new StringBuffer();
139
140    // output file?
141    tmpStr = Utils.getOption('o', options);
142    if (tmpStr.length() == 0)
143      output = new BufferedWriter(
144                  new OutputStreamWriter(System.out));
145    else
146      output = new BufferedWriter(
147                  new OutputStreamWriter(new FileOutputStream(tmpStr)));
148
149    // lowercase?
150    lowerCase = Utils.getFlag('l', options);
151
152    // stemmer options
153    if (stemmer instanceof OptionHandler)
154      ((OptionHandler) stemmer).setOptions(options);
155   
156    // unknown options?
157    try {
158      Utils.checkForRemainingOptions(options);
159    }
160    catch (Exception e) {
161      System.out.println(e.getMessage());
162      System.out.println(makeOptionsString(stemmer));
163      return;
164    }
165   
166    // process file
167    int character;
168    while ((character = reader.read()) != -1) {
169      char ch = (char) character;
170      if (Character.isWhitespace((char) ch)) {
171        if (input.length() > 0) {
172          output.write(stemmer.stem(input.toString()));
173          input = new StringBuffer();
174        }
175        output.write(ch);
176      } 
177      else {
178        if (lowerCase)
179          input.append(Character.toLowerCase(ch));
180        else
181          input.append(ch);
182      }
183    }
184    output.flush();
185  }
186 
187  /**
188   * Returns the revision string.
189   *
190   * @return            the revision
191   */
192  public String getRevision() {
193    return RevisionUtils.extract("$Revision: 5953 $");
194  }
195}
Note: See TracBrowser for help on using the repository browser.