Example usage for org.apache.mahout.cf.taste.impl.recommender TopItems getTopItems

List of usage examples for org.apache.mahout.cf.taste.impl.recommender TopItems getTopItems

Introduction

In this page you can find the example usage for org.apache.mahout.cf.taste.impl.recommender TopItems getTopItems.

Prototype

public static List<RecommendedItem> getTopItems(int howMany, LongPrimitiveIterator possibleItemIDs,
            IDRescorer rescorer, Estimator<Long> estimator) throws TasteException 

Source Link

Usage

From source file:com.buddycloud.channeldirectory.search.handler.common.mahout.ChannelRecommender.java

License:Apache License

/**
 * Recommends a list of jids of channels that are
 * similar to a given channel.// w ww . j a  va 2  s  . co m
 * 
 * @param channelJid The channel jid
 * @param howMany The number of recommendations
 * @return A list of similar channels' jids 
 * @throws TasteException
 * @throws SQLException 
 */
public RecommendationResponse getSimilarChannels(String channelJid, int howMany)
        throws TasteException, SQLException {

    Long itemId = recommenderDataModel.toChannelId(channelJid);

    if (itemId == null) {
        return new RecommendationResponse(new LinkedList<ChannelData>(), 0);
    }

    TopItems.Estimator<Long> estimator = new MostSimilarEstimator(itemId, itemSimilarity, null);
    MostSimilarItemsCandidateItemsStrategy candidateStrategy = new PreferredItemsNeighborhoodCandidateItemsStrategy();

    FastIDSet possibleItemIDs = candidateStrategy.getCandidateItems(new long[] { itemId },
            recommenderDataModel.getDataModel());
    List<RecommendedItem> recommended = TopItems.getTopItems(howMany, possibleItemIDs.iterator(), null,
            estimator);

    List<ChannelData> recommendedChannels = new LinkedList<ChannelData>();

    for (RecommendedItem recommendedItem : recommended) {
        recommendedChannels.add(recommenderDataModel.toChannelData(recommendedItem.getItemID()));
    }

    return new RecommendationResponse(recommendedChannels, possibleItemIDs.size());
}

From source file:com.recsys.svd.CustomSVDRecommender.java

License:Apache License

@Override
public List<RecommendedItem> recommend(long userID, int howMany, IDRescorer rescorer) throws TasteException {
    Preconditions.checkArgument(howMany >= 1, "howMany must be at least 1");
    log.debug("Recommending items for user ID '{}'", userID);

    PreferenceArray preferencesFromUser = getDataModel().getPreferencesFromUser(userID);
    FastIDSet possibleItemIDs = getAllOtherItems(userID, preferencesFromUser);

    List<RecommendedItem> topItems = TopItems.getTopItems(howMany, possibleItemIDs.iterator(), rescorer,
            new Estimator(userID));
    log.debug("Recommendations are: {}", topItems);

    return topItems;
}

From source file:com.webir.popcornsaver.cluster.TreeClusteringRecommender.java

License:Apache License

private List<RecommendedItem> computeTopRecsForCluster(FastIDSet cluster) throws TasteException {
    DataModel dataModel = getDataModel();
    FastIDSet possibleItemIDs = new FastIDSet();
    LongPrimitiveIterator it = cluster.iterator();
    while (it.hasNext()) {
        possibleItemIDs.addAll(dataModel.getItemIDsFromUser(it.next()));
    }//from  w  w  w. ja v a 2s .  c  o m

    TopItems.Estimator<Long> estimator = new Estimator(cluster);

    List<RecommendedItem> topItems = TopItems.getTopItems(possibleItemIDs.size(), possibleItemIDs.iterator(),
            null, estimator);

    log.debug("Recommendations are: {}", topItems);
    return Collections.unmodifiableList(topItems);
}

From source file:de.unima.dws.webmining.rs.recommender.AvgUserPrefAdaptedUserBasedRecommender.java

License:Apache License

public List<RecommendedItem> recommend(long userID, int howMany, IDRescorer rescorer) throws TasteException {
    Preconditions.checkArgument(howMany >= 1, "howMany must be at least 1");

    log.debug("Recommending items for user ID '{}'", userID);

    long[] theNeighborhood = neighborhood.getUserNeighborhood(userID);

    if (theNeighborhood.length == 0) {
        return Collections.emptyList();
    }/*w w  w.j  a v  a2 s  .  com*/

    FastIDSet allItemIDs = getAllOtherItems(theNeighborhood, userID);
    TopItems.Estimator<Long> estimator = new Estimator(userID, theNeighborhood);
    List<RecommendedItem> topItems = TopItems.getTopItems(howMany, allItemIDs.iterator(), rescorer, estimator);

    log.debug("Recommendations are: {}", topItems);
    return topItems;
}

From source file:net.ufida.info.mahout.common.SlopeOneRecommender.java

License:Apache License

@Override
public List<RecommendedItem> recommend(long userID, int howMany, IDRescorer rescorer) throws TasteException {
    Preconditions.checkArgument(howMany >= 1, "howMany must be at least 1");
    log.debug("Recommending items for user ID '{}'", userID);

    FastIDSet possibleItemIDs = diffStorage.getRecommendableItemIDs(userID);

    TopItems.Estimator<Long> estimator = new Estimator(userID);

    List<RecommendedItem> topItems = TopItems.getTopItems(howMany, possibleItemIDs.iterator(), rescorer,
            estimator); //

    log.debug("Recommendations are: {}", topItems);
    return topItems;
}

From source file:org.plista.kornakapi.core.recommender.FoldingFactorizationBasedRecommender.java

License:Apache License

@Override
public List<RecommendedItem> recommend(long userID, long[] itemIDs, int howMany, IDRescorer rescorer)
        throws TasteException {
    Preconditions.checkArgument(howMany >= 1, "howMany must be at least 1");
    log.debug("Recommending items for user ID '{}'", userID);

    PreferenceArray preferencesFromUser = asPreferences(itemIDs);

    long fetchItemIDsStart = System.currentTimeMillis();
    FastIDSet possibleItemIDs = getAllOtherItems(userID, preferencesFromUser);
    long fetchItemIDsDuration = System.currentTimeMillis() - fetchItemIDsStart;

    long estimateStart = System.currentTimeMillis();
    List<RecommendedItem> topItems;
    if (numEstimationThreads > 1) {
        topItems = ParallelTopItems.getTopItems(howMany, numEstimationThreads, possibleItemIDs, rescorer,
                new Estimator(userID));
    } else {/*from   w w w  . j a  v a 2s .com*/
        topItems = TopItems.getTopItems(howMany, possibleItemIDs.iterator(), rescorer, new Estimator(userID));
    }
    long estimateDuration = System.currentTimeMillis() - estimateStart;

    long numCandidates = -1;
    if (rescorer != null && rescorer instanceof FixedCandidatesIDRescorer) {
        numCandidates = ((FixedCandidatesIDRescorer) rescorer).numCandidates();
    }

    if (log.isDebugEnabled()) {
        log.debug("fetched {} interactions of user {} ({} itemIDs in {} ms, estimation of {} in {} ms)",
                new Object[] { preferencesFromUser.length(), userID, possibleItemIDs.size(),
                        fetchItemIDsDuration, numCandidates, estimateDuration });

    }

    log.debug("Recommendations are: {}", topItems);

    return topItems;
}

From source file:org.plista.kornakapi.core.recommender.FoldingFactorizationBasedRecommender.java

License:Apache License

@Override
public List<RecommendedItem> recommendToAnonymous(long[] itemIDs, int howMany, IDRescorer rescorer)
        throws TasteException {

    //TODO what to do here in the non-implicit case? choose a rating?
    long fetchItemIDsStart = System.currentTimeMillis();
    PreferenceArray preferences = asPreferences(itemIDs);
    long fetchItemIDsDuration = System.currentTimeMillis() - fetchItemIDsStart;

    long estimateStart = System.currentTimeMillis();
    double[] foldedInUserFeatures = foldingFactorization.foldInAnonymousUser(itemIDs);

    FastIDSet possibleItemIDs = getAllOtherItems(Long.MIN_VALUE, preferences);
    List<RecommendedItem> topItems;

    if (numEstimationThreads > 1) {
        topItems = ParallelTopItems.getTopItems(howMany, numEstimationThreads, possibleItemIDs, rescorer,
                new AnonymousEstimator(foldedInUserFeatures));
    } else {// ww w  . j  av  a  2 s. c  o m
        topItems = TopItems.getTopItems(howMany, possibleItemIDs.iterator(), rescorer,
                new AnonymousEstimator(foldedInUserFeatures));
    }
    log.debug("Recommendations are: {}", topItems);
    long estimateDuration = System.currentTimeMillis() - estimateStart;
    if (log.isDebugEnabled()) {
        log.debug("fetched {} interactions of Anonymous ({} itemIDs in {} ms, estimation in {} ms)",
                new Object[] { preferences.length(), preferences.length(), fetchItemIDsDuration,
                        estimateDuration });

    }
    return topItems;
}

From source file:recommender.CustomRecommender.java

@Override
public List<RecommendedItem> recommend(long userID, int howMany, IDRescorer rescorer, boolean includeKnownItems)
        throws TasteException {
    Preconditions.checkArgument(howMany >= 1, "howMany must be at least 1");
    log.debug("Recommending items for user ID '{}'", userID);

    PreferenceArray preferencesFromUser = getDataModel().getPreferencesFromUser(userID);
    FastIDSet possibleItemIDs = getAllOtherItems(userID, preferencesFromUser, includeKnownItems);

    List<RecommendedItem> topItems = TopItems.getTopItems(howMany, possibleItemIDs.iterator(), rescorer,
            new Estimator(userID));
    log.debug("Recommendations are: {}", topItems);

    return topItems;
}

From source file:recommender.MyRecommender.java

public List<RecommendedItem> recommend1(long userID, int howMany, IDRescorer rescorer,
        boolean includeKnownItems) throws TasteException {
    Preconditions.checkArgument(howMany >= 1, "howMany must be at least 1");
    log.debug("Recommending items for user ID '{}'", userID);
    System.out.println("Recommending items for user ID: " + userID);

    PreferenceArray preferencesFromUser = getDataModel().getPreferencesFromUser(userID);
    FastIDSet possibleItemIDs = getAllOtherItems(userID, preferencesFromUser, includeKnownItems);

    List<RecommendedItem> topItems = TopItems.getTopItems(howMany, possibleItemIDs.iterator(), rescorer,
            new Estimator(userID));
    log.debug("Recommendations are: {}", topItems);

    System.out.println("Recommendations are: " + topItems);

    return topItems;
}