filter.java

来自「Java 编写的多种数据挖掘算法 包括聚类、分类、预处理等」· Java 代码 · 共 921 行 · 第 1/2 页

JAVA
921
字号
    Instance result = (Instance)m_OutputQueue.peek();    return result;  }  /**   * Returns the number of instances pending output   *   * @return the number of instances  pending output   * @throws NullPointerException if no input structure has been defined   */  public int numPendingOutput() {    if (m_OutputFormat == null) {      throw new NullPointerException("No output instance format defined");    }    return m_OutputQueue.size();  }  /**   * Returns whether the output format is ready to be collected   *   * @return true if the output format is set   */  public boolean isOutputFormatDefined() {    return (m_OutputFormat != null);  }  /**   * Creates a deep copy of the given filter using serialization.   *   * @param model 	the filter to copy   * @return 		a deep copy of the filter   * @throws Exception 	if an error occurs   */  public static Filter makeCopy(Filter model) throws Exception {    return (Filter)new SerializedObject(model).getObject();  }  /**   * Creates a given number of deep copies of the given filter using    * serialization.   *    * @param model 	the filter to copy   * @param num 	the number of filter copies to create.   * @return 		an array of filters.   * @throws Exception 	if an error occurs   */  public static Filter[] makeCopies(Filter model, int num) throws Exception {    if (model == null) {      throw new Exception("No model filter set");    }    Filter[] filters = new Filter[num];    SerializedObject so = new SerializedObject(model);    for (int i = 0; i < filters.length; i++) {      filters[i] = (Filter) so.getObject();    }    return filters;  }    /**   * Filters an entire set of instances through a filter and returns   * the new set.    *   * @param data the data to be filtered   * @param filter the filter to be used   * @return the filtered set of data   * @throws Exception if the filter can't be used successfully   */  public static Instances useFilter(Instances data,				    Filter filter) throws Exception {    /*    System.err.println(filter.getClass().getName()                        + " in:" + data.numInstances());    */    for (int i = 0; i < data.numInstances(); i++) {      filter.input(data.instance(i));    }    filter.batchFinished();    Instances newData = filter.getOutputFormat();    Instance processed;    while ((processed = filter.output()) != null) {      newData.add(processed);    }    /*    System.err.println(filter.getClass().getName()                        + " out:" + newData.numInstances());    */    return newData;  }  /**   * Method for testing filters.   *   * @param filter the filter to use   * @param options should contain the following arguments: <br>   * -i input_file <br>   * -o output_file <br>   * -c class_index <br>   * or -h for help on options   * @throws Exception if something goes wrong or the user requests help on   * command options   */  public static void filterFile(Filter filter, String [] options)     throws Exception {    boolean debug = false;    Instances data = null;    Reader input = null;    PrintWriter output = null;    boolean helpRequest;    try {       helpRequest = Utils.getFlag('h', options);      if (Utils.getFlag('d', options)) {	debug = true;      }      String infileName = Utils.getOption('i', options);      String outfileName = Utils.getOption('o', options);       String classIndex = Utils.getOption('c', options);            if (filter instanceof OptionHandler) {	((OptionHandler)filter).setOptions(options);      }      Utils.checkForRemainingOptions(options);      if (helpRequest) {	throw new Exception("Help requested.\n");      }      if (infileName.length() != 0) {	input = new BufferedReader(new FileReader(infileName));      } else {	input = new BufferedReader(new InputStreamReader(System.in));      }      if (outfileName.length() != 0) {	output = new PrintWriter(new FileOutputStream(outfileName));      } else { 	output = new PrintWriter(System.out);      }      data = new Instances(input, 1);      if (classIndex.length() != 0) {	if (classIndex.equals("first")) {	  data.setClassIndex(0);	} else if (classIndex.equals("last")) {	  data.setClassIndex(data.numAttributes() - 1);	} else {	  data.setClassIndex(Integer.parseInt(classIndex) - 1);	}      }    } catch (Exception ex) {      String filterOptions = "";      // Output the error and also the valid options      if (filter instanceof OptionHandler) {	filterOptions += "\nFilter options:\n\n";	Enumeration enu = ((OptionHandler)filter).listOptions();	while (enu.hasMoreElements()) {	  Option option = (Option) enu.nextElement();	  filterOptions += option.synopsis() + '\n'	    + option.description() + "\n";	}      }      String genericOptions = "\nGeneral options:\n\n"	+ "-h\n"	+ "\tGet help on available options.\n"	+ "\t(use -b -h for help on batch mode.)\n"	+ "-i <file>\n"	+ "\tThe name of the file containing input instances.\n"	+ "\tIf not supplied then instances will be read from stdin.\n"	+ "-o <file>\n"	+ "\tThe name of the file output instances will be written to.\n"	+ "\tIf not supplied then instances will be written to stdout.\n"	+ "-c <class index>\n"	+ "\tThe number of the attribute to use as the class.\n"	+ "\t\"first\" and \"last\" are also valid entries.\n"	+ "\tIf not supplied then no class is assigned.\n";      throw new Exception('\n' + ex.getMessage()			  + filterOptions+genericOptions);    }        if (debug) {      System.err.println("Setting input format");    }    boolean printedHeader = false;    if (filter.setInputFormat(data)) {      if (debug) {	System.err.println("Getting output format");      }      output.println(filter.getOutputFormat().toString());      printedHeader = true;    }        // Pass all the instances to the filter    while (data.readInstance(input)) {      if (debug) {	System.err.println("Input instance to filter");      }      if (filter.input(data.instance(0))) {	if (debug) {	  System.err.println("Filter said collect immediately");	}	if (!printedHeader) {	  throw new Error("Filter didn't return true from setInputFormat() "			  + "earlier!");	}	if (debug) {	  System.err.println("Getting output instance");	}	output.println(filter.output().toString());      }      data.delete(0);    }        // Say that input has finished, and print any pending output instances    if (debug) {      System.err.println("Setting end of batch");    }    if (filter.batchFinished()) {      if (debug) {	System.err.println("Filter said collect output");      }      if (!printedHeader) {	if (debug) {	  System.err.println("Getting output format");	}	output.println(filter.getOutputFormat().toString());      }      if (debug) {	System.err.println("Getting output instance");      }      while (filter.numPendingOutput() > 0) {	output.println(filter.output().toString());	if (debug){	  System.err.println("Getting output instance");	}      }    }    if (debug) {      System.err.println("Done");    }        if (output != null) {      output.close();    }  }  /**   * Method for testing filters ability to process multiple batches.   *   * @param filter the filter to use   * @param options should contain the following arguments:<br>   * -i (first) input file <br>   * -o (first) output file <br>   * -r (second) input file <br>   * -s (second) output file <br>   * -c class_index <br>   * or -h for help on options   * @throws Exception if something goes wrong or the user requests help on   * command options   */  public static void batchFilterFile(Filter filter, String [] options)     throws Exception {    Instances firstData = null;    Instances secondData = null;    Reader firstInput = null;    Reader secondInput = null;    PrintWriter firstOutput = null;    PrintWriter secondOutput = null;    boolean helpRequest;    try {      helpRequest = Utils.getFlag('h', options);      String fileName = Utils.getOption('i', options);       if (fileName.length() != 0) {	firstInput = new BufferedReader(new FileReader(fileName));      } else {	throw new Exception("No first input file given.\n");      }      fileName = Utils.getOption('r', options);       if (fileName.length() != 0) {	secondInput = new BufferedReader(new FileReader(fileName));      } else {	throw new Exception("No second input file given.\n");      }      fileName = Utils.getOption('o', options);       if (fileName.length() != 0) {	firstOutput = new PrintWriter(new FileOutputStream(fileName));      } else {	firstOutput = new PrintWriter(System.out);      }            fileName = Utils.getOption('s', options);       if (fileName.length() != 0) {	secondOutput = new PrintWriter(new FileOutputStream(fileName));      } else {	secondOutput = new PrintWriter(System.out);      }      String classIndex = Utils.getOption('c', options);      if (filter instanceof OptionHandler) {	((OptionHandler)filter).setOptions(options);      }      Utils.checkForRemainingOptions(options);            if (helpRequest) {	throw new Exception("Help requested.\n");      }      firstData = new Instances(firstInput, 1);      secondData = new Instances(secondInput, 1);      if (!secondData.equalHeaders(firstData)) {	throw new Exception("Input file formats differ.\n");      }      if (classIndex.length() != 0) {	if (classIndex.equals("first")) {	  firstData.setClassIndex(0);	  secondData.setClassIndex(0);	} else if (classIndex.equals("last")) {	  firstData.setClassIndex(firstData.numAttributes() - 1);	  secondData.setClassIndex(secondData.numAttributes() - 1);	} else {	  firstData.setClassIndex(Integer.parseInt(classIndex) - 1);	  secondData.setClassIndex(Integer.parseInt(classIndex) - 1);	}      }    } catch (Exception ex) {      String filterOptions = "";      // Output the error and also the valid options      if (filter instanceof OptionHandler) {	filterOptions += "\nFilter options:\n\n";	Enumeration enu = ((OptionHandler)filter).listOptions();	while (enu.hasMoreElements()) {	  Option option = (Option) enu.nextElement();	  filterOptions += option.synopsis() + '\n'	    + option.description() + "\n";	}      }      String genericOptions = "\nGeneral options:\n\n"	+ "-h\n"	+ "\tGet help on available options.\n"	+ "-i <filename>\n"	+ "\tThe file containing first input instances.\n"	+ "-o <filename>\n"	+ "\tThe file first output instances will be written to.\n"	+ "-r <filename>\n"	+ "\tThe file containing second input instances.\n"	+ "-s <filename>\n"	+ "\tThe file second output instances will be written to.\n"	+ "-c <class index>\n"	+ "\tThe number of the attribute to use as the class.\n"	+ "\t\"first\" and \"last\" are also valid entries.\n"	+ "\tIf not supplied then no class is assigned.\n";      throw new Exception('\n' + ex.getMessage()			  + filterOptions+genericOptions);    }    boolean printedHeader = false;    if (filter.setInputFormat(firstData)) {      firstOutput.println(filter.getOutputFormat().toString());      printedHeader = true;    }        // Pass all the instances to the filter    while (firstData.readInstance(firstInput)) {      if (filter.input(firstData.instance(0))) {	if (!printedHeader) {	  throw new Error("Filter didn't return true from setInputFormat() "			  + "earlier!");	}	firstOutput.println(filter.output().toString());      }      firstData.delete(0);    }        // Say that input has finished, and print any pending output instances    if (filter.batchFinished()) {      if (!printedHeader) {	firstOutput.println(filter.getOutputFormat().toString());      }      while (filter.numPendingOutput() > 0) {	firstOutput.println(filter.output().toString());      }    }        if (firstOutput != null) {      firstOutput.close();    }        printedHeader = false;    if (filter.isOutputFormatDefined()) {      secondOutput.println(filter.getOutputFormat().toString());      printedHeader = true;    }    // Pass all the second instances to the filter    while (secondData.readInstance(secondInput)) {      if (filter.input(secondData.instance(0))) {	if (!printedHeader) {	  throw new Error("Filter didn't return true from"			  + " isOutputFormatDefined() earlier!");	}	secondOutput.println(filter.output().toString());      }      secondData.delete(0);    }        // Say that input has finished, and print any pending output instances    if (filter.batchFinished()) {      if (!printedHeader) {	secondOutput.println(filter.getOutputFormat().toString());      }      while (filter.numPendingOutput() > 0) {	secondOutput.println(filter.output().toString());      }    }    if (secondOutput != null) {      secondOutput.close();    }  }  /**   * Main method for testing this class.   *   * @param args should contain arguments to the filter: use -h for help   */  public static void main(String [] args) {        try {      if (args.length == 0) {        throw new Exception("First argument must be the class name of a Filter");      }      String fname = args[0];      Filter f = (Filter)Class.forName(fname).newInstance();      args[0] = "";      if (Utils.getFlag('b', args)) {	Filter.batchFilterFile(f, args);      } else {	Filter.filterFile(f, args);      }    } catch (Exception ex) {      ex.printStackTrace();      System.out.println(ex.getMessage());    }  }}

⌨️ 快捷键说明

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