From 2b79fcc0414dd382d1467286d2030f4cd15b3460 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Tue, 7 Sep 2021 10:38:32 -0500 Subject: [PATCH] not working yet --- .../AssociationItemBasedItemRecommender.java | 26 +++++++++++++++++++ .../assoc/BasicAssociationModelProvider.java | 6 ++++- 2 files changed, 31 insertions(+), 1 deletion(-) 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 4cb4452..e545bd2 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,6 +5,7 @@ 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; @@ -14,6 +15,8 @@ import javax.inject.Inject; import java.util.ArrayList; import java.util.List; import java.util.Set; +import java.util.PriorityQueue; +import java.util.AbstractMap; /** * An item-based item scorer that uses association rules. @@ -65,8 +68,31 @@ public class AssociationItemBasedItemRecommender extends AbstractItemBasedItemRe */ private ResultList recommendItems(int n, long refItem, LongSet candidates) { List results = new ArrayList<>(); + + 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)); + } + } // TODO Compute the n highest-scoring items from candidates + PriorityQueue scoring = new PriorityQueue(); + for (long cand : candidates) { + double score = model.getItemAssociation(refItem, cand); + scoring.offer(new Score(score, cand)); + } + + for (int i = 0; i < n; ++i) { + Score entry = scoring.poll(); + if (entry == null) break; + + results.add(new BasicResult(entry.id, entry.score)); + } return Results.newResultList(results); } diff --git a/nonpers-assignment/src/main/java/org/lenskit/mooc/nonpers/assoc/BasicAssociationModelProvider.java b/nonpers-assignment/src/main/java/org/lenskit/mooc/nonpers/assoc/BasicAssociationModelProvider.java index db8d722..415941b 100644 --- a/nonpers-assignment/src/main/java/org/lenskit/mooc/nonpers/assoc/BasicAssociationModelProvider.java +++ b/nonpers-assignment/src/main/java/org/lenskit/mooc/nonpers/assoc/BasicAssociationModelProvider.java @@ -12,6 +12,7 @@ import org.lenskit.util.io.ObjectStream; import javax.inject.Inject; import javax.inject.Provider; import java.util.List; +import java.util.HashSet; /** * Build a model for basic association rules. This class computes the association for all pairs of items. @@ -71,7 +72,10 @@ public class BasicAssociationModelProvider implements Provider LongSortedSet yUsers = yEntry.getValue(); // TODO Compute P(Y & X) / P(X) and store in itemScores - itemScores.addTo(yId, 1.0 / xUsers.size()); + HashSet ySet = new HashSet(yUsers), xSet = new HashSet(xUsers); + ySet.retainAll(xSet); + double p = ySet.size() / xSet.size(); + itemScores.put(yId, p); } // save the score map to the main map