package edu.ntnu.idi.goldfish.preprocessors;

import edu.ntnu.idi.goldfish.StopWatch;
import edu.ntnu.idi.goldfish.configurations.Config;
import edu.ntnu.idi.goldfish.mahout.DBModel;
import weka.classifiers.Classifier;
import weka.core.Instance;
import weka.core.Instances;

import java.util.List;
import java.util.stream.Collectors;

public abstract class PreprocessorClassifier extends Preprocessor {


    public DBModel classify(Config config, Classifier classifier, Instances dataset) throws Exception {
        DBModel model = config.get("model");
        double threshold = config.get("threshold");

        StopWatch.start("classifier-build");
        classifier.buildClassifier(dataset);
        config.set("time:preprocess-build", StopWatch.get("classifier-build"));

        List<DBModel.DBRow> results = model.getFeedbackRows().stream().filter(row -> row.rating == 0).collect(Collectors.toList());
        for (DBModel.DBRow row : results) {
            Instance un = new Instance(1, new double[]{1, row.timeonpage, row.timeonmouse});
            un.setDataset(dataset);

            double rating;
            double index = classifier.classifyInstance(un);
            double[] distributions = classifier.distributionForInstance(un);

            if(dataset.classAttribute().isNominal()) {
                // ensure we have a certain threshold
                if(distributions[(int)index] < threshold) {
                    continue;
                }

                // get rating value of classification
                double[] values = new double[]{1.0, 2.0, 3.0, 4.0, 5.0};

//                System.out.format("%s u:%d i:%6d res: %.2f  probs:%s\n", prep.getSimpleName(), row.userid, row.itemid, rating, Arrays.toString(distributions));
                rating = values[(int)index];
            }

            // rating is not an index
            else {
                rating = index;
            }

            model.setPreference(row.userid, row.itemid, (float) Math.round(rating));
            addPseudoPref(row.userid, row.itemid);
        }
        return model;
    }
}
@@ -1588,18 +1588,16 @@
  5.0%7D;%0A%0A
-//
         
@@ -1608,151 +1608,108 @@
     
-System.out.format(%22%25s u:%25d i:%256d res: %25.2f  probs:%25s%5Cn%22, prep.getSimpleName(), row.userid, row.itemid, rating, Arrays.toString(distributions));
+rating = values%5B(int)index%5D;%0A            %7D%0A%0A            // rating is not an index%0A            else %7B
 %0A   
@@ -1730,34 +1730,21 @@
 ating = 
-values%5B(int)
 index
-%5D
 ;%0A      
@@ -1741,35 +1741,41 @@
 ex;%0A            
-%7D%0A%0A
+    %0A    
             // r
@@ -1777,81 +1777,211 @@
  // 
-rating is not an index%0A            else %7B%0A                rating = index;
+set confidence threshold as difference between estimated rating and closest integer%0A                if(Math.abs(rating - Math.round(rating)) %3E threshold) %7B%0A                    continue;%0A                %7D
 %0A   

Support threshold for numeric classifiers by using distance to closest integer