All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.lenskit.basic.TopNItemRecommender Maven / Gradle / Ivy

There is a newer version: 3.0-M3
Show newest version
/*
 * LensKit, an open source recommender systems toolkit.
 * Copyright 2010-2014 LensKit Contributors.  See CONTRIBUTORS.md.
 * Work on LensKit has been funded by the National Science Foundation under
 * grants IIS 05-34939, 08-08692, 08-12148, and 10-17697.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.lenskit.basic;


import com.google.common.collect.Ordering;
import it.unimi.dsi.fastutil.longs.*;
import org.lenskit.util.ScoredIdAccumulator;
import org.lenskit.util.TopNScoredIdAccumulator;
import org.lenskit.util.UnlimitedScoredIdAccumulator;
import org.lenskit.api.ItemScorer;
import org.lenskit.api.Result;
import org.lenskit.api.ResultList;
import org.lenskit.api.ResultMap;
import org.lenskit.data.dao.ItemDAO;
import org.lenskit.data.dao.UserEventDAO;
import org.lenskit.data.events.Event;
import org.lenskit.data.history.UserHistory;
import org.lenskit.results.Results;
import org.lenskit.util.collections.LongUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * Recommender that recommends the top N items by a scorer.
 * Implements all methods required by {@link AbstractItemRecommender}. The
 * default exclude set is all items rated by the user.
 *
 * 

Recommendations are returned in descending order of score. * * @author GroupLens Research * @since 1.1 */ public class TopNItemRecommender extends AbstractItemRecommender { private static final Logger logger = LoggerFactory.getLogger(TopNItemRecommender.class); protected final UserEventDAO userEventDAO; protected final ItemDAO itemDAO; protected final ItemScorer scorer; @Inject public TopNItemRecommender(UserEventDAO uedao, ItemDAO idao, ItemScorer scorer) { userEventDAO = uedao; itemDAO = idao; this.scorer = scorer; } public ItemScorer getScorer() { return scorer; } /** * Implement recommendation by calling {@link ItemScorer#score(long, Collection)} and sorting * the results by score. This method uses {@link #getDefaultExcludes(long)} to get the default * exclude set for the user, if none is provided. */ @Override protected List recommend(long user, int n, LongSet candidates, LongSet exclude) { candidates = getEffectiveCandidates(user, candidates, exclude); logger.debug("Computing {} recommendations for user {} from {} candidates", n, user, candidates.size()); Map scores = scorer.score(user, candidates); ScoredIdAccumulator accum; if (n >= 0) { accum = new TopNScoredIdAccumulator(n); } else { accum = new UnlimitedScoredIdAccumulator(); } Long2DoubleFunction map = LongUtils.asLong2DoubleFunction(scores); LongIterator iter = LongIterators.asLongIterator(scores.keySet().iterator()); while (iter.hasNext()) { long item = iter.nextLong(); accum.put(item, map.get(item)); } return accum.finishList(); } /** * Implement recommendation by calling {@link ItemScorer#scoreWithDetails(long, Collection)} and sorting * the results. This method uses {@link #getDefaultExcludes(long)} to get the default * exclude set for the user, if none is provided. */ @Override protected ResultList recommendWithDetails(long user, int n, LongSet candidates, LongSet exclude) { candidates = getEffectiveCandidates(user, candidates, exclude); logger.debug("Computing {} recommendations for user {} from {} candidates", n, user, candidates.size()); ResultMap scores = scorer.scoreWithDetails(user, candidates); return getTopNResults(n, scores); } private LongSet getEffectiveCandidates(long user, LongSet candidates, LongSet exclude) { if (candidates == null) { candidates = getPredictableItems(user); } if (exclude == null) { exclude = getDefaultExcludes(user); } logger.debug("computing effective candidates for user {} from {} candidates and {} excluded items", user, candidates.size(), exclude.size()); if (!exclude.isEmpty()) { candidates = LongUtils.setDifference(candidates, exclude); } return candidates; } @Nonnull private ResultList getTopNResults(int n, Iterable scores) { Ordering ord = Results.scoreOrder(); List topN; if (n < 0) { topN = ord.reverse().immutableSortedCopy(scores); } else { topN = ord.greatestOf(scores, n); } return Results.newResultList(topN); } /** * Get the default exclude set for a user. The base implementation gets * all the items they have interacted with. * * @param user The user ID. * @return The set of items to exclude. */ protected LongSet getDefaultExcludes(long user) { return getDefaultExcludes(userEventDAO.getEventsForUser(user)); } /** * Get the default exclude set for a user. The base implementation returns * all the items they have interacted with (from {@link UserHistory#itemSet()}). * * @param user The user history. * @return The set of items to exclude. */ protected LongSet getDefaultExcludes(@Nullable UserHistory user) { if (user == null) { return LongSets.EMPTY_SET; } else { return user.itemSet(); } } /** * Determine the items for which predictions can be made for a certain user. * This implementation is naive and asks the DAO for all items; subclasses * should override it with something more efficient if practical. * * @param user The user's ID. * @return All items for which predictions can be generated for the user. */ protected LongSet getPredictableItems(long user) { return itemDAO.getItemIds(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy