/// <summary> Method for testing filters ability to process multiple batches. /// /// </summary> /// <param name="argv">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 /// </param> /// <exception cref="Exception">if something goes wrong or the user requests help on /// command options /// </exception> public static void batchFilterFile(Filter filter, System.String[] options) { Instances firstData = null; Instances secondData = null; //UPGRADE_ISSUE: Class hierarchy differences between 'java.io.Reader' and 'System.IO.StreamReader' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1186'" System.IO.StreamReader firstInput = null; //UPGRADE_ISSUE: Class hierarchy differences between 'java.io.Reader' and 'System.IO.StreamReader' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1186'" System.IO.StreamReader secondInput = null; System.IO.StreamWriter firstOutput = null; System.IO.StreamWriter secondOutput = null; bool helpRequest; try { helpRequest = Utils.getFlag('h', options); System.String fileName = Utils.getOption('i', options); if (fileName.Length != 0) { //UPGRADE_TODO: The differences in the expected value of parameters for constructor 'java.io.BufferedReader.BufferedReader' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'" //UPGRADE_WARNING: At least one expression was used more than once in the target code. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1181'" //UPGRADE_TODO: Constructor 'java.io.FileReader.FileReader' was converted to 'System.IO.StreamReader' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073'" firstInput = new System.IO.StreamReader(new System.IO.StreamReader(fileName, System.Text.Encoding.Default).BaseStream, new System.IO.StreamReader(fileName, System.Text.Encoding.Default).CurrentEncoding); } else { throw new System.Exception("No first input file given.\n"); } fileName = Utils.getOption('r', options); if (fileName.Length != 0) { //UPGRADE_TODO: The differences in the expected value of parameters for constructor 'java.io.BufferedReader.BufferedReader' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'" //UPGRADE_WARNING: At least one expression was used more than once in the target code. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1181'" //UPGRADE_TODO: Constructor 'java.io.FileReader.FileReader' was converted to 'System.IO.StreamReader' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073'" secondInput = new System.IO.StreamReader(new System.IO.StreamReader(fileName, System.Text.Encoding.Default).BaseStream, new System.IO.StreamReader(fileName, System.Text.Encoding.Default).CurrentEncoding); } else { throw new System.Exception("No second input file given.\n"); } fileName = Utils.getOption('o', options); if (fileName.Length != 0) { //UPGRADE_TODO: Constructor 'java.io.FileOutputStream.FileOutputStream' was converted to 'System.IO.FileStream.FileStream' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioFileOutputStreamFileOutputStream_javalangString'" firstOutput = new System.IO.StreamWriter(new System.IO.FileStream(fileName, System.IO.FileMode.Create), System.Text.Encoding.Default); } else { firstOutput = new System.IO.StreamWriter(System.Console.OpenStandardOutput(), System.Text.Encoding.Default); } fileName = Utils.getOption('s', options); if (fileName.Length != 0) { //UPGRADE_TODO: Constructor 'java.io.FileOutputStream.FileOutputStream' was converted to 'System.IO.FileStream.FileStream' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioFileOutputStreamFileOutputStream_javalangString'" secondOutput = new System.IO.StreamWriter(new System.IO.FileStream(fileName, System.IO.FileMode.Create), System.Text.Encoding.Default); } else { secondOutput = new System.IO.StreamWriter(System.Console.OpenStandardOutput(), System.Text.Encoding.Default); } System.String classIndex = Utils.getOption('c', options); // if (filter instanceof OptionHandler) // { // ((OptionHandler)filter).setOptions(options); // } Utils.checkForRemainingOptions(options); if (helpRequest) { throw new System.Exception("Help requested.\n"); } firstData = new Instances(firstInput, 1); secondData = new Instances(secondInput, 1); if (!secondData.equalHeaders(firstData)) { throw new System.Exception("Input file formats differ.\n"); } if (classIndex.Length != 0) { if (classIndex.Equals("first")) { firstData.ClassIndex = 0; secondData.ClassIndex = 0; } else if (classIndex.Equals("last")) { firstData.ClassIndex = firstData.numAttributes() - 1; secondData.ClassIndex = secondData.numAttributes() - 1; } else { firstData.ClassIndex = System.Int32.Parse(classIndex) - 1; secondData.ClassIndex = System.Int32.Parse(classIndex) - 1; } } } catch (System.Exception ex) { System.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"; // } // } System.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"; //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'" throw new System.Exception('\n' + ex.Message + filterOptions + genericOptions); } bool printedHeader = false; if (filter.setInputFormat(firstData)) { //UPGRADE_TODO: Method 'java.io.PrintWriter.println' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintWriterprintln_javalangString'" firstOutput.WriteLine(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 System.ApplicationException("Filter didn't return true from setInputFormat() " + "earlier!"); } //UPGRADE_TODO: Method 'java.io.PrintWriter.println' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintWriterprintln_javalangString'" firstOutput.WriteLine(filter.output().ToString()); } firstData.delete(0); } // Say that input has finished, and print any pending output instances if (filter.batchFinished()) { if (!printedHeader) { //UPGRADE_TODO: Method 'java.io.PrintWriter.println' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintWriterprintln_javalangString'" firstOutput.WriteLine(filter.getOutputFormat().ToString()); } while (filter.numPendingOutput() > 0) { //UPGRADE_TODO: Method 'java.io.PrintWriter.println' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintWriterprintln_javalangString'" firstOutput.WriteLine(filter.output().ToString()); } } if (firstOutput != null) { //UPGRADE_NOTE: Exceptions thrown by the equivalent in .NET of method 'java.io.PrintWriter.close' may be different. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1099'" firstOutput.Close(); } printedHeader = false; if (filter.OutputFormatDefined) { //UPGRADE_TODO: Method 'java.io.PrintWriter.println' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintWriterprintln_javalangString'" secondOutput.WriteLine(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 System.ApplicationException("Filter didn't return true from" + " isOutputFormatDefined() earlier!"); } //UPGRADE_TODO: Method 'java.io.PrintWriter.println' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintWriterprintln_javalangString'" secondOutput.WriteLine(filter.output().ToString()); } secondData.delete(0); } // Say that input has finished, and print any pending output instances if (filter.batchFinished()) { if (!printedHeader) { //UPGRADE_TODO: Method 'java.io.PrintWriter.println' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintWriterprintln_javalangString'" secondOutput.WriteLine(filter.getOutputFormat().ToString()); } while (filter.numPendingOutput() > 0) { //UPGRADE_TODO: Method 'java.io.PrintWriter.println' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintWriterprintln_javalangString'" secondOutput.WriteLine(filter.output().ToString()); } } if (secondOutput != null) { //UPGRADE_NOTE: Exceptions thrown by the equivalent in .NET of method 'java.io.PrintWriter.close' may be different. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1099'" secondOutput.Close(); } }
/// <summary> Filters an entire set of instances through a filter and returns /// the new set. /// /// </summary> /// <param name="data">the data to be filtered /// </param> /// <param name="filter">the filter to be used /// </param> /// <returns> the filtered set of data /// </returns> /// <exception cref="Exception">if the filter can't be used successfully /// </exception> public static Instances useFilter(Instances data, Filter filter) { /* 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; }