📄 examplesource.java
字号:
/*
* YALE - Yet Another Learning Environment
* Copyright (C) 2001-2004
* Simon Fischer, Ralf Klinkenberg, Ingo Mierswa,
* Katharina Morik, Oliver Ritthoff
* Artificial Intelligence Unit
* Computer Science Department
* University of Dortmund
* 44221 Dortmund, Germany
* email: yale-team@lists.sourceforge.net
* web: http://yale.cs.uni-dortmund.de/
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package edu.udo.cs.yale.operator.io;
import edu.udo.cs.yale.operator.Operator;
import edu.udo.cs.yale.operator.IOObject;
import edu.udo.cs.yale.operator.OperatorException;
import edu.udo.cs.yale.operator.UserError;
import edu.udo.cs.yale.operator.parameter.*;
import edu.udo.cs.yale.example.Attribute;
import edu.udo.cs.yale.example.ExampleSet;
import edu.udo.cs.yale.example.DataRowReader;
import edu.udo.cs.yale.example.FileDataRowReader;
import edu.udo.cs.yale.example.ExampleReader;
import edu.udo.cs.yale.example.ExampleTable;
import edu.udo.cs.yale.example.MemoryExampleTable;
import edu.udo.cs.yale.example.SkipNANExampleReader;
import edu.udo.cs.yale.example.DataRowFactory;
import edu.udo.cs.yale.example.SimpleExampleSet;
import edu.udo.cs.yale.tools.LogService;
import edu.udo.cs.yale.tools.att.AttributeDataSource;
import edu.udo.cs.yale.tools.att.AttributeDataSources;
import edu.udo.cs.yale.tools.att.AttributeSet;
import java.io.File;
import java.io.IOException;
import java.io.FileReader;
import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
/** This operator reads an example set from (a) file(s). The files are parsed by StreamTokenizers
* which can use several delimiter characters and can ignore given characters totally.
* Additionally you can specify comment characters and can use " and ' as quote characters.
* <br/>
* Probably you can use the default values for the most file formats. Please refer to
* section {@yale.ref sec:inputfiles|First steps/File formats} for details on the
* attribute description file set by the parameter <var>attributes</var> used to specify
* attribute types.
*
* @yale.xmlclass ExampleSource
* @see edu.udo.cs.yale.operator.ExampleSetIterator
* @author Simon, Ingo
* @version $Id: ExampleSource.java,v 1.2 2004/08/27 11:57:37 ingomierswa Exp $
*
*/
public class ExampleSource extends Operator {
private static final Class[] INPUT_CLASSES = {};
private static final Class[] OUTPUT_CLASSES = { ExampleSet.class };
private String attributes;
private int maxLines;
private char[] separators;
private char[] toIgnore;
private char[] comments;
private int dataRowType;
public IOObject[] apply() throws OperatorException {
attributes = getParameterAsString("attributes");
maxLines = getParameterAsInt("sample_size");
separators = getParameterAsString("separator_chars").toCharArray();
toIgnore = getParameterAsString("ignore_chars").toCharArray();
comments = getParameterAsString("comment_chars").toCharArray();
dataRowType = getParameterAsInt("datamanagement");
AttributeDataSources attributeDataSources = null;
FileDataRowReader reader = null;
try {
attributeDataSources = AttributeDataSource.createAttributeDataSources(getExperiment().resolveFileName(attributes), true);
reader = new FileDataRowReader(new DataRowFactory(dataRowType),
attributeDataSources.getDataSources(),
maxLines,
separators,
comments,
toIgnore);
} catch (IOException e) {
throw new UserError(this, e, 302, new Object[] {attributes,e.getMessage()});
} catch (edu.udo.cs.yale.tools.XMLException e) {
throw new UserError(this, e, 401, e.getMessage());
} catch (ParserConfigurationException e) {
throw new UserError(this, e, 401, e.toString());
} catch (SAXException e) {
throw new UserError(this, e, 401, e.toString());
}
AttributeSet attributeSet = new AttributeSet(attributeDataSources);
ExampleTable table = new MemoryExampleTable(attributeSet.getAllAttributes(), reader);
ExampleSet result = table.createExampleSet(attributeSet);
if (result.getSize() == 0) {
throw new UserError(this, 117);
}
return new IOObject[] { result };
}
public Class[] getInputClasses() { return INPUT_CLASSES; }
public Class[] getOutputClasses() { return OUTPUT_CLASSES; }
public List getParameterTypes() {
List types = super.getParameterTypes();
types.add(new ParameterTypeAttributeFile("attributes", "Filename for the xml attribute description file. This file also contains the names of the files to read the data from.", false));
ParameterType type = new ParameterTypeInt("sample_size", "The maximum number of examples to read from the data files (-1 = all)", -1, Integer.MAX_VALUE, -1);
type.setExpert(false);
types.add(type);
types.add(new ParameterTypeCategory("datamanagement", "Determines, how the data is represented internally.",
DataRowFactory.TYPE_NAMES, DataRowFactory.TYPE_DOUBLE_ARRAY));
types.add(new ParameterTypeString("separator_chars", "Column separators for data files", ",;"));
types.add(new ParameterTypeString("ignore_chars", "Characters that are ignored in the data files", ""));
types.add(new ParameterTypeString("comment_chars", "Lines beginning with these characters are ignored.", "#"));
return types;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -