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.
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.
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.
18
* CSVResultListener.java
19
* Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
24
package weka.experiment;
26
import weka.core.Option;
27
import weka.core.OptionHandler;
28
import weka.core.Utils;
30
import java.io.BufferedOutputStream;
32
import java.io.FileOutputStream;
33
import java.io.PrintWriter;
34
import java.util.Enumeration;
35
import java.util.Vector;
38
<!-- globalinfo-start -->
39
* Takes results from a result producer and assembles them into comma separated value form.
41
<!-- globalinfo-end -->
43
<!-- options-start -->
44
* Valid options are: <p/>
46
* <pre> -O <file name>
47
* The filename where output will be stored. Use - for stdout.
48
* (default temp file)</pre>
52
* @author Len Trigg (trigg@cs.waikato.ac.nz)
53
* @version $Revision: 1.12 $
55
public class CSVResultListener
56
implements ResultListener, OptionHandler {
58
/** for serialization */
59
static final long serialVersionUID = -623185072785174658L;
61
/** The ResultProducer sending us results */
62
protected ResultProducer m_RP;
64
/** The destination output file, null sends to System.out */
65
protected File m_OutputFile = null;
67
/** The name of the output file. Empty for temporary file. */
68
protected String m_OutputFileName = "";
70
/** The destination for results (typically connected to the output file) */
71
protected transient PrintWriter m_Out = new PrintWriter(System.out, true);
74
* Sets temporary file.
76
public CSVResultListener() {
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("-");
86
setOutputFile(resultsFile);
87
setOutputFileName("");
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
95
public String globalInfo() {
96
return "Takes results from a result producer and assembles them into "
97
+"comma separated value form.";
101
* Returns an enumeration describing the available options..
103
* @return an enumeration of all the available options.
105
public Enumeration listOptions() {
107
Vector newVector = new Vector(1);
109
newVector.addElement(new Option(
110
"\tThe filename where output will be stored. Use - for stdout.\n"
111
+"\t(default temp file)",
115
return newVector.elements();
119
* Parses a given list of options. <p/>
121
<!-- options-start -->
122
* Valid options are: <p/>
124
* <pre> -O <file name>
125
* The filename where output will be stored. Use - for stdout.
126
* (default temp file)</pre>
130
* @param options the list of options as an array of strings
131
* @throws Exception if an option is not supported
133
public void setOptions(String[] options) throws Exception {
135
String fName = Utils.getOption('O', options);
136
if (fName.length() != 0) {
137
setOutputFile(new File(fName));
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("-");
147
setOutputFile(resultsFile);
148
setOutputFileName("");
153
* Gets the current settings of the Classifier.
155
* @return an array of strings suitable for passing to setOptions
157
public String [] getOptions() {
159
String [] options = new String [2];
162
options[current++] = "-O";
163
options[current++] = getOutputFile().getName();
164
while (current < options.length) {
165
options[current++] = "";
171
* Returns the tip text for this property
172
* @return tip text for this property suitable for
173
* displaying in the explorer/experimenter gui
175
public String outputFileTipText() {
176
return "File to save to. Use '-' to write to standard out.";
180
* Get the value of OutputFile.
182
* @return Value of OutputFile.
184
public File getOutputFile() {
190
* Set the value of OutputFile. Also sets the
193
* @param newOutputFile Value to assign to OutputFile.
195
public void setOutputFile(File newOutputFile) {
197
m_OutputFile = newOutputFile;
198
setOutputFileName(newOutputFile.getName());
202
* Get the value of OutputFileName.
204
* @return Value of OutputFile.
206
public String outputFileName() {
208
return m_OutputFileName;
212
* Set the value of OutputFileName. Must be used
213
* AFTER setOutputFile.
215
* @param name the name of OutputFile.
217
public void setOutputFileName(String name) {
219
m_OutputFileName = name;
223
* Prepare for the results to be received.
225
* @param rp the ResultProducer that will generate the results
226
* @throws Exception if an error occurs during preprocessing.
228
public void preProcess(ResultProducer rp) throws Exception {
231
if ((m_OutputFile == null) || (m_OutputFile.getName().equals("-"))) {
232
m_Out = new PrintWriter(System.out, true);
234
m_Out = new PrintWriter(
235
new BufferedOutputStream(
236
new FileOutputStream(m_OutputFile)), true);
238
printResultNames(m_RP);
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
246
* @param rp the ResultProducer that generated the results
247
* @throws Exception if an error occurs
249
public void postProcess(ResultProducer rp) throws Exception {
251
if (!(m_OutputFile == null) && !(m_OutputFile.getName().equals("-"))) {
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.
267
public String [] determineColumnConstraints(ResultProducer rp) throws Exception {
272
* Just prints out each result as it is received.
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.
279
public void acceptResult(ResultProducer rp, Object[] key, Object[] result)
283
throw new Error("Unrecognized ResultProducer sending results!!");
285
for (int i = 0; i < key.length; i++) {
289
if (key[i] == null) {
292
m_Out.print(Utils.quote(key[i].toString()));
295
for (int i = 0; i < result.length; i++) {
297
if (result[i] == null) {
300
m_Out.print(Utils.quote(result[i].toString()));
307
* Always says a result is required. If this is the first call,
308
* prints out the header for the csv output.
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
316
public boolean isResultRequired(ResultProducer rp, Object[] key)
324
* Prints the names of each field out as the first row of the CSV output.
326
* @param rp the ResultProducer generating our results.
327
* @throws Exception if the field names could not be determined.
329
private void printResultNames(ResultProducer rp) throws Exception {
331
String [] key = rp.getKeyNames();
332
for (int i = 0; i < key.length; i++) {
336
if (key[i] == null) {
339
m_Out.print("Key_" + key[i].toString());
342
String [] result = rp.getResultNames();
343
for (int i = 0; i < result.length; i++) {
345
if (result[i] == null) {
348
m_Out.print(result[i].toString());
353
} // CSVResultListener