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

« back to all changes in this revision

Viewing changes to weka/core/converters/TextDirectoryLoader.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
 * TextDirectoryLoader.java
 
19
 * Copyright (C) 2006 University of Waikato, Hamilton, New Zealand
 
20
 *
 
21
 */
 
22
 
 
23
package weka.core.converters;
 
24
 
 
25
import weka.core.Attribute;
 
26
import weka.core.FastVector;
 
27
import weka.core.Instance;
 
28
import weka.core.Instances;
 
29
import weka.core.Option;
 
30
import weka.core.OptionHandler;
 
31
import weka.core.Utils;
 
32
 
 
33
import java.io.BufferedInputStream;
 
34
import java.io.File;
 
35
import java.io.FileInputStream;
 
36
import java.io.IOException;
 
37
import java.util.Enumeration;
 
38
import java.util.Vector;
 
39
 
 
40
/**
 
41
 <!-- globalinfo-start -->
 
42
 * Loads all text files in a directory and uses the subdirectory names as class labels. The content of the text files will be stored in a String attribute, the filename can be stored as well.
 
43
 * <p/>
 
44
 <!-- globalinfo-end -->
 
45
 *
 
46
 <!-- options-start -->
 
47
 * Valid options are: <p/>
 
48
 * 
 
49
 * <pre> -D
 
50
 *  Enables debug output.
 
51
 *  (default: off)</pre>
 
52
 * 
 
53
 * <pre> -F
 
54
 *  Stores the filename in an additional attribute.
 
55
 *  (default: off)</pre>
 
56
 * 
 
57
 * <pre> -dir &lt;directory&gt;
 
58
 *  The directory to work on.
 
59
 *  (default: current directory)</pre>
 
60
 * 
 
61
 <!-- options-end -->
 
62
 *
 
63
 * Based on code from the TextDirectoryToArff tool:
 
64
 * <ul>
 
65
 *    <li><a href="http://list.scms.waikato.ac.nz/mailman/htdig/wekalist/2002-October/000681.html" target="_blank">Original tool</a></li>
 
66
 *    <li><a href="http://list.scms.waikato.ac.nz/mailman/htdig/wekalist/2004-January/002152.html" target="_blank">Current version</a></li>
 
67
 *    <li><a href="http://weka.sourceforge.net/wiki/index.php/ARFF_files_from_Text_Collections" target="_blank">Wiki article</a></li>
 
68
 * </ul>
 
69
 *
 
70
 * @author Ashraf M. Kibriya (amk14 at cs.waikato.ac.nz)
 
71
 * @author Richard Kirkby (rkirkby at cs.waikato.ac.nz)
 
72
 * @author fracpete (fracpete at waikato dot ac dot nz)
 
73
 * @version $Revision: 1.2 $
 
74
 * @see Loader
 
75
 */
 
76
public class TextDirectoryLoader
 
77
  extends AbstractLoader
 
78
  implements BatchConverter, OptionHandler {
 
79
  
 
80
  /** for serialization */
 
81
  private static final long serialVersionUID = 2592118773712247647L;
 
82
  
 
83
  /** Holds the determined structure (header) of the data set. */
 
84
  protected Instances m_structure = null;
 
85
  
 
86
  /** Holds the source of the data set. */
 
87
  protected File m_sourceFile = new File(System.getProperty("user.dir"));
 
88
  
 
89
  /** whether to print some debug information */
 
90
  protected boolean m_Debug = false;
 
91
  
 
92
  /** whether to include the filename as an extra attribute */
 
93
  protected boolean m_OutputFilename = false;
 
94
  
 
95
  /**
 
96
   * default constructor
 
97
   */
 
98
  public TextDirectoryLoader() {
 
99
    // No instances retrieved yet
 
100
    setRetrieval(NONE);
 
101
  }
 
102
  
 
103
  /**
 
104
   * Returns a string describing this loader
 
105
   * 
 
106
   * @return            a description of the evaluator suitable for
 
107
   *                    displaying in the explorer/experimenter gui
 
108
   */
 
109
  public String globalInfo() {
 
110
    return 
 
111
        "Loads all text files in a directory and uses the subdirectory names "
 
112
      + "as class labels. The content of the text files will be stored in a "
 
113
      + "String attribute, the filename can be stored as well.";
 
114
  }
 
115
  
 
116
  /** 
 
117
   * Lists the available options
 
118
   * 
 
119
   * @return            an enumeration of the available options
 
120
   */  
 
121
  public Enumeration listOptions() {
 
122
    
 
123
    Vector result = new Vector();
 
124
    
 
125
    result.add(new Option(
 
126
        "\tEnables debug output.\n"
 
127
        + "\t(default: off)",
 
128
        "D", 0, "-D"));
 
129
    
 
130
    result.add(new Option(
 
131
        "\tStores the filename in an additional attribute.\n"
 
132
        + "\t(default: off)",
 
133
        "F", 0, "-F"));
 
134
    
 
135
    result.add(new Option(
 
136
        "\tThe directory to work on.\n"
 
137
        + "\t(default: current directory)",
 
138
        "dir", 0, "-dir <directory>"));
 
139
    
 
140
    return  result.elements();
 
141
  }
 
142
  
 
143
  /** 
 
144
   * Parses a given list of options. <p/>
 
145
   *
 
146
   <!-- options-start -->
 
147
   * Valid options are: <p/>
 
148
   * 
 
149
   * <pre> -D
 
150
   *  Enables debug output.
 
151
   *  (default: off)</pre>
 
152
   * 
 
153
   * <pre> -F
 
154
   *  Stores the filename in an additional attribute.
 
155
   *  (default: off)</pre>
 
156
   * 
 
157
   * <pre> -dir &lt;directory&gt;
 
158
   *  The directory to work on.
 
159
   *  (default: current directory)</pre>
 
160
   * 
 
161
   <!-- options-end -->
 
162
   *
 
163
   * @param options the options
 
164
   * @throws Exception if options cannot be set
 
165
   */  
 
166
  public void setOptions(String[] options) throws Exception {
 
167
    setDebug(Utils.getFlag("D", options));
 
168
    
 
169
    setOutputFilename(Utils.getFlag("F", options));
 
170
    
 
171
    setDirectory(new File(Utils.getOption("dir", options)));
 
172
  }
 
173
  
 
174
  /** 
 
175
   * Gets the setting
 
176
   * 
 
177
   * @return the current setting
 
178
   */  
 
179
  public String[] getOptions() {
 
180
    Vector options = new Vector();
 
181
    
 
182
    if (getDebug())
 
183
      options.add("-D");
 
184
    
 
185
    if (getOutputFilename())
 
186
      options.add("-F");
 
187
 
 
188
    options.add("-dir");
 
189
    options.add(getDirectory().getAbsolutePath());
 
190
    
 
191
    return (String[]) options.toArray(new String[options.size()]);
 
192
  }
 
193
  
 
194
  /**
 
195
   * Sets whether to print some debug information.
 
196
   * 
 
197
   * @param value       if true additional debug information will be printed.
 
198
   */
 
199
  public void setDebug(boolean value) {
 
200
    m_Debug = value;
 
201
  }
 
202
  
 
203
  /**
 
204
   * Gets whether additional debug information is printed.
 
205
   * 
 
206
   * @return            true if additional debug information is printed
 
207
   */
 
208
  public boolean getDebug() {
 
209
    return m_Debug;
 
210
  }
 
211
  
 
212
  /**
 
213
   * the tip text for this property
 
214
   * 
 
215
   * @return            the tip text
 
216
   */
 
217
  public String debugTipText(){
 
218
    return "Whether to print additional debug information to the console.";
 
219
  }
 
220
  
 
221
  /**
 
222
   * Sets whether the filename will be stored as an extra attribute.
 
223
   * 
 
224
   * @param value       if true the filename will be stored in an extra
 
225
   *                    attribute
 
226
   */
 
227
  public void setOutputFilename(boolean value) {
 
228
    m_OutputFilename = value;
 
229
    reset();
 
230
  }
 
231
  
 
232
  /**
 
233
   * Gets whether the filename will be stored as an extra attribute.
 
234
   * 
 
235
   * @return            true if the filename is stored in an extra attribute
 
236
   */
 
237
  public boolean getOutputFilename() {
 
238
    return m_OutputFilename;
 
239
  }
 
240
  
 
241
  /**
 
242
   * the tip text for this property
 
243
   * 
 
244
   * @return            the tip text
 
245
   */
 
246
  public String outputFilenameTipText(){
 
247
    return "Whether to store the filename in an additional attribute.";
 
248
  }
 
249
  
 
250
  /**
 
251
   * Returns a description of the file type, actually it's directories.
 
252
   *
 
253
   * @return            a short file description
 
254
   */
 
255
  public String getFileDescription() {
 
256
    return "Directories";
 
257
  }
 
258
  
 
259
  /**
 
260
   * get the Dir specified as the source
 
261
   *
 
262
   * @return            the source directory
 
263
   */
 
264
  public File getDirectory() {
 
265
    return new File(m_sourceFile.getAbsolutePath());
 
266
  }
 
267
  
 
268
  /**
 
269
   * sets the source directory
 
270
   *
 
271
   * @param                     dir the source directory
 
272
   * @throws IOException        if an error occurs
 
273
   */
 
274
  public void setDirectory(File dir) throws IOException {
 
275
    setSource(dir);
 
276
  }
 
277
  
 
278
  /**
 
279
   * Resets the loader ready to read a new data set
 
280
   */
 
281
  public void reset() {
 
282
    m_structure = null;
 
283
    setRetrieval(NONE);
 
284
  }
 
285
  
 
286
  /**
 
287
   * Resets the Loader object and sets the source of the data set to be 
 
288
   * the supplied File object.
 
289
   *
 
290
   * @param dir                 the source directory.
 
291
   * @throws IOException        if an error occurs
 
292
   */
 
293
  public void setSource(File dir) throws IOException {
 
294
    reset();
 
295
    
 
296
    if (dir == null) {
 
297
      throw new IOException("Source directory object is null!");
 
298
    }
 
299
    
 
300
    m_sourceFile = dir;
 
301
    if (!dir.exists() || !dir.isDirectory())
 
302
      throw new IOException("Directory '" + dir + "' not found");
 
303
  }
 
304
  
 
305
  /**
 
306
   * Determines and returns (if possible) the structure (internally the 
 
307
   * header) of the data set as an empty set of instances.
 
308
   *
 
309
   * @return                    the structure of the data set as an empty 
 
310
   *                            set of Instances
 
311
   * @throws IOException        if an error occurs
 
312
   */
 
313
  public Instances getStructure() throws IOException {
 
314
    if (getDirectory() == null) {
 
315
      throw new IOException("No directory/source has been specified");
 
316
    }
 
317
    
 
318
    // determine class labels, i.e., sub-dirs
 
319
    if (m_structure == null) {
 
320
      String directoryPath = getDirectory().getAbsolutePath();
 
321
      FastVector atts = new FastVector();
 
322
      FastVector classes = new FastVector();
 
323
      
 
324
      File dir = new File(directoryPath);
 
325
      String[] subdirs = dir.list();
 
326
      
 
327
      for (int i = 0; i < subdirs.length; i++) {
 
328
        File subdir = new File(directoryPath + File.separator + subdirs[i]);
 
329
        if (subdir.isDirectory())
 
330
          classes.addElement(subdirs[i]);
 
331
      }
 
332
      
 
333
      atts.addElement(new Attribute("text", (FastVector) null));
 
334
      if (m_OutputFilename)
 
335
        atts.addElement(new Attribute("filename", (FastVector) null));
 
336
      atts.addElement(new Attribute("class", classes));
 
337
      
 
338
      String relName = directoryPath.replaceAll("/", "_");
 
339
      relName = relName.replaceAll("\\\\", "_").replaceAll(":", "_");
 
340
      m_structure = new Instances(relName, atts, 0);    
 
341
      m_structure.setClassIndex(m_structure.numAttributes() - 1);
 
342
    }
 
343
    
 
344
    return m_structure;
 
345
  }
 
346
  
 
347
  /**
 
348
   * Return the full data set. If the structure hasn't yet been determined
 
349
   * by a call to getStructure then method should do so before processing
 
350
   * the rest of the data set.
 
351
   *
 
352
   * @return the structure of the data set as an empty set of Instances
 
353
   * @throws IOException if there is no source or parsing fails
 
354
   */
 
355
  public Instances getDataSet() throws IOException {
 
356
    if (getDirectory() == null)
 
357
      throw new IOException("No directory/source has been specified");
 
358
    
 
359
    String directoryPath = getDirectory().getAbsolutePath();
 
360
    FastVector classes = new FastVector();
 
361
    Enumeration enm = getStructure().classAttribute().enumerateValues();
 
362
    while (enm.hasMoreElements())
 
363
      classes.addElement(enm.nextElement());
 
364
    
 
365
    Instances data = getStructure();
 
366
    int fileCount = 0;
 
367
    for (int k = 0; k < classes.size(); k++) {
 
368
      String subdirPath = (String) classes.elementAt(k);
 
369
      File subdir = new File(directoryPath + File.separator + subdirPath);
 
370
      String[] files = subdir.list();
 
371
      for (int j = 0; j < files.length; j++) {
 
372
        try {
 
373
          fileCount++;
 
374
          if (getDebug())
 
375
            System.err.println(
 
376
                "processing " + fileCount + " : " + subdirPath + " : " + files[j]); 
 
377
          
 
378
          double[] newInst = null;
 
379
          if (m_OutputFilename)
 
380
            newInst = new double[3];
 
381
          else
 
382
            newInst = new double[2];                
 
383
          File txt = new File(directoryPath + File.separator + subdirPath + File.separator + files[j]);
 
384
          BufferedInputStream is;
 
385
          is = new BufferedInputStream(new FileInputStream(txt));
 
386
          StringBuffer txtStr = new StringBuffer();
 
387
          int c;
 
388
          while ((c = is.read()) != -1) {
 
389
            txtStr.append((char) c);
 
390
          }
 
391
          
 
392
          newInst[0] = (double) data.attribute(0).addStringValue(txtStr.toString());
 
393
          if (m_OutputFilename)
 
394
            newInst[1] = (double) data.attribute(1).addStringValue(subdirPath + File.separator + files[j]);
 
395
          newInst[data.classIndex()] = (double) k;
 
396
          data.add(new Instance(1.0, newInst));
 
397
        }
 
398
        catch (Exception e) {
 
399
          System.err.println("failed to convert file: " + directoryPath + File.separator + subdirPath + File.separator + files[j]);
 
400
        }
 
401
      }
 
402
    }
 
403
    
 
404
    return data;
 
405
  }
 
406
  
 
407
  /**
 
408
   * TextDirectoryLoader is unable to process a data set incrementally.
 
409
   *
 
410
   * @param structure ignored
 
411
   * @return never returns without throwing an exception
 
412
   * @throws IOException always. TextDirectoryLoader is unable to process a data
 
413
   * set incrementally.
 
414
   */
 
415
  public Instance getNextInstance(Instances structure) throws IOException {
 
416
    throw new IOException("TextDirectoryLoader can't read data sets incrementally.");
 
417
  }
 
418
  
 
419
  /**
 
420
   * Main method.
 
421
   *
 
422
   * @param args should contain the name of an input file.
 
423
   */
 
424
  public static void main(String[] args) {
 
425
    if (args.length > 0) {
 
426
      try {
 
427
        TextDirectoryLoader loader = new TextDirectoryLoader();
 
428
        loader.setOptions(args);
 
429
        System.out.println(loader.getDataSet());
 
430
      } 
 
431
      catch (Exception e) {
 
432
        e.printStackTrace();
 
433
      }
 
434
    } 
 
435
    else {
 
436
      System.err.println(
 
437
          "\nUsage:\n" 
 
438
          + "\tTextDirectoryLoader [options]\n"
 
439
          + "\n"
 
440
          + "Options:\n");
 
441
 
 
442
      Enumeration enm = ((OptionHandler) new TextDirectoryLoader()).listOptions();
 
443
      while (enm.hasMoreElements()) {
 
444
        Option option = (Option) enm.nextElement();
 
445
        System.err.println(option.synopsis());
 
446
        System.err.println(option.description());
 
447
      }
 
448
      
 
449
      System.err.println();
 
450
    }
 
451
  }
 
452
}