Example usage for org.apache.mahout.cf.taste.recommender MostSimilarItemsCandidateItemsStrategy getCandidateItems

List of usage examples for org.apache.mahout.cf.taste.recommender MostSimilarItemsCandidateItemsStrategy getCandidateItems

Introduction

In this page you can find the example usage for org.apache.mahout.cf.taste.recommender MostSimilarItemsCandidateItemsStrategy getCandidateItems.

Prototype

FastIDSet getCandidateItems(long[] itemIDs, DataModel dataModel) 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./*from  w  w  w.j ava2s .com*/
 * 
 * @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());
}