fix
This commit is contained in:
parent
fb31f850e7
commit
ed23bb6b83
3 changed files with 36 additions and 3 deletions
|
@ -5,7 +5,6 @@ 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;
|
||||||
|
@ -91,7 +90,7 @@ public class AssociationItemBasedItemRecommender extends AbstractItemBasedItemRe
|
||||||
Score entry = scoring.poll();
|
Score entry = scoring.poll();
|
||||||
if (entry == null) break;
|
if (entry == null) break;
|
||||||
|
|
||||||
results.add(new BasicResult(entry.id, entry.score));
|
results.add(Results.create(entry.id, entry.score));
|
||||||
}
|
}
|
||||||
|
|
||||||
return Results.newResultList(results);
|
return Results.newResultList(results);
|
||||||
|
|
|
@ -14,6 +14,7 @@ import org.slf4j.LoggerFactory;
|
||||||
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 an association rule model using a lift metric.
|
* Build an association rule model using a lift metric.
|
||||||
|
@ -68,11 +69,21 @@ public class LiftAssociationModelProvider implements Provider<AssociationModel>
|
||||||
for (Long2ObjectMap.Entry<LongSortedSet> xEntry: itemUsers.long2ObjectEntrySet()) {
|
for (Long2ObjectMap.Entry<LongSortedSet> xEntry: itemUsers.long2ObjectEntrySet()) {
|
||||||
long xId = xEntry.getLongKey();
|
long xId = xEntry.getLongKey();
|
||||||
LongSortedSet xUsers = xEntry.getValue();
|
LongSortedSet xUsers = xEntry.getValue();
|
||||||
|
HashSet xSet = new HashSet(xUsers);
|
||||||
|
|
||||||
// set up a map to hold the scores for each 'y' item
|
// set up a map to hold the scores for each 'y' item
|
||||||
Long2DoubleMap itemScores = new Long2DoubleOpenHashMap();
|
Long2DoubleMap itemScores = new Long2DoubleOpenHashMap();
|
||||||
|
|
||||||
// TODO Compute lift association formulas for all other 'Y' items with respect to this 'X'
|
// TODO Compute lift association formulas for all other 'Y' items with respect to this 'X'
|
||||||
|
for (Long2ObjectMap.Entry<LongSortedSet> 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
|
// save the score map to the main map
|
||||||
assocMatrix.put(xId, itemScores);
|
assocMatrix.put(xId, itemScores);
|
||||||
|
|
|
@ -15,6 +15,7 @@ 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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An item scorer that scores each item with its mean rating.
|
* 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) {
|
private ResultList recommendItems(int n, LongSet items) {
|
||||||
List<Result> results = new ArrayList<>();
|
List<Result> results = new ArrayList<>();
|
||||||
|
|
||||||
// TODO Find the top N items by mean rating
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PriorityQueue<Score> 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);
|
return Results.newResultList(results);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue