diff --git a/nonpers-assignment/src/main/java/org/lenskit/mooc/nonpers/assoc/AssociationItemBasedItemRecommender.java b/nonpers-assignment/src/main/java/org/lenskit/mooc/nonpers/assoc/AssociationItemBasedItemRecommender.java index e545bd2..ffb0ff3 100644 --- a/nonpers-assignment/src/main/java/org/lenskit/mooc/nonpers/assoc/AssociationItemBasedItemRecommender.java +++ b/nonpers-assignment/src/main/java/org/lenskit/mooc/nonpers/assoc/AssociationItemBasedItemRecommender.java @@ -5,7 +5,6 @@ import org.lenskit.api.Result; import org.lenskit.api.ResultList; import org.lenskit.basic.AbstractItemBasedItemRecommender; import org.lenskit.results.Results; -import org.lenskit.results.BasicResult; import org.lenskit.util.collections.LongUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -91,7 +90,7 @@ public class AssociationItemBasedItemRecommender extends AbstractItemBasedItemRe Score entry = scoring.poll(); if (entry == null) break; - results.add(new BasicResult(entry.id, entry.score)); + results.add(Results.create(entry.id, entry.score)); } return Results.newResultList(results); diff --git a/nonpers-assignment/src/main/java/org/lenskit/mooc/nonpers/assoc/LiftAssociationModelProvider.java b/nonpers-assignment/src/main/java/org/lenskit/mooc/nonpers/assoc/LiftAssociationModelProvider.java index 9f7762b..ad1923b 100644 --- a/nonpers-assignment/src/main/java/org/lenskit/mooc/nonpers/assoc/LiftAssociationModelProvider.java +++ b/nonpers-assignment/src/main/java/org/lenskit/mooc/nonpers/assoc/LiftAssociationModelProvider.java @@ -14,6 +14,7 @@ import org.slf4j.LoggerFactory; import javax.inject.Inject; import javax.inject.Provider; import java.util.List; +import java.util.HashSet; /** * Build an association rule model using a lift metric. @@ -68,11 +69,21 @@ public class LiftAssociationModelProvider implements Provider for (Long2ObjectMap.Entry xEntry: itemUsers.long2ObjectEntrySet()) { long xId = xEntry.getLongKey(); LongSortedSet xUsers = xEntry.getValue(); + HashSet xSet = new HashSet(xUsers); // set up a map to hold the scores for each 'y' item Long2DoubleMap itemScores = new Long2DoubleOpenHashMap(); // TODO Compute lift association formulas for all other 'Y' items with respect to this 'X' + for (Long2ObjectMap.Entry yEntry: itemUsers.long2ObjectEntrySet()) { + long yId = yEntry.getLongKey(); + LongSortedSet yUsers = yEntry.getValue(); + + HashSet ySet = new HashSet(yUsers); + ySet.retainAll(xSet); + double p = 1.0 * ySet.size() * allUsers.size() / (xUsers.size() * yUsers.size()); + itemScores.put(yId, p); + } // save the score map to the main map assocMatrix.put(xId, itemScores); diff --git a/nonpers-assignment/src/main/java/org/lenskit/mooc/nonpers/mean/MeanItemBasedItemRecommender.java b/nonpers-assignment/src/main/java/org/lenskit/mooc/nonpers/mean/MeanItemBasedItemRecommender.java index 4e12b48..fee7255 100644 --- a/nonpers-assignment/src/main/java/org/lenskit/mooc/nonpers/mean/MeanItemBasedItemRecommender.java +++ b/nonpers-assignment/src/main/java/org/lenskit/mooc/nonpers/mean/MeanItemBasedItemRecommender.java @@ -15,6 +15,7 @@ import javax.inject.Inject; import java.util.ArrayList; import java.util.List; import java.util.Set; +import java.util.PriorityQueue; /** * An item scorer that scores each item with its mean rating. @@ -85,7 +86,29 @@ public class MeanItemBasedItemRecommender extends AbstractItemBasedItemRecommend private ResultList recommendItems(int n, LongSet items) { List results = new ArrayList<>(); - // TODO Find the top N items by mean rating + class Score implements Comparable { + private double score; + private long id; + public Score(double score, long id) { this.score = score; this.id = id; } + + @Override + public int compareTo(Score other) { + return new Double(-this.score).compareTo(new Double(-other.score)); + } + } + + PriorityQueue scoring = new PriorityQueue(); + for (long item : items) { + double score = model.getMeanRating(item); + scoring.offer(new Score(score, item)); + } + + for (int i = 0; i < n; ++i) { + Score entry = scoring.poll(); + if (entry == null) break; + + results.add(Results.create(entry.id, entry.score)); + } return Results.newResultList(results); }