package org.canova.cli.csv.vectorization;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

import org.canova.api.io.data.DoubleWritable;
import org.canova.api.io.data.Text;
import org.canova.api.writable.Writable;
import org.canova.cli.csv.schema.CSVInputSchema;
import org.canova.cli.csv.schema.CSVSchemaColumn;
import org.canova.cli.csv.schema.CSVSchemaColumn.TransformType;

/**
 * Vectorization Engine
 * - takes CSV input and converts it to a transformed vector output in a standard format
 * - uses the input CSV schema and the collected statistics from a pre-pass
 * 
 * @author josh
 *
 */
public class CSVVectorizationEngine {

	/**
	 * Use statistics collected from a previous pass to vectorize (or drop) each column
	 * 
	 * @return
	 */
	public Collection<Writable> vectorize( String key, String value, CSVInputSchema schema ) {
		
		//INDArray
        Collection<Writable> ret = new new ArrayList<>();
		
		// TODO: this needs to be different (needs to be real vector representation
		//String outputVector = "";
		String[] columns = value.split( schema.delimiter );
				
		if ( columns[0].trim().equals("") ) {
			System.out.println("Skipping blank line");
			return null;
		}
		
		int srcColIndex = 0;
		int dstColIndex = 0;
		
		System.out.println( "> Engine.vectorize() ----- ");
		
		// scan through the columns in the schema / input csv data
		for (Map.Entry<String, CSVSchemaColumn> entry : schema.getColumnSchemas().entrySet()) {
			
			String colKey = entry.getKey();
		    CSVSchemaColumn colSchemaEntry = entry.getValue();
		    
		    // produce the per column transform based on stats
		    
		    if ( TransformType.SKIP == colSchemaEntry.transform ) {
		    	
		    	// dont append this to the output vector, skipping
		    	
		    } else {
		    
		    	System.out.println( " column value: " + columns[ srcColIndex ] );
		    	
		    	double convertedColumn = colSchemaEntry.transformColumnValue( columns[ srcColIndex ].trim() );
		    	// add this value to the output vector
                ret.add(new DoubleWritable(convertedColumn));

		    	dstColIndex++;
		    	
		    }
		    
		    
		    
		    srcColIndex++;
		    
		}		
		
		
		return ret;
		
	}
	
	/**
	 * Use statistics collected from a previous pass to vectorize (or drop) each column
	 * 
	 * @return
	 */
	public Collection<Writable> vectorizeToWritable( String key, String value, CSVInputSchema schema ) {
		
		//INDArray
		//INDArray ret = this.createArray( schema.getTransformedVectorSize() );
		Collection<Writable> ret = new ArrayList<>();
		
		// TODO: this needs to be different (needs to be real vector representation
		//String outputVector = "";
		String[] columns = value.split( schema.delimiter );
				
		if ( columns[0].trim().equals("") ) {
			System.out.println("Skipping blank line");
			return null;
		}
		
		int srcColIndex = 0;
		int dstColIndex = 0;
		
		System.out.println( "> Engine.vectorize() ----- ");
		
		// scan through the columns in the schema / input csv data
		for (Map.Entry<String, CSVSchemaColumn> entry : schema.getColumnSchemas().entrySet()) {
			
			String colKey = entry.getKey();
		    CSVSchemaColumn colSchemaEntry = entry.getValue();
		    
		    // produce the per column transform based on stats
		    
		    if ( TransformType.SKIP == colSchemaEntry.transform ) {
		    	
		    	// dont append this to the output vector, skipping
		    	
		    } else {
		    
		    	System.out.println( " column value: " + columns[ srcColIndex ] );
		    	
		    	double convertedColumn = colSchemaEntry.transformColumnValue( columns[ srcColIndex ].trim() );
		    	// add this value to the output vector

		    	//ret.putScalar(dstColIndex, convertedColumn);
		    	ret.add(new Text(convertedColumn + ""));
		    	
		    	dstColIndex++;
		    	
		    }
		    
		    
		    
		    srcColIndex++;
		    
		}		
		
		
		return ret;
		
	}	
	

	
	
	
}

@@ -401,16 +401,73 @@
 ormType;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0A/**%0A *
@@ -719,16 +719,108 @@
 gine %7B%0A%0A
+    private static Logger log = LoggerFactory.getLogger(CSVVectorizationEngine.class);%0A    %0A
 %09/**%0A%09 *
@@ -1066,19 +1066,16 @@
 %3E ret = 
-new
  new Arr
@@ -1299,34 +1299,24 @@
  ) %7B%0A%09%09%09
-System.out.println
+log.info
 (%22Skippi
@@ -1406,34 +1406,24 @@
 0;%0A%09%09%0A%09%09
-System.out.println
+log.info
 ( %22%3E Eng
@@ -1934,34 +1934,24 @@
 %0A%09%09    %09
-System.out.println
+log.info
 ( %22 colu
@@ -2895,34 +2895,24 @@
  ) %7B%0A%09%09%09
-System.out.println
+log.info
 (%22Skippi
@@ -3002,34 +3002,24 @@
 0;%0A%09%09%0A%09%09
-System.out.println
+log.info
 ( %22%3E Eng
@@ -3534,26 +3534,16 @@
    %09
-System.out.println
+log.info
 ( %22 

update canova cli