~ubuntu-branches/ubuntu/precise/weka/precise

« back to all changes in this revision

Viewing changes to weka/experiment/CSVResultListener.java

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2008-02-24 09:18:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080224091845-1l8zy6fm6xipbzsr
Tags: upstream-3.5.7+tut1
ImportĀ upstreamĀ versionĀ 3.5.7+tut1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 *    CSVResultListener.java
 
19
 *    Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
 
20
 *
 
21
 */
 
22
 
 
23
 
 
24
package weka.experiment;
 
25
 
 
26
import weka.core.Option;
 
27
import weka.core.OptionHandler;
 
28
import weka.core.Utils;
 
29
 
 
30
import java.io.BufferedOutputStream;
 
31
import java.io.File;
 
32
import java.io.FileOutputStream;
 
33
import java.io.PrintWriter;
 
34
import java.util.Enumeration;
 
35
import java.util.Vector;
 
36
 
 
37
/**
 
38
 <!-- globalinfo-start -->
 
39
 * Takes results from a result producer and assembles them into comma separated value form.
 
40
 * <p/>
 
41
 <!-- globalinfo-end -->
 
42
 *
 
43
 <!-- options-start -->
 
44
 * Valid options are: <p/>
 
45
 * 
 
46
 * <pre> -O &lt;file name&gt;
 
47
 *  The filename where output will be stored. Use - for stdout.
 
48
 *  (default temp file)</pre>
 
49
 * 
 
50
 <!-- options-end -->
 
51
 * 
 
52
 * @author Len Trigg (trigg@cs.waikato.ac.nz)
 
53
 * @version $Revision: 1.12 $
 
54
 */
 
55
public class CSVResultListener 
 
56
  implements ResultListener, OptionHandler {
 
57
  
 
58
  /** for serialization */
 
59
  static final long serialVersionUID = -623185072785174658L;
 
60
 
 
61
  /** The ResultProducer sending us results */
 
62
  protected ResultProducer m_RP;
 
63
 
 
64
  /** The destination output file, null sends to System.out */
 
65
  protected File m_OutputFile = null;
 
66
 
 
67
  /** The name of the output file. Empty for temporary file. */
 
68
  protected String m_OutputFileName = "";
 
69
 
 
70
  /** The destination for results (typically connected to the output file) */
 
71
  protected transient PrintWriter m_Out = new PrintWriter(System.out, true);
 
72
 
 
73
  /** 
 
74
   * Sets temporary file.
 
75
   */
 
76
  public CSVResultListener() {
 
77
 
 
78
    File resultsFile;
 
79
    try {
 
80
      resultsFile = File.createTempFile("weka_experiment", ".csv");
 
81
      resultsFile.deleteOnExit();
 
82
    } catch (Exception e) {
 
83
      System.err.println("Cannot create temp file, writing to standard out.");
 
84
      resultsFile = new File("-");
 
85
    }
 
86
    setOutputFile(resultsFile);
 
87
    setOutputFileName("");
 
88
  }
 
89
 
 
90
  /**
 
91
   * Returns a string describing this result listener
 
92
   * @return a description of the result listener suitable for
 
93
   * displaying in the explorer/experimenter gui
 
94
   */
 
95
  public String globalInfo() {
 
96
    return "Takes results from a result producer and assembles them into "
 
97
      +"comma separated value form.";
 
98
  }
 
99
 
 
100
  /**
 
101
   * Returns an enumeration describing the available options..
 
102
   *
 
103
   * @return an enumeration of all the available options.
 
104
   */
 
105
  public Enumeration listOptions() {
 
106
 
 
107
    Vector newVector = new Vector(1);
 
108
 
 
109
    newVector.addElement(new Option(
 
110
             "\tThe filename where output will be stored. Use - for stdout.\n"
 
111
              +"\t(default temp file)", 
 
112
             "O", 1, 
 
113
             "-O <file name>"));
 
114
 
 
115
    return newVector.elements();
 
116
  }
 
117
 
 
118
  /**
 
119
   * Parses a given list of options. <p/>
 
120
   *
 
121
   <!-- options-start -->
 
122
   * Valid options are: <p/>
 
123
   * 
 
124
   * <pre> -O &lt;file name&gt;
 
125
   *  The filename where output will be stored. Use - for stdout.
 
126
   *  (default temp file)</pre>
 
127
   * 
 
128
   <!-- options-end -->
 
129
   *
 
130
   * @param options the list of options as an array of strings
 
131
   * @throws Exception if an option is not supported
 
132
   */
 
133
  public void setOptions(String[] options) throws Exception {
 
134
    
 
135
    String fName = Utils.getOption('O', options);
 
136
    if (fName.length() != 0) {
 
137
      setOutputFile(new File(fName));
 
138
    } else {
 
139
      File resultsFile;
 
140
      try {
 
141
        resultsFile = File.createTempFile("weka_experiment", null);
 
142
        resultsFile.deleteOnExit();
 
143
      } catch (Exception e) {
 
144
        System.err.println("Cannot create temp file, writing to standard out.");
 
145
        resultsFile = new File("-");
 
146
      }
 
147
      setOutputFile(resultsFile);
 
148
      setOutputFileName("");
 
149
    }
 
150
  }
 
151
 
 
152
  /**
 
153
   * Gets the current settings of the Classifier.
 
154
   *
 
155
   * @return an array of strings suitable for passing to setOptions
 
156
   */
 
157
  public String [] getOptions() {
 
158
 
 
159
    String [] options = new String [2];
 
160
    int current = 0;
 
161
 
 
162
    options[current++] = "-O";
 
163
    options[current++] = getOutputFile().getName();
 
164
    while (current < options.length) {
 
165
      options[current++] = "";
 
166
    }
 
167
    return options;
 
168
  }
 
169
 
 
170
  /**
 
171
   * Returns the tip text for this property
 
172
   * @return tip text for this property suitable for
 
173
   * displaying in the explorer/experimenter gui
 
174
   */
 
175
  public String outputFileTipText() {
 
176
    return "File to save to. Use '-' to write to standard out.";
 
177
  }
 
178
 
 
179
  /**
 
180
   * Get the value of OutputFile.
 
181
   *
 
182
   * @return Value of OutputFile.
 
183
   */
 
184
  public File getOutputFile() {
 
185
    
 
186
    return m_OutputFile;
 
187
  }
 
188
  
 
189
  /**
 
190
   * Set the value of OutputFile. Also sets the
 
191
   * OutputFileName.
 
192
   *
 
193
   * @param newOutputFile Value to assign to OutputFile.
 
194
   */
 
195
  public void setOutputFile(File newOutputFile) {
 
196
    
 
197
    m_OutputFile = newOutputFile;
 
198
    setOutputFileName(newOutputFile.getName());
 
199
  }
 
200
 
 
201
  /**
 
202
   * Get the value of OutputFileName.
 
203
   *
 
204
   * @return Value of OutputFile.
 
205
   */
 
206
  public String outputFileName() {
 
207
    
 
208
    return m_OutputFileName;
 
209
  }
 
210
 
 
211
  /**
 
212
   * Set the value of OutputFileName. Must be used
 
213
   * AFTER setOutputFile.
 
214
   *
 
215
   * @param name the name of OutputFile.
 
216
   */
 
217
  public void setOutputFileName(String name) {
 
218
    
 
219
    m_OutputFileName = name;
 
220
  }
 
221
  
 
222
  /**
 
223
   * Prepare for the results to be received.
 
224
   *
 
225
   * @param rp the ResultProducer that will generate the results
 
226
   * @throws Exception if an error occurs during preprocessing.
 
227
   */
 
228
  public void preProcess(ResultProducer rp) throws Exception {
 
229
 
 
230
    m_RP = rp;
 
231
    if ((m_OutputFile == null) || (m_OutputFile.getName().equals("-"))) {
 
232
      m_Out = new PrintWriter(System.out, true);
 
233
    } else {
 
234
      m_Out = new PrintWriter(
 
235
              new BufferedOutputStream(
 
236
              new FileOutputStream(m_OutputFile)), true);
 
237
    }
 
238
    printResultNames(m_RP);
 
239
  }
 
240
  
 
241
  /**
 
242
   * Perform any postprocessing. When this method is called, it indicates
 
243
   * that no more results will be sent that need to be grouped together
 
244
   * in any way.
 
245
   *
 
246
   * @param rp the ResultProducer that generated the results
 
247
   * @throws Exception if an error occurs
 
248
   */
 
249
  public void postProcess(ResultProducer rp) throws Exception {
 
250
    
 
251
    if (!(m_OutputFile == null) && !(m_OutputFile.getName().equals("-"))) {
 
252
      m_Out.close();
 
253
    }
 
254
  }
 
255
 
 
256
  /**
 
257
   * Determines if there are any constraints (imposed by the
 
258
   * destination) on the result columns to be produced by
 
259
   * resultProducers. Null should be returned if there are NO
 
260
   * constraints, otherwise a list of column names should be
 
261
   * returned as an array of Strings.
 
262
   * @param rp the ResultProducer to which the constraints will apply
 
263
   * @return an array of column names to which resutltProducer's
 
264
   * results will be restricted.
 
265
   * @throws Exception if an error occurs.
 
266
   */
 
267
  public String [] determineColumnConstraints(ResultProducer rp) throws Exception {
 
268
    return null;
 
269
  }
 
270
 
 
271
  /**
 
272
   * Just prints out each result as it is received.
 
273
   *
 
274
   * @param rp the ResultProducer that generated the result
 
275
   * @param key The key for the results.
 
276
   * @param result The actual results.
 
277
   * @throws Exception if the result could not be accepted.
 
278
   */
 
279
  public void acceptResult(ResultProducer rp, Object[] key, Object[] result) 
 
280
    throws Exception {
 
281
 
 
282
    if (m_RP != rp) {
 
283
      throw new Error("Unrecognized ResultProducer sending results!!");
 
284
    }
 
285
    for (int i = 0; i < key.length; i++) {
 
286
      if (i != 0) {
 
287
        m_Out.print(',');
 
288
      }
 
289
      if (key[i] == null) {
 
290
        m_Out.print("?");
 
291
      } else {
 
292
        m_Out.print(Utils.quote(key[i].toString()));
 
293
      }
 
294
    }
 
295
    for (int i = 0; i < result.length; i++) {
 
296
      m_Out.print(',');
 
297
      if (result[i] == null) {
 
298
        m_Out.print("?");
 
299
      } else {
 
300
        m_Out.print(Utils.quote(result[i].toString()));
 
301
      }
 
302
    }
 
303
    m_Out.println("");
 
304
  }
 
305
 
 
306
  /**
 
307
   * Always says a result is required. If this is the first call,
 
308
   * prints out the header for the csv output.
 
309
   *
 
310
   * @param rp the ResultProducer wanting to generate the result
 
311
   * @param key The key for which a result may be needed.
 
312
   * @return true if the result should be calculated.
 
313
   * @throws Exception if it could not be determined if the result 
 
314
   * is needed.
 
315
   */
 
316
  public boolean isResultRequired(ResultProducer rp, Object[] key) 
 
317
    throws Exception {
 
318
 
 
319
    return true;
 
320
  }
 
321
 
 
322
 
 
323
  /**
 
324
   * Prints the names of each field out as the first row of the CSV output.
 
325
   *
 
326
   * @param rp the ResultProducer generating our results.
 
327
   * @throws Exception if the field names could not be determined.
 
328
   */
 
329
  private void printResultNames(ResultProducer rp) throws Exception {
 
330
 
 
331
    String [] key = rp.getKeyNames();
 
332
    for (int i = 0; i < key.length; i++) {
 
333
      if (i != 0) {
 
334
        m_Out.print(',');
 
335
      }
 
336
      if (key[i] == null) {
 
337
        m_Out.print("?");
 
338
      } else {
 
339
        m_Out.print("Key_" + key[i].toString());
 
340
      }
 
341
    }
 
342
    String [] result = rp.getResultNames();
 
343
    for (int i = 0; i < result.length; i++) {
 
344
      m_Out.print(',');
 
345
      if (result[i] == null) {
 
346
        m_Out.print("?");
 
347
      } else {
 
348
        m_Out.print(result[i].toString());
 
349
      }
 
350
    }
 
351
    m_Out.println("");
 
352
  }
 
353
} // CSVResultListener