lenskit

How do I add items with a score above x to goodItems for precision metric in Lenskit 3.0?


I'd like to add the precision metric and use only items with a rating higher than 4.0 as 'goodItems'

In Lenskit 2 this could be done by:

   metric precision {
    listSize 10
    candidates ItemSelectors.addNRandom(ItemSelectors.testItems(), 100)
    exclude ItemSelectors.trainingItems()
    goodItems ItemSelectors.testRatingMatches(Matchers.greaterThanOrEqualTo(4.0d))
}

Now I'm trying to do the same in Lenskit 3 with graddle but obviously

    metric('pr') {
        goodItems 'ItemSelectors.testRatingMatches(Matchers.greaterThanOrEqualTo(4.0d))'
    }

doesn't work, since there is no ItemSelectors class in Lenskit 3.0. How can I link the goodItems with the appropriate items and discard low-rated items in order to achieve a correct precision value?


Solution

  • As told by Mr. Ekstrand, you can select the good items by adding the following line to the gradle build file.

    goodItems 'user.testHistory.findAll({ it instanceof org.lenskit.data.ratings.Rating && it.value >= 4 })*.itemId'
    

    However, this returns an Object, in Itemselector.class, there is a parsing that happens to Set, this however doesn't work since the returned object is of the ArrayList Type. If I'm correct this means that the Object needs to be casted to an ArrayList before being casted to a set, I did this by copying the Itemselector class and replacing:

    Set<Long> set = (Set<Long>) script.run();
    

    by:

    Set<Long> set = new HashSet<Long>((ArrayList<Long>)script.run());
    

    This returns the correct items from my test-set, rated above 4.0