📄 obfuscator.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.preprocessing;
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.ExampleSet;
import edu.udo.cs.yale.example.Attribute;
import edu.udo.cs.yale.tools.LogService;
import edu.udo.cs.yale.tools.RandomGenerator;
import java.util.List;
import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;
import java.io.*;
/** This operator takes an <code>ExampleSet</code> as input and maps all nominal values
* to randomly created strings. The names and the construction descriptions of all attributes
* will also replaced by random strings. This operator can be used to anonymize your data.
* It is possible to save the obfuscating map into a file which can be used to remap the old
* values and names. Please use the operator <code>Deobfuscator</code> for this purpose.
* The new example set can be written with an <code>ExampleSetWriter</code>.
*
* @version $Id: Obfuscator.java,v 1.3 2004/09/17 12:57:42 ingomierswa Exp $
*/
public class Obfuscator extends Operator {
public IOObject[] apply() throws OperatorException {
// init
ExampleSet exampleSet = (ExampleSet)getInput(ExampleSet.class);
Map obfuscatorMap = new HashMap();
// obfuscate regular attributes
for (int i = 0; i < exampleSet.getNumberOfAttributes(); i++) {
obfuscateAttribute(exampleSet.getAttribute(i), obfuscatorMap);
}
// obfuscate special attributes
Iterator i = exampleSet.getSpecialAttributeNames().iterator();
while (i.hasNext()) {
obfuscateAttribute(exampleSet.getAttribute((String)i.next()), obfuscatorMap);
}
exampleSet.recalculateAllAttributeStatistics();
String filename = getParameterAsString("obfuscation_map_file");
if ((filename != null) && (filename.trim().length() > 0)) {
File file = getExperiment().resolveFileName(filename);
try {
writeObfuscatorMap(obfuscatorMap, file);
} catch (IOException e) {
throw new UserError(this, 303, filename, e.getMessage());
}
}
return new IOObject[] { exampleSet };
}
private void obfuscateAttribute(Attribute attribute, Map obfuscatorMap) {
String oldName = attribute.getName();
String newName = RandomGenerator.getGlobalRandomGenerator().nextString(8);
attribute.setName(newName);
attribute.clearConstructionDescription();
obfuscatorMap.put(newName, oldName);
if (attribute.isNominal()) {
Iterator v = attribute.getValues().iterator();
while (v.hasNext()) {
String oldValue = (String)v.next();
String newValue = RandomGenerator.getGlobalRandomGenerator().nextString(8);
attribute.replaceValue(oldValue, newValue);
obfuscatorMap.put(oldName + ":" + newValue, oldValue);
}
}
}
private void writeObfuscatorMap(Map obfuscatorMap, File file) throws IOException {
PrintWriter out = new PrintWriter(new FileWriter(file));
Iterator i = obfuscatorMap.keySet().iterator();
while (i.hasNext()) {
String key = (String)i.next();
String value = (String)obfuscatorMap.get(key);
out.println(key + "\t" + value);
}
out.close();
}
public Class[] getInputClasses() { return new Class[] { ExampleSet.class }; }
public Class[] getOutputClasses() { return new Class[] { ExampleSet.class }; }
public List getParameterTypes() {
List types = super.getParameterTypes();
ParameterType type = new ParameterTypeFile("obfuscation_map_file", "File where the obfuscator map should be written to.", true);
types.add(type);
return types;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -