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

« back to all changes in this revision

Viewing changes to weka/core/converters/XRFFLoader.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
 * XRFFLoader.java
 
19
 * Copyright (C) 2006 University of Waikato, Hamilton, New Zealand
 
20
 *
 
21
 */
 
22
 
 
23
package weka.core.converters;
 
24
 
 
25
import weka.core.Instance;
 
26
import weka.core.Instances;
 
27
import weka.core.xml.XMLInstances;
 
28
 
 
29
import java.io.BufferedReader;
 
30
import java.io.File;
 
31
import java.io.FileInputStream;
 
32
import java.io.FileNotFoundException;
 
33
import java.io.IOException;
 
34
import java.io.InputStream;
 
35
import java.io.InputStreamReader;
 
36
import java.io.Reader;
 
37
import java.net.URL;
 
38
import java.util.zip.GZIPInputStream;
 
39
 
 
40
/**
 
41
 <!-- globalinfo-start -->
 
42
 * Reads a source that is in the XML version of the ARFF format. It automatically decompresses the data if the extension is '.xrff.gz'.
 
43
 * <p/>
 
44
 <!-- globalinfo-end -->
 
45
 *
 
46
 * @author FracPete (fracpete at waikato dot ac dot nz)
 
47
 * @version $Revision: 1.3 $
 
48
 * @see Loader
 
49
 */
 
50
public class XRFFLoader 
 
51
  extends AbstractFileLoader 
 
52
  implements BatchConverter, URLSourcedLoader {
 
53
 
 
54
  /** for serialization */
 
55
  private static final long serialVersionUID = 3764533621135196582L;
 
56
 
 
57
  /** the file extension */
 
58
  public static String FILE_EXTENSION = XMLInstances.FILE_EXTENSION;
 
59
 
 
60
  /** the extension for compressed files */
 
61
  public static String FILE_EXTENSION_COMPRESSED = FILE_EXTENSION + ".gz";
 
62
 
 
63
  /** the url */
 
64
  protected String m_URL = "http://";
 
65
 
 
66
  /** The reader for the source file. */
 
67
  protected transient Reader m_sourceReader = null;
 
68
 
 
69
  /** the loaded XML document */
 
70
  protected XMLInstances m_XMLInstances;
 
71
  
 
72
  /**
 
73
   * Returns a string describing this Loader
 
74
   * 
 
75
   * @return            a description of the Loader suitable for
 
76
   *                    displaying in the explorer/experimenter gui
 
77
   */
 
78
  public String globalInfo() {
 
79
    return 
 
80
        "Reads a source that is in the XML version of the ARFF format. "
 
81
      + "It automatically decompresses the data if the extension is '" 
 
82
      + FILE_EXTENSION_COMPRESSED + "'.";
 
83
  }
 
84
 
 
85
  /**
 
86
   * Get the file extension used for libsvm files
 
87
   *
 
88
   * @return            the file extension
 
89
   */
 
90
  public String getFileExtension() {
 
91
    return FILE_EXTENSION;
 
92
  }
 
93
 
 
94
  /**
 
95
   * Gets all the file extensions used for this type of file
 
96
   *
 
97
   * @return the file extensions
 
98
   */
 
99
  public String[] getFileExtensions() {
 
100
    return new String[]{FILE_EXTENSION, FILE_EXTENSION_COMPRESSED};
 
101
  }
 
102
 
 
103
  /**
 
104
   * Returns a description of the file type.
 
105
   *
 
106
   * @return            a short file description
 
107
   */
 
108
  public String getFileDescription() {
 
109
    return "XRFF data files";
 
110
  }
 
111
 
 
112
  /**
 
113
   * Resets the Loader ready to read a new data set
 
114
   * 
 
115
   * @throws IOException        if something goes wrong
 
116
   */
 
117
  public void reset() throws IOException {
 
118
    m_structure    = null;
 
119
    m_XMLInstances = null;
 
120
 
 
121
    setRetrieval(NONE);
 
122
    
 
123
    if ((m_File != null) && (new File(m_File)).isFile()) {
 
124
      setFile(new File(m_File));
 
125
    }
 
126
    else if ((m_URL != null) && !m_URL.equals("http://")) {
 
127
      setURL(m_URL);
 
128
    }
 
129
  }
 
130
 
 
131
  /**
 
132
   * Resets the Loader object and sets the source of the data set to be 
 
133
   * the supplied File object.
 
134
   *
 
135
   * @param file                the source file.
 
136
   * @throws IOException        if an error occurs
 
137
   */
 
138
  public void setSource(File file) throws IOException {
 
139
    m_structure    = null;
 
140
    m_XMLInstances = null;
 
141
    
 
142
    setRetrieval(NONE);
 
143
 
 
144
    if (file == null)
 
145
      throw new IOException("Source file object is null!");
 
146
 
 
147
    try {
 
148
      if (file.getName().endsWith(FILE_EXTENSION_COMPRESSED))
 
149
        setSource(new GZIPInputStream(new FileInputStream(file)));
 
150
      else
 
151
        setSource(new FileInputStream(file));
 
152
    }
 
153
    catch (FileNotFoundException ex) {
 
154
      throw new IOException("File not found");
 
155
    }
 
156
    
 
157
    m_sourceFile = file;
 
158
    m_File       = file.getAbsolutePath();
 
159
  }
 
160
 
 
161
  /**
 
162
   * Resets the Loader object and sets the source of the data set to be 
 
163
   * the supplied url.
 
164
   *
 
165
   * @param url         the source url.
 
166
   * @throws IOException        if an error occurs
 
167
   */
 
168
  public void setSource(URL url) throws IOException {
 
169
    m_structure    = null;
 
170
    m_XMLInstances = null;
 
171
    
 
172
    setRetrieval(NONE);
 
173
    
 
174
    setSource(url.openStream());
 
175
 
 
176
    m_URL = url.toString();
 
177
  }
 
178
  
 
179
  /**
 
180
   * Set the url to load from
 
181
   *
 
182
   * @param url                 the url to load from
 
183
   * @throws IOException        if the url can't be set.
 
184
   */
 
185
  public void setURL(String url) throws IOException {
 
186
    m_URL = url;
 
187
    setSource(new URL(url));
 
188
  }
 
189
 
 
190
  /**
 
191
   * Return the current url
 
192
   *
 
193
   * @return the current url
 
194
   */
 
195
  public String retrieveURL() {
 
196
    return m_URL;
 
197
  }
 
198
 
 
199
  /**
 
200
   * Resets the Loader object and sets the source of the data set to be 
 
201
   * the supplied InputStream.
 
202
   *
 
203
   * @param in                  the source InputStream.
 
204
   * @throws IOException        if initialization of reader fails.
 
205
   */
 
206
  public void setSource(InputStream in) throws IOException {
 
207
    m_File = (new File(System.getProperty("user.dir"))).getAbsolutePath();
 
208
    m_URL  = "http://";
 
209
 
 
210
    m_sourceReader = new BufferedReader(new InputStreamReader(in));
 
211
  }
 
212
  
 
213
  /**
 
214
   * Determines and returns (if possible) the structure (internally the 
 
215
   * header) of the data set as an empty set of instances.
 
216
   *
 
217
   * @return                    the structure of the data set as an empty set 
 
218
   *                            of Instances
 
219
   * @throws IOException        if an error occurs
 
220
   */
 
221
  public Instances getStructure() throws IOException {
 
222
    if (m_sourceReader == null)
 
223
      throw new IOException("No source has been specified");
 
224
 
 
225
    if (m_structure == null) {
 
226
      try {
 
227
        m_XMLInstances = new XMLInstances(m_sourceReader);
 
228
        m_structure    = new Instances(m_XMLInstances.getInstances(), 0);
 
229
      }
 
230
      catch (IOException ioe) {
 
231
        // just re-throw it
 
232
        throw ioe;
 
233
      }
 
234
      catch (Exception e) {
 
235
        throw new RuntimeException(e);
 
236
      }
 
237
    }
 
238
 
 
239
    return new Instances(m_structure, 0);
 
240
  }
 
241
  
 
242
  /**
 
243
   * Return the full data set. If the structure hasn't yet been determined
 
244
   * by a call to getStructure then method should do so before processing
 
245
   * the rest of the data set.
 
246
   *
 
247
   * @return                    the structure of the data set as an empty 
 
248
   *                            set of Instances
 
249
   * @throws IOException        if there is no source or parsing fails
 
250
   */
 
251
  public Instances getDataSet() throws IOException {
 
252
    if (m_sourceReader == null)
 
253
      throw new IOException("No source has been specified");
 
254
    
 
255
    if (getRetrieval() == INCREMENTAL)
 
256
      throw new IOException("Cannot mix getting Instances in both incremental and batch modes");
 
257
 
 
258
    setRetrieval(BATCH);
 
259
    if (m_structure == null)
 
260
      getStructure();
 
261
 
 
262
    return m_XMLInstances.getInstances();
 
263
  }
 
264
 
 
265
  /**
 
266
   * XRFFLoader is unable to process a data set incrementally.
 
267
   *
 
268
   * @param structure           ignored
 
269
   * @return                    never returns without throwing an exception
 
270
   * @throws IOException        always. XRFFLoader is unable to process a 
 
271
   *                            data set incrementally.
 
272
   */
 
273
  public Instance getNextInstance(Instances structure) throws IOException {
 
274
    throw new IOException("XRFFLoader can't read data sets incrementally.");
 
275
  }
 
276
 
 
277
  /**
 
278
   * Main method.
 
279
   *
 
280
   * @param args        should contain the name of an input file.
 
281
   */
 
282
  public static void main(String[] args) {
 
283
    runFileLoader(new XRFFLoader(), args);
 
284
  }
 
285
}