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