📄 attributevaluemapper.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.Example;import edu.udo.cs.yale.example.ExampleReader;import edu.udo.cs.yale.example.Attribute;import edu.udo.cs.yale.tools.LogService;import java.util.List;/** * This operator takes an <code>ExampleSet</code> as input and maps the values * of certain attributes to other values. For example, it can replace all occurrences * of the String "unknown" in a nominal Attribute by a default String, for all * examples in the ExampleSet. * * <h4>Parameters:</h4> * <ul> * <li><b>[attribute_name]</b>: mapping of values will be applied to this attribute.</li> * <li><b>[replace_what]</b>: all occurrences of this value will be replaced.</li> * <li><b>[replace_by]</b>: the new value.</li> * </ul> */public class AttributeValueMapper extends Operator{ private static final Class[] INPUT_CLASSES = { ExampleSet.class }; private static final Class[] OUTPUT_CLASSES = { ExampleSet.class }; private String attribute_name, replace_what, replace_by; /** * Applies the operator by looping through all examples and replacing the * specified values. * * @return An array of IOObjects, with the output example set as the only member. */ public IOObject[] apply() throws OperatorException { // get the input example set IOContainer input = getInput(); ExampleSet inputSet = (ExampleSet) input.getInput(ExampleSet.class); // check if all specifications are there: if ((attribute_name == null) || (replace_what == null) || (replace_by == null)) { LogService.logMessage("AttributeValueMapper is underspecified and will have no effect!", LogService.WARNING); return new IOObject[] { inputSet }; } // check if specified attribute exists: int attributePosition = -1; for (int i = 0; i < inputSet.getNumberOfAttributes(); i++) { if (inputSet.getAttribute(i).getName().equalsIgnoreCase(attribute_name)) { attributePosition = i; } } if (attributePosition == -1) { LogService.logMessage("AttributeValueMapper: unknown attribute! Nothing done.", LogService.WARNING); return new IOObject[] { inputSet }; } ExampleReader myReader = inputSet.getExampleReader(); Example myEx; int numberOfReplacements = 0; try { while (myReader.hasNext()) { myEx = myReader.next(); if (myEx.getValueAsString(attributePosition).equalsIgnoreCase(replace_what)) { myEx.setValue(attributePosition, replace_by); numberOfReplacements++; } } } catch (NumberFormatException nfe) { LogService.logMessage("Attribute >" + attribute_name + "< expected double value, but found >" + replace_by + "<! No replacements were made!", LogService.WARNING); } LogService.logMessage("AttributeValueMapper: replaced '" + replace_what + "' by '" + replace_by + "' for attribute '" + attribute_name + "' " + numberOfReplacements + " times.", LogService.STATUS); // return the output: return new IOObject[] { inputSet }; } /* * Call the supermethod, and read the parameters. */ public void initApply() throws OperatorException { super.initApply(); attribute_name = getParameterAsString("attribute_name"); replace_what = getParameterAsString("replace_what"); replace_by = getParameterAsString("replace_by"); } public Class[] getInputClasses() { return INPUT_CLASSES; } /** the example set */ public Class[] getOutputClasses() { return OUTPUT_CLASSES; } public List getParameterTypes() { List types = super.getParameterTypes(); types.add(new ParameterTypeString("attribute_name", "Mapping of values will be applied to this attribute.", null)); types.add(new ParameterTypeString("replace_what", "All occurrences of this value will be replaced.", null)); types.add(new ParameterTypeString("replace_by", "The new value", null)); return types; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -