| 1 | /* |
|---|
| 2 | * This program is free software; you can redistribute it and/or modify |
|---|
| 3 | * it under the terms of the GNU General Public License as published by |
|---|
| 4 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 5 | * (at your option) any later version. |
|---|
| 6 | * |
|---|
| 7 | * This program is distributed in the hope that it will be useful, |
|---|
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 10 | * GNU General Public License for more details. |
|---|
| 11 | * |
|---|
| 12 | * You should have received a copy of the GNU General Public License |
|---|
| 13 | * along with this program; if not, write to the Free Software |
|---|
| 14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | /* |
|---|
| 18 | * Obfuscate.java |
|---|
| 19 | * Copyright (C) 2002 University of Waikato, Hamilton, New Zealand |
|---|
| 20 | * |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | package weka.filters.unsupervised.attribute; |
|---|
| 25 | |
|---|
| 26 | import weka.core.Attribute; |
|---|
| 27 | import weka.core.Capabilities; |
|---|
| 28 | import weka.core.FastVector; |
|---|
| 29 | import weka.core.Instance; |
|---|
| 30 | import weka.core.DenseInstance; |
|---|
| 31 | import weka.core.Instances; |
|---|
| 32 | import weka.core.RevisionUtils; |
|---|
| 33 | import weka.core.Capabilities.Capability; |
|---|
| 34 | import weka.filters.Filter; |
|---|
| 35 | import weka.filters.StreamableFilter; |
|---|
| 36 | import weka.filters.UnsupervisedFilter; |
|---|
| 37 | |
|---|
| 38 | /** |
|---|
| 39 | <!-- globalinfo-start --> |
|---|
| 40 | * A simple instance filter that renames the relation, all attribute names and all nominal (and string) attribute values. For exchanging sensitive datasets. Currently doesn't like string or relational attributes. |
|---|
| 41 | * <p/> |
|---|
| 42 | <!-- globalinfo-end --> |
|---|
| 43 | * |
|---|
| 44 | * @author Len Trigg (len@reeltwo.com) |
|---|
| 45 | * @version $Revision: 6174 $ |
|---|
| 46 | */ |
|---|
| 47 | public class Obfuscate |
|---|
| 48 | extends Filter |
|---|
| 49 | implements UnsupervisedFilter, StreamableFilter { |
|---|
| 50 | |
|---|
| 51 | /** for serialization */ |
|---|
| 52 | static final long serialVersionUID = -343922772462971561L; |
|---|
| 53 | |
|---|
| 54 | /** |
|---|
| 55 | * Returns a string describing this filter |
|---|
| 56 | * |
|---|
| 57 | * @return a description of the filter suitable for |
|---|
| 58 | * displaying in the explorer/experimenter gui |
|---|
| 59 | */ |
|---|
| 60 | public String globalInfo() { |
|---|
| 61 | return |
|---|
| 62 | "A simple instance filter that renames the relation, all attribute names " |
|---|
| 63 | + "and all nominal (and string) attribute values. For exchanging sensitive " |
|---|
| 64 | + "datasets. Currently doesn't like string or relational attributes."; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | /** |
|---|
| 68 | * Returns the Capabilities of this filter. |
|---|
| 69 | * |
|---|
| 70 | * @return the capabilities of this object |
|---|
| 71 | * @see Capabilities |
|---|
| 72 | */ |
|---|
| 73 | public Capabilities getCapabilities() { |
|---|
| 74 | Capabilities result = super.getCapabilities(); |
|---|
| 75 | result.disableAll(); |
|---|
| 76 | |
|---|
| 77 | // attributes |
|---|
| 78 | result.enableAllAttributes(); |
|---|
| 79 | result.enable(Capability.MISSING_VALUES); |
|---|
| 80 | |
|---|
| 81 | // class |
|---|
| 82 | result.enableAllClasses(); |
|---|
| 83 | result.enable(Capability.MISSING_CLASS_VALUES); |
|---|
| 84 | result.enable(Capability.NO_CLASS); |
|---|
| 85 | |
|---|
| 86 | return result; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | /** |
|---|
| 90 | * Sets the format of the input instances. |
|---|
| 91 | * |
|---|
| 92 | * @param instanceInfo an Instances object containing the input instance |
|---|
| 93 | * structure (any instances contained in the object are ignored - only the |
|---|
| 94 | * structure is required). |
|---|
| 95 | * @return true if the outputFormat may be collected immediately |
|---|
| 96 | * @throws Exception if |
|---|
| 97 | */ |
|---|
| 98 | public boolean setInputFormat(Instances instanceInfo) throws Exception { |
|---|
| 99 | |
|---|
| 100 | super.setInputFormat(instanceInfo); |
|---|
| 101 | |
|---|
| 102 | // Make the obfuscated header |
|---|
| 103 | FastVector v = new FastVector(); |
|---|
| 104 | for (int i = 0; i < instanceInfo.numAttributes(); i++) { |
|---|
| 105 | Attribute oldAtt = instanceInfo.attribute(i); |
|---|
| 106 | Attribute newAtt = null; |
|---|
| 107 | switch (oldAtt.type()) { |
|---|
| 108 | case Attribute.NUMERIC: |
|---|
| 109 | newAtt = new Attribute("A" + (i + 1)); |
|---|
| 110 | break; |
|---|
| 111 | case Attribute.DATE: |
|---|
| 112 | String format = oldAtt.getDateFormat(); |
|---|
| 113 | newAtt = new Attribute("A" + (i + 1), format); |
|---|
| 114 | break; |
|---|
| 115 | case Attribute.NOMINAL: |
|---|
| 116 | FastVector vals = new FastVector(); |
|---|
| 117 | for (int j = 0; j < oldAtt.numValues(); j++) { |
|---|
| 118 | vals.addElement("V" + (j + 1)); |
|---|
| 119 | } |
|---|
| 120 | newAtt = new Attribute("A" + (i + 1), vals); |
|---|
| 121 | break; |
|---|
| 122 | case Attribute.STRING: |
|---|
| 123 | case Attribute.RELATIONAL: |
|---|
| 124 | default: |
|---|
| 125 | newAtt = (Attribute) oldAtt.copy(); |
|---|
| 126 | System.err.println("Not converting attribute: " + oldAtt.name()); |
|---|
| 127 | break; |
|---|
| 128 | } |
|---|
| 129 | v.addElement(newAtt); |
|---|
| 130 | } |
|---|
| 131 | Instances newHeader = new Instances("R", v, 10); |
|---|
| 132 | newHeader.setClassIndex(instanceInfo.classIndex()); |
|---|
| 133 | setOutputFormat(newHeader); |
|---|
| 134 | return true; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | /** |
|---|
| 139 | * Input an instance for filtering. Ordinarily the instance is processed |
|---|
| 140 | * and made available for output immediately. Some filters require all |
|---|
| 141 | * instances be read before producing output. |
|---|
| 142 | * |
|---|
| 143 | * @param instance the input instance |
|---|
| 144 | * @return true if the filtered instance may now be |
|---|
| 145 | * collected with output(). |
|---|
| 146 | * @throws IllegalStateException if no input format has been set. |
|---|
| 147 | */ |
|---|
| 148 | public boolean input(Instance instance) { |
|---|
| 149 | |
|---|
| 150 | if (getInputFormat() == null) { |
|---|
| 151 | throw new IllegalStateException("No input instance format defined"); |
|---|
| 152 | } |
|---|
| 153 | if (m_NewBatch) { |
|---|
| 154 | resetQueue(); |
|---|
| 155 | m_NewBatch = false; |
|---|
| 156 | } |
|---|
| 157 | push((Instance)instance.copy()); |
|---|
| 158 | return true; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | /** |
|---|
| 162 | * Returns the revision string. |
|---|
| 163 | * |
|---|
| 164 | * @return the revision |
|---|
| 165 | */ |
|---|
| 166 | public String getRevision() { |
|---|
| 167 | return RevisionUtils.extract("$Revision: 6174 $"); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | /** |
|---|
| 171 | * Main method for testing this class. |
|---|
| 172 | * |
|---|
| 173 | * @param argv should contain arguments to the filter: use -h for help |
|---|
| 174 | */ |
|---|
| 175 | public static void main(String [] argv) { |
|---|
| 176 | runFilter(new Obfuscate(), argv); |
|---|
| 177 | } |
|---|
| 178 | } |
|---|