Obfuscate.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 4k
Category:

Windows Develop

Development Platform:

Java

  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.  *    Obfuscate.java
  18.  *    Copyright (C) 2000 Intelligenesis Corp.
  19.  *
  20.  */
  21. package weka.filters.unsupervised.attribute;
  22. import weka.filters.*;
  23. import java.io.*;
  24. import java.util.*;
  25. import weka.core.*;
  26. /** 
  27.  * A simple instance filter that renames the relation, all attribute names
  28.  * and all nominal (and string) attribute values. For exchanging sensitive
  29.  * datasets. Currently doesn't like string attributes.
  30.  *
  31.  * @author Len Trigg (len@intelligenesis.net)
  32.  * @version $Revision: 1.1 $
  33.  */
  34. public class Obfuscate extends Filter implements UnsupervisedFilter,
  35.  StreamableFilter {
  36.   /**
  37.    * Returns a string describing this filter
  38.    *
  39.    * @return a description of the filter suitable for
  40.    * displaying in the explorer/experimenter gui
  41.    */
  42.   public String globalInfo() {
  43.     return "An instance filter that obfuscates all strings in the data";
  44.   }
  45.   /**
  46.    * Sets the format of the input instances.
  47.    *
  48.    * @param instanceInfo an Instances object containing the input instance
  49.    * structure (any instances contained in the object are ignored - only the
  50.    * structure is required).
  51.    * @return true if the outputFormat may be collected immediately
  52.    */
  53.   public boolean setInputFormat(Instances instanceInfo) throws Exception {
  54.     super.setInputFormat(instanceInfo);
  55.     
  56.     // Make the obfuscated header
  57.     FastVector v = new FastVector();
  58.     for (int i = 0; i < instanceInfo.numAttributes(); i++) {
  59.       Attribute oldAtt = instanceInfo.attribute(i);
  60.       Attribute newAtt = null;
  61.       switch (oldAtt.type()) {
  62.       case Attribute.NUMERIC:
  63.         newAtt = new Attribute("A" + (i + 1));
  64.         break;
  65.       case Attribute.NOMINAL:
  66.         FastVector vals = new FastVector();
  67.         for (int j = 0; j < oldAtt.numValues(); j++) {
  68.           vals.addElement("V" + (j + 1));
  69.         }
  70.         newAtt = new Attribute("A" + (i + 1), vals);
  71.         break;
  72.       case Attribute.STRING:
  73.       default:
  74.         newAtt = (Attribute) oldAtt.copy();
  75.         System.err.println("Not converting attribute: " + oldAtt.name());
  76.         break;
  77.       }
  78.       v.addElement(newAtt);
  79.     }
  80.     Instances newHeader = new Instances("R", v, 10);
  81.     setOutputFormat(newHeader);
  82.     return true;
  83.   }
  84.   /**
  85.    * Input an instance for filtering. Ordinarily the instance is processed
  86.    * and made available for output immediately. Some filters require all
  87.    * instances be read before producing output.
  88.    *
  89.    * @param instance the input instance
  90.    * @return true if the filtered instance may now be
  91.    * collected with output().
  92.    * @exception IllegalStateException if no input format has been set.
  93.    */
  94.   public boolean input(Instance instance) {
  95.     if (getInputFormat() == null) {
  96.       throw new IllegalStateException("No input instance format defined");
  97.     }
  98.     if (m_NewBatch) {
  99.       resetQueue();
  100.       m_NewBatch = false;
  101.     }
  102.     push((Instance)instance.copy());
  103.     return true;
  104.   }
  105.   /**
  106.    * Main method for testing this class.
  107.    *
  108.    * @param argv should contain arguments to the filter: use -h for help
  109.    */
  110.   public static void main(String [] argv) {
  111.     
  112.     try {
  113.       if (Utils.getFlag('b', argv)) {
  114. Filter.batchFilterFile(new Obfuscate(), argv);
  115.       } else {
  116. Filter.filterFile(new Obfuscate(), argv);
  117.       }
  118.     } catch (Exception ex) {
  119.       System.out.println(ex.getMessage());
  120.     }
  121.   }
  122. }