📄 loadfromfile.java
字号:
/* * $RCSfile: LoadFromFile.java,v $ * $Revision: 1.5 $ * $Date: 2005/05/03 14:39:14 $ * * NeuralNetworkToolkit * Copyright (C) 2004 Universidade de Brasília * * This file is part of NeuralNetworkToolkit. * * NeuralNetworkToolkit is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * NeuralNetworkToolkit is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with NeuralNetworkToolkit; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 - USA. */package neuralnetworktoolkit.datamanager.textfilemanager;import java.io.*;import java.util.*;/** * Provides data loading from text files. The files to be readed * must have a first line with attributes names separated by a separator * (user defined); the other lines must have data values separated by the * same separator, one instance by line. All data values must be real * (<code>float</code> or <code>double</code>). * * @version 1.0 21 Jun 2004 * * @author <a href="mailto:rodbra@pop.com.br">Rodrigo C. M. Coimbra</a> * @author <a href="mailto:hugoiver@yahoo.com.br">Hugo Iver V. Gon???alves</a> */public class LoadFromFile { /** Stores all data values. */ private double[][] dataValues; /** Stores only expected output data. */ private double[][] expectedOutput; /** Stores only input data. */ private double[][] inputData; /** Stores attributes names. */ private String[] dataHeader; private ResourceBundle resource; /** * * @param file * @param separator * * @throws LoadFromFileException */ public LoadFromFile(File file, String separator) throws LoadFromFileException { String readedDataHeader; String dataLine; String[] temp; Object[] readedDataLines; Vector readedDataValues; FileReader inputReader; BufferedReader inputBuffer; int line = 2; try { resource = ResourceBundle .getBundle("neuralnetworktoolkit.datamanager.textfilemanager.resources.LoadFromFileResource"); inputReader = new FileReader(file); inputBuffer = new BufferedReader(inputReader); readedDataHeader = inputBuffer.readLine(); readedDataValues = new Vector(); separator = separator.replaceAll("\\|","\\\\|"); separator = separator.replaceAll("\\$","\\\\\\$"); separator = separator.replaceAll("\\(","\\\\("); separator = separator.replaceAll("\\)","\\\\)"); separator = separator.replaceAll("\\*","\\\\*"); separator = separator.replaceAll("\\+","\\\\+"); separator = separator.replaceAll("\\?","\\\\?"); separator = separator.replaceAll("\\[","\\\\["); separator = separator.replaceAll("\\]","\\\\]"); separator = separator.replaceAll("\\{","\\\\{"); separator = separator.replaceAll("\\}","\\\\}"); separator = separator.replaceAll("\\^","\\\\^"); separator = separator.replaceAll("\\`","\\\\`"); separator = separator.replaceAll("\\~","\\\\~"); while(inputBuffer.ready()) { readedDataValues.add(inputBuffer.readLine()); } inputReader.close(); dataHeader = readedDataHeader.split(separator); dataValues = new double[readedDataValues.size()][dataHeader.length]; readedDataLines = readedDataValues.toArray(); for(int i = 0; i < readedDataLines.length; i++) { dataLine = (String) readedDataLines[i]; temp = dataLine.split(separator); for(int j = 0; j < temp.length; j++) { dataValues[i][j] = Double.parseDouble(temp[j]); } line++; } } catch (FileNotFoundException e) { throw new LoadFromFileException(resource.getString("file") + " " + file.getName() + " " + resource.getString("notFound")); } catch (IOException e) { throw new LoadFromFileException(resource.getString("failToRead") + " " + file.getName() + "!\n"); } catch (NumberFormatException e) { throw new LoadFromFileException(resource.getString("failToRead") + " " + file.getName() + " (" + resource.getString("line") + " " + line + ")!\n" + resource.getString("readError")); } } //LoadFromFile() /** * * @param fileName * @param separator * * @throws LoadFromFileException */ public LoadFromFile(String fileName, String separator) throws LoadFromFileException { this(new File(fileName), separator); } //LoadFromFile() /** * Retrieves input data and expected output data from a file. * * @param fileName File name to load data. * @param expected Column number of expected output. * * @throws IOException Open/read file error. */ public LoadFromFile(String fileName, int expected) throws IOException { // TODO Treat parse double error. String readedDataHeader; Object[] readedDataLines; Vector readedDataValues = new Vector(); String dataLine; String[] temp; int expectedIndex = 0; int inputColumnIndex = 0; int inputRowIndex = 0; File inputFile = new File(fileName); FileReader inputReader = new FileReader(inputFile); BufferedReader inputBuffer = new BufferedReader(inputReader); readedDataHeader = inputBuffer.readLine(); while (inputBuffer.ready()) readedDataValues.add(inputBuffer.readLine()); inputReader.close(); dataHeader = readedDataHeader.split(" "); expectedOutput = new double[readedDataValues.size()][1]; inputData = new double[readedDataValues.size()][dataHeader.length - 1]; readedDataLines = readedDataValues.toArray(); for (int linha = 0; linha < readedDataLines.length; linha++) { dataLine = (String) readedDataLines[linha]; temp = dataLine.split(" "); for (int j = 0; j < temp.length; j++) { if (j != expected) { inputData[inputRowIndex][inputColumnIndex] = Double.parseDouble(temp[j]); inputColumnIndex++; } else { expectedOutput[expectedIndex][0] = Double.parseDouble(temp[j]); expectedIndex++; } } inputRowIndex++; inputColumnIndex = 0; } } //LoadFromFile() /** * Returns attributes names. * * @return Attributes names array. */ public String[] getDataHeader() { return dataHeader; } //getDataHeader() /** * Returns all data values. * * @return All data values matrix. */ public double[][] getDataValues() { return dataValues; } //getDataValues() /** * Returns expected output data. * * @return Expected output data matrix. */ public double[][] getExpectedOutput() { return expectedOutput; } //getExpectedOutput() /** * Returns input data. * * @return Input data matrix. */ public double[][] getInputData() { return inputData; } //getInputData() /** * Sets expected data matrix. * * @param expectedOutput Expected data matrix. */ public void setExpectedOutput(double[][] expectedOutput) { this.expectedOutput = expectedOutput; } //setExpectedOutput() /** * Sets input data matrix. * * @param inputData Input data matrix. */ public void setInputData(double[][] inputData) { this.inputData = inputData; } //setInputData()} //LoadFromFile
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -