public static void main ( String [ ] args ) throws Exception { parseArgs ( args ) ; GenMunger rawMunger ; rawMunger = ( hex . genmodel . GenMunger ) Class . forName ( assemblyClassName ) . newInstance ( ) ; BufferedReader input = new BufferedReader ( new FileReader ( inputCSVFileName ) ) ; BufferedWriter output = new BufferedWriter ( new FileWriter ( outputCSVFileName ) ) ; // Emit outputCSV column names. String [ ] rawHeader = rawMunger . outNames ( ) ; StringBuilder header = new StringBuilder ( ) ; for ( int i = 0 ; i < rawHeader . length ; ++ i ) { header . append ( "\"" ) . append ( rawHeader [ i ] ) . append ( "\"" ) ; if ( i < rawHeader . length - 1 ) header . append ( "," ) ; } output . write ( header . toString ( ) ) ; output . write ( "\n" ) ; // Loop over inputCSV one row at a time. int lineNum = 0 ; String line ; try { while ( ( line = input . readLine ( ) ) != null ) { lineNum ++ ; // skip the header. if ( lineNum == 1 ) continue ; // Parse the CSV line. Somewhat handles quoted commas. But this ain't no parser test! RowData row ; try { row = parseDataRow ( line , rawMunger ) ; } catch ( NumberFormatException nfe ) { nfe . printStackTrace ( ) ; System . out . println ( "Failed to parse row: " + lineNum ) ; throw new RuntimeException ( ) ; } RowData mungedRow = rawMunger . fit ( row ) ; for ( int i = 0 ; i < rawMunger . outNames ( ) . length ; ++ i ) { Object val = mungedRow == null ? Double . NaN : mungedRow . get ( rawMunger . outNames ( ) [ i ] ) ; if ( val instanceof Double ) output . write ( String . valueOf ( val ) ) ; else output . write ( "\"" + val + "\"" ) ; if ( i < rawMunger . outNames ( ) . length - 1 ) output . write ( "," ) ; } output . write ( "\n" ) ; } } catch ( Exception e ) { System . out . println ( "Caught exception on line " + lineNum ) ; System . out . println ( "" ) ; e . printStackTrace ( ) ; System . exit ( 1 ) ; } finally { // Clean up. output . close ( ) ; input . close ( ) ; } // Predictions were successfully generated. Calling program can now compare them with something. System . exit ( 0 ) ; }
CSV reader and predictor test program .