/*
 * LensKit, an open source recommender systems toolkit.
 * Copyright 2010-2013 Regents of the University of Minnesota and contributors
 * Work on LensKit has been funded by the National Science Foundation under
 * grants IIS 05-34939, 08-08692, 08-12148, and 10-17697.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.grouplens.lenskit.transform.quantize;

import it.unimi.dsi.fastutil.longs.LongSet;
import org.grouplens.lenskit.ItemScorer;
import org.grouplens.lenskit.RatingPredictor;
import org.grouplens.lenskit.baseline.BaselineScorer;
import org.grouplens.lenskit.baseline.PrimaryScorer;
import org.grouplens.lenskit.basic.AbstractRatingPredictor;
import org.grouplens.lenskit.vectors.MutableSparseVector;
import org.grouplens.lenskit.vectors.SparseVector;
import org.grouplens.lenskit.vectors.VectorEntry;

import javax.annotation.Nonnull;
import javax.inject.Inject;

/**
 * A rating predictor wrapper that quantizes predictions.
 * @author <a href="http://www.grouplens.org">GroupLens Research</a>
 */
public class QuantizedRatingPredictor extends AbstractRatingPredictor implements RatingPredictor {
    private final ItemScorer itemScorer;
    private final ItemScorer baselineScorer;
    private final Quantizer quantizer;

    /**
     * Construct a new quantized predictor.
     * @param scorer The item scorer to use.
     * @param baseline A baseline scorer to fall back to.
     * @param q The quantizer.
     */
    @Inject
    public QuantizedRatingPredictor(@PrimaryScorer ItemScorer scorer,
                                    @BaselineScorer ItemScorer baseline,
                                    Quantizer q) {
        itemScorer = scorer;
        baselineScorer = baseline;
        quantizer = q;
    }

    private void quantize(MutableSparseVector scores) {
        for (VectorEntry e: scores.fast()) {
            scores.set(e, quantizer.getIndexValue(quantizer.index(e.getValue())));
        }
    }

    @Override
    public void predict(long user, @Nonnull MutableSparseVector scores) {
        itemScorer.score(user, scores);
        LongSet unset = scores.unsetKeySet();
        if (!unset.isEmpty()) {
            SparseVector bscores = baselineScorer.score(user, unset);
            scores.set(bscores);
        }
        quantize(scores);
    }
}

@@ -1540,16 +1540,50 @@
 onnull;%0A
+import javax.annotation.Nullable;%0A
 import j
@@ -1602,16 +1602,16 @@
 Inject;%0A
-
 %0A/**%0A * 
@@ -2275,16 +2275,26 @@
         
+@Nullable 
 @Baselin
@@ -2788,32 +2788,74 @@
 (user, scores);%0A
+        if (baselineScorer != null) %7B%0A    
         LongSet 
@@ -2888,24 +2888,28 @@
 ();%0A        
+ 
+   
 if (!unset.i
@@ -2916,24 +2916,28 @@
 sEmpty()) %7B%0A
+    
             
@@ -2998,32 +2998,36 @@
 t);%0A            
+ 
+   
 scores.set(bscor
@@ -3023,32 +3023,46 @@
 s.set(bscores);%0A
+            %7D%0A
         %7D%0A      

Make quantized rating predictor support null baselines