⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 examplesetwriter.java

📁 著名的开源仿真软件yale
💻 JAVA
字号:
/* *  YALE - Yet Another Learning Environment *  Copyright (C) 2002, 2003 *      Simon Fischer, Ralf Klinkenberg, Ingo Mierswa,  *          Katharina Morik, Oliver Ritthoff *      Artificial Intelligence Unit *      Computer Science Department *      University of Dortmund *      44221 Dortmund,  Germany *  email: yale@ls8.cs.uni-dortmund.de *  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;import edu.udo.cs.yale.operator.parameter.*;import edu.udo.cs.yale.example.ExampleSet;import edu.udo.cs.yale.example.ExampleReader;import edu.udo.cs.yale.example.ExampleFormatter;import edu.udo.cs.yale.example.SparseFormatDataRowReader;import edu.udo.cs.yale.tools.LogService;import java.io.File;import java.io.FileWriter;import java.io.PrintWriter;import java.io.IOException;import java.util.List;import java.util.Iterator;import java.util.Collection;/** Writes values of all examples in an {@link ExampleSet} to a file. Dense, sparse, *  and user defined formats (specified by the parameter 'format') can be used.  *  Attribute description files may be generated for dense and sparse format as well. *  These formats can be read using the {@link ExampleSource} and  *  {@link SparseFormatExampleSource} operators. * *  <dl> *    <dt>dense:</dt><dd> *        Each line of the generated data file is of the form<br/> *        <center><pre>value1 value2 ... valueN label predicted weight cluster</pre></center> *    </dd> *    <dt>sparse:</dt><dd>Only non 0 values are written to the file, prefixed by a column index. See  *        the description of {@link SparseFormatExampleSource} for details. *    </dd> *    <dt>special:</dt><dd>Using the parameter 'special_format', the user can specify the exact  *        format. The $ sign has a special meaning and introduces a command (the following character) *        Additional arguments to this command may be supplied enclosing it in square brackets. *  <dl> *    <dt>$a[separator]:</dt><dd> All attributes separated by separator</dd> *    <dt>$s[separator][indexSeparator]:</dt><dd> Sparse format. For all non zero  *           attributes the following strings are concatenated:  *           the column index, the value of indexSeparator, the attribute value. Attributes *           are separated by separator.</dd> *    <dt>$v[name]:</dt><dd> The value of the named attribute</dd> *    <dt>$c[index]:</dt><dd> The value of the attribute with the given index</dd> *    <dt>$l:</dt><dd> The label</dd> *    <dt>$p:</dt><dd> The predicted label</dd>  *    <dt>$i:</dt><dd> The id</dd> *    <dt>$w:</dt><dd> The weight</dd> *    <dt>$n:</dt><dd> The newline character</dd> *    <dt>$t:</dt><dd> The tabulator character</dd> *    <dt>$$:</dt><dd> The dollar sign</dd> *    <dt>$[:</dt><dd> The '[' character</dd> *    <dt>$]:</dt><dd> The ']' character</dd> *  </dl>  *    Make sure the format string ends with $n if you want examples to be separated by newlines!</dd> *  </dl> * *  @yale.xmlclass ExampleSetWriter *  @see edu.udo.cs.yale.example.ExampleSet *  @author Simon, Ingo *  @version $Id: ExampleSetWriter.java,v 2.12 2003/07/09 15:19:58 fischer Exp $ */public class ExampleSetWriter extends Operator {    /** Diese Klassen m&uuml;ssen mindestens in apply &uuml;bergeben werden. */    private static final Class[] INPUT_CLASSES = { ExampleSet.class };    /** Diese Klasse werden in dem Outputarray zur&uuml;ckgeliefert. */    private static final Class[] OUTPUT_CLASSES = { ExampleSet.class };    private static String[] formatNames;        static {	formatNames = new String[SparseFormatDataRowReader.FORMAT_NAMES.length+2];	formatNames[0] = "dense";	for (int i = 0; i < SparseFormatDataRowReader.FORMAT_NAMES.length; i++) {	    formatNames[i+1] = "sparse_"+SparseFormatDataRowReader.FORMAT_NAMES[i];	}	formatNames[formatNames.length-1] = "special_format";    }    private String filename;    private String attDescFile;    public IOObject[] apply() throws OperatorException {	ExampleSet eSet = (ExampleSet)getInput(ExampleSet.class, false);	filename    = getParameterAsString("example_set_file");	attDescFile = getParameterAsString("attribute_description_file");	try {	    // write example set	    File dataFile = getExperiment().resolveFileName(filename);	    File attFile = (attDescFile != null) ? getExperiment().resolveFileName(attDescFile) : null;	    int format = getParameterAsInt("format");	    if (format == 0) {		eSet.writeToFile(dataFile, attFile);	    } else if (format == formatNames.length-1) {		if (attDescFile != null) 		    logMessage("special_format used. Ignoring attribute description file.",			       LogService.ERROR);		writeSpecialFormat(eSet, dataFile);	    } else {		eSet.writeToFileSparse(format-1, dataFile, attFile);	    }	} catch (IOException e) {	    throw new UserError(this, e, 303, new Object[] {filename+" / "+attDescFile, e.getMessage()});	}		return new IOObject[0];    }    private void writeSpecialFormat(ExampleSet exampleSet, File dataFile) throws OperatorException {	String format = getParameterAsString("special_format");	if (format == null) 	    throw new UserError(this, 201, new Object[] { "special_format", "format", "special_format"} );	ExampleFormatter formatter;	try {	    formatter = ExampleFormatter.compile(format, exampleSet);	} catch (Throwable e) {	    throw new UserError(this, 901, format, e.getMessage());	}	try {	    PrintWriter out = new PrintWriter(new FileWriter(dataFile));	    ExampleReader reader = exampleSet.getExampleReader();	    while (reader.hasNext())		out.print(formatter.format(reader.next()));	    out.close();	} catch (IOException e) {	    throw new UserError(this, e, 301, new Object[] {dataFile,e.getMessage()});	}    }    public Class[] getInputClasses() { return INPUT_CLASSES; }    public Class[] getOutputClasses() { return OUTPUT_CLASSES; }    public List getParameterTypes() {	List types = super.getParameterTypes();	types.add(new ParameterTypeFile("example_set_file", "File to save the example set to.", false));	types.add(new ParameterTypeFile("attribute_description_file", "File to save the attribute descriptions to.", true));	types.add(new ParameterTypeCategory("format", "Format to use for output.", formatNames, 0));	types.add(new ParameterTypeString("special_format", "Format string to use for output.", true));	return types;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -