/** * Method for testing filters ability to process multiple batches. * * @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 * @exception Exception if something goes wrong or the user requests help on command options */ public static void batchFilterFile(NominalToBinaryFilter filter, String[] options) throws Exception { M5Instances firstData = null; M5Instances secondData = null; Reader firstInput = null; Reader secondInput = null; PrintWriter firstOutput = null; PrintWriter secondOutput = null; boolean helpRequest; try { helpRequest = M5StaticUtils.getFlag('h', options); String fileName = M5StaticUtils.getOption('i', options); if (fileName.length() != 0) { firstInput = new BufferedReader(new FileReader(fileName)); } else { throw new Exception("No first input file given.\n"); } fileName = M5StaticUtils.getOption('r', options); if (fileName.length() != 0) { secondInput = new BufferedReader(new FileReader(fileName)); } else { throw new Exception("No second input file given.\n"); } fileName = M5StaticUtils.getOption('o', options); if (fileName.length() != 0) { firstOutput = new PrintWriter(new FileOutputStream(fileName)); } else { firstOutput = new PrintWriter(System.out); } fileName = M5StaticUtils.getOption('s', options); if (fileName.length() != 0) { secondOutput = new PrintWriter(new FileOutputStream(fileName)); } else { secondOutput = new PrintWriter(System.out); } String classIndex = M5StaticUtils.getOption('c', options); if (filter instanceof NominalToBinaryFilter) { ((NominalToBinaryFilter) filter).setOptions(options); } M5StaticUtils.checkForRemainingOptions(options); if (helpRequest) { throw new Exception("Help requested.\n"); } firstData = new M5Instances(firstInput, 1); secondData = new M5Instances(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 NominalToBinaryFilter) { filterOptions += "\nFilter options:\n\n"; Enumeration enume = ((NominalToBinaryFilter) filter).listOptions(); while (enume.hasMoreElements()) { Information option = (Information) enume.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(); } }