not working yet

This commit is contained in:
Michael Zhang 2021-09-07 10:38:32 -05:00
parent 89fa4e34c7
commit 2b79fcc041
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
2 changed files with 31 additions and 1 deletions

View file

@ -5,6 +5,7 @@ import org.lenskit.api.Result;
import org.lenskit.api.ResultList; import org.lenskit.api.ResultList;
import org.lenskit.basic.AbstractItemBasedItemRecommender; import org.lenskit.basic.AbstractItemBasedItemRecommender;
import org.lenskit.results.Results; import org.lenskit.results.Results;
import org.lenskit.results.BasicResult;
import org.lenskit.util.collections.LongUtils; import org.lenskit.util.collections.LongUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -14,6 +15,8 @@ import javax.inject.Inject;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.PriorityQueue;
import java.util.AbstractMap;
/** /**
* An item-based item scorer that uses association rules. * 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) { private ResultList recommendItems(int n, long refItem, LongSet candidates) {
List<Result> results = new ArrayList<>(); List<Result> results = new ArrayList<>();
class Score implements Comparable<Score> {
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 // TODO Compute the n highest-scoring items from candidates
PriorityQueue<Score> 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); return Results.newResultList(results);
} }

View file

@ -12,6 +12,7 @@ import org.lenskit.util.io.ObjectStream;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Provider; import javax.inject.Provider;
import java.util.List; 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. * 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<AssociationModel>
LongSortedSet yUsers = yEntry.getValue(); LongSortedSet yUsers = yEntry.getValue();
// TODO Compute P(Y & X) / P(X) and store in itemScores // 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 // save the score map to the main map