/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.basistech.ninja;

import com.basistech.ninja.ejml.ColVector;
import com.google.common.base.Charsets;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.List;

public class Predict {
    Network net;

    Predict(Network net) {
        this.net = net;
    }

    List<Result> predict(ColVector x) {
        ColVector outVector = net.apply(x);
        return Network.sort(outVector);
    }

    public static void main(String[] args) throws IOException {
        if (args.length != 3) {
            System.err.println("Usage: Predict model examples response");
            System.exit(1);
        }

        Network net = Network.fromText(new File(args[0]));
        File examplesFile = new File(args[1]);
        File responseFile = new File(args[2]);

        Predict that = new Predict(net);

        int inputNeurons = net.getNumNeurons(0);

        try (BufferedReader reader = new BufferedReader(new InputStreamReader(
            new FileInputStream(examplesFile), Charsets.UTF_8));
             BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                 new FileOutputStream(responseFile), Charsets.UTF_8))) {

            int lineno = 0;
            String line;
            while ((line = reader.readLine()) != null) {
                // 1 1:1 2:1 5:1
                String[] fields = line.split("\\s+");
                ColVector x = new ColVector(inputNeurons);
                for (int i = 1; i < fields.length; i++) {
                    String[] feature = fields[i].split(":");
                    int index = Integer.valueOf(feature[0]);
                    double value = Double.valueOf(feature[1]);
                    if (index < 0 || index >= inputNeurons) {
                        throw new RuntimeException(
                            String.format(
                                "line %d: index (%d) out of range [0, %d); wrong network architecture?",
                                lineno + 1,
                                index,
                                inputNeurons));
                    }
                    x.set(index, value);
                }

                Result result = that.predict(x).get(0);
                writer.write(String.valueOf(result.getId()));
                writer.newLine();
                lineno++;
            }
        }
    }
}

@@ -1507,16 +1507,36 @@
 gth != 3
+ && args.length != 4
 ) %7B%0A    
@@ -1601,16 +1601,28 @@
 response
+ %5B--verbose%5D
 %22);%0A    
@@ -1809,16 +1809,100 @@
 gs%5B2%5D);%0A
+        boolean verbose = args.length == 4 && args%5B3%5D.equalsIgnoreCase(%22--verbose%22);
 %0A       
@@ -3278,21 +3278,28 @@
     
+List%3C
 Result
+%3E
  result
+s
  = t
@@ -3316,61 +3316,445 @@
 t(x)
-.get(0);%0A                writer.write(String.valueOf(
+;%0A                String prediction = String.valueOf(results.get(0).getId());%0A                writer.append(prediction);%0A                writer.append('%5Ct');%0A                writer.append(String.format(%22%25f%22, results.get(0).getScore()));%0A                writer.newLine();%0A                if (verbose) %7B%0A                    for (Result result : results) %7B%0A                        writer.append(String.format(%22%5Ct%25f%5Ct%25s%22, result.getScore(), 
 resu
@@ -3775,32 +3775,40 @@
                 
+       
+ 
 writer.newLine()
@@ -3801,32 +3801,72 @@
 iter.newLine();%0A
+                    %7D%0A                %7D%0A
                 

Add verbose option to Predict