ArffLoader.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 6k
Category:

Windows Develop

Development Platform:

Java

  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.  *    ArffLoader.java
  18.  *    Copyright (C) 2000 Mark Hall
  19.  *
  20.  */
  21. package weka.core.converters;
  22. import java.io.BufferedReader;
  23. import java.io.File;
  24. import java.io.FileNotFoundException;
  25. import java.io.FileReader;
  26. import java.io.IOException;
  27. import java.io.Reader;
  28. import weka.core.Instance;
  29. import weka.core.Instances;
  30. import java.io.InputStream;
  31. import java.io.FileInputStream;
  32. import java.io.InputStreamReader;
  33. /**
  34.  * Reads a source that is in arff text format.
  35.  *
  36.  * @author Mark Hall (mhall@cs.waikato.ac.nz)
  37.  * @version $Revision: 1.3 $
  38.  * @see Loader
  39.  */
  40. public class ArffLoader extends AbstractLoader {
  41.   /**
  42.    * Holds the determined structure (header) of the data set.
  43.    */
  44.   //@ protected depends: model_structureDetermined -> m_structure;
  45.   //@ protected represents: model_structureDetermined <- (m_structure != null);
  46.   protected Instances m_structure = null;
  47.   /**
  48.    * The reader for the source file.
  49.    */
  50.   private transient Reader m_sourceReader = null;
  51.   /**
  52.    * Resets the Loader ready to read a new data set
  53.    */
  54.   public void reset() {
  55.     m_structure = null;
  56.     m_sourceReader = null;
  57.     setRetrieval(NONE);
  58.   }
  59.   /**
  60.    * Resets the Loader object and sets the source of the data set to be 
  61.    * the supplied File object.
  62.    *
  63.    * @param file the source file.
  64.    * @exception IOException if an error occurs
  65.    */
  66.   public void setSource(File file) throws IOException {
  67.     reset();
  68.     if (file == null) {
  69.       throw new IOException("Source file object is null!");
  70.     }
  71.     try {
  72.       setSource(new FileInputStream(file));
  73.     } catch (FileNotFoundException ex) {
  74.       throw new IOException("File not found");
  75.     }
  76.   }
  77.   /**
  78.    * Resets the Loader object and sets the source of the data set to be 
  79.    * the supplied InputStream.
  80.    *
  81.    * @param in the source InputStream.
  82.    * @exception IOException always thrown.
  83.    */
  84.   public void setSource(InputStream in) throws IOException {
  85.     m_sourceReader = new BufferedReader(new InputStreamReader(in));
  86.   }
  87.   /**
  88.    * Determines and returns (if possible) the structure (internally the 
  89.    * header) of the data set as an empty set of instances.
  90.    *
  91.    * @return the structure of the data set as an empty set of Instances
  92.    * @exception IOException if an error occurs
  93.    */
  94.   public Instances getStructure() throws IOException {
  95.     if (m_sourceReader == null) {
  96.       throw new IOException("No source has been specified");
  97.     }
  98.     if (m_structure == null) {
  99.       try {
  100. m_structure = new Instances(m_sourceReader, 1);
  101.       } catch (Exception ex) {
  102. throw new IOException("Unable to determine structure as arff.");
  103.       }
  104.     }
  105.     return new Instances(m_structure, 0);
  106.   }
  107.   /**
  108.    * Return the full data set. If the structure hasn't yet been determined
  109.    * by a call to getStructure then method should do so before processing
  110.    * the rest of the data set.
  111.    *
  112.    * @return the structure of the data set as an empty set of Instances
  113.    * @exception IOException if there is no source or parsing fails
  114.    */
  115.   public Instances getDataSet() throws IOException {
  116.     if (m_sourceReader == null) {
  117.       throw new IOException("No source has been specified");
  118.     }
  119.     if (getRetrieval() == INCREMENTAL) {
  120.       throw new IOException("Cannot mix getting Instances in both incremental and batch modes");
  121.     }
  122.     setRetrieval(BATCH);
  123.     
  124.     // Read all instances
  125.     // XXX This is inefficient because readInstance creates a new 
  126.     // StringTokenizer each time: This will be fixed once all arff reading
  127.     // is moved out of Instances and into this Loader.
  128.     while (m_structure.readInstance(m_sourceReader));
  129.     
  130.     return m_structure;
  131.   }
  132.   /**
  133.    * Read the data set incrementally---get the next instance in the data 
  134.    * set or returns null if there are no
  135.    * more instances to get. If the structure hasn't yet been 
  136.    * determined by a call to getStructure then method should do so before
  137.    * returning the next instance in the data set.
  138.    *
  139.    * @return the next instance in the data set as an Instance object or null
  140.    * if there are no more instances to be read
  141.    * @exception IOException if there is an error during parsing
  142.    */
  143.   public Instance getNextInstance() throws IOException {
  144.     if (m_structure == null) {
  145.       getStructure();
  146.     }
  147.     if (getRetrieval() == BATCH) {
  148.       throw new IOException("Cannot mix getting Instances in both incremental and batch modes");
  149.     }
  150.     setRetrieval(INCREMENTAL);
  151.     if (!m_structure.readInstance(m_sourceReader)) {
  152.       return null;
  153.     }
  154.    
  155.     Instance current = m_structure.instance(0);
  156.     m_structure.delete(0);
  157.     return current;
  158.   }
  159.   /**
  160.    * Main method.
  161.    *
  162.    * @param args should contain the name of an input file.
  163.    */
  164.   public static void main(String [] args) {
  165.     if (args.length > 0) {
  166.       File inputfile;
  167.       inputfile = new File(args[0]);
  168.       try {
  169. ArffLoader atf = new ArffLoader();
  170. atf.setSource(inputfile);
  171. System.out.println(atf.getStructure());
  172. Instance temp;
  173. do {
  174.   temp = atf.getNextInstance();
  175.   if (temp != null) {
  176.     System.out.println(temp);
  177.   }
  178. } while (temp != null);
  179.       } catch (Exception ex) {
  180. ex.printStackTrace();
  181. }
  182.     } else {
  183.       System.err.println("Usage:ntArffLoader <file.arff>n");
  184.     }
  185.   }
  186. }