Example usage for org.apache.mahout.cf.taste.impl.common FastIDSet iterator

List of usage examples for org.apache.mahout.cf.taste.impl.common FastIDSet iterator

Introduction

In this page you can find the example usage for org.apache.mahout.cf.taste.impl.common FastIDSet iterator.

Prototype

@Override
    public LongPrimitiveIterator iterator() 

Source Link

Usage

From source file:com.aguin.stock.recommender.WriteUserInfo.java

License:Apache License

public void readFromDB() {
    MongoDBDataModel md = MongoDBUserModel.instance();
    if (!MongoDBUserModel.registered(user)) {
        System.out.format("User %s not registered. Please use -ip or -f options to add preferences first",
                user);/*from w w w. j  av a 2  s  .c  om*/
        return;
    }
    try {
        FastIDSet idset = md.getItemIDsFromUser(Long.parseLong(md.fromIdToLong(user, true)));
        if (idset.contains(Long.parseLong(md.fromIdToLong(item, false)))) {
            System.out.format("Found in db: user=%s,item=%s\n", user, item);
        } else {
            System.out.format("Not found: user=%s,item=%s\n", user, item);
        }
        LongPrimitiveIterator fi = idset.iterator();
        StringBuilder sb = new StringBuilder();
        while (fi.hasNext()) {
            sb.append("Item ");
            sb.append(md.fromLongToId(fi.next()));
            sb.append("\n");
        }
        System.out.println(sb);
    } catch (TasteException e) {
        e.printStackTrace();
    }
}

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  ww.j  a va  2s  .  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.msiiplab.recsys.implicit.TanimotoIDF2CoefficientSimilarity.java

License:Apache License

@Override
public double userSimilarity(long userID1, long userID2) throws TasteException {

    DataModel dataModel = getDataModel();

    FastIDSet xPrefs = dataModel.getItemIDsFromUser(userID1);
    FastIDSet yPrefs = dataModel.getItemIDsFromUser(userID2);

    int xPrefsSize = xPrefs.size();
    int yPrefsSize = yPrefs.size();
    if (xPrefsSize == 0 && yPrefsSize == 0) {
        return Double.NaN;
    }//from   w  w w  .  j a  v  a2  s.c  om
    if (xPrefsSize == 0 || yPrefsSize == 0) {
        return 0.0;
    }

    double intersection = 0.0;
    double union = 0.0;

    for (LongPrimitiveIterator it_item = xPrefs.iterator(); it_item.hasNext();) {
        long itemID = (long) it_item.nextLong();
        double weight = (double) getDataModel().getNumUsers() / mItemPrefNum.get(itemID);
        if (yPrefs.contains(itemID)) {
            intersection += weight;
            union -= weight;
        }
        union += weight;
    }
    for (LongPrimitiveIterator it_item = yPrefs.iterator(); it_item.hasNext();) {
        long itemID = (long) it_item.nextLong();
        double weight = (double) getDataModel().getNumUsers() / mItemPrefNum.get(itemID);
        union += weight;
    }

    return Math.log(intersection) / Math.log(union);
}

From source file:com.msiiplab.recsys.implicit.TanimotoIDF2CoefficientSimilarity.java

License:Apache License

private double doItemSimilarity(long itemID1, long itemID2, FastIDSet preferring1) throws TasteException {
    double intersection = 0.0;
    double union = 0.0;

    for (Preference pref : getDataModel().getPreferencesForItem(itemID2)) {
        long userID = pref.getUserID();
        double weight = (double) getDataModel().getNumItems() / mUserPrefNum.get(userID);
        if (preferring1.contains(userID)) {
            intersection += weight;/*w w  w .ja  v  a2s .  c o m*/
            union -= weight;
        }
        union += weight;
    }
    for (LongPrimitiveIterator it_user = preferring1.iterator(); it_user.hasNext();) {
        long userID = (long) it_user.nextLong();
        double weight = (double) getDataModel().getNumItems() / mUserPrefNum.get(userID);
        union += weight;
    }

    if (intersection == 0) {
        return Double.NaN;
    }

    return Math.log(intersection) / Math.log(union);
}

From source file:com.msiiplab.recsys.implicit.TanimotoIDF3CoefficientSimilarity.java

License:Apache License

@Override
public double userSimilarity(long userID1, long userID2) throws TasteException {

    DataModel dataModel = getDataModel();

    FastIDSet xPrefs = dataModel.getItemIDsFromUser(userID1);
    FastIDSet yPrefs = dataModel.getItemIDsFromUser(userID2);

    int xPrefsSize = xPrefs.size();
    int yPrefsSize = yPrefs.size();
    if (xPrefsSize == 0 && yPrefsSize == 0) {
        return Double.NaN;
    }//w  w  w  .java 2  s. c  o  m
    if (xPrefsSize == 0 || yPrefsSize == 0) {
        return 0.0;
    }

    double intersection = 0.0;
    double union = 0.0;

    for (LongPrimitiveIterator it_item = xPrefs.iterator(); it_item.hasNext();) {
        long itemID = (long) it_item.nextLong();
        double weight = (double) getDataModel().getNumUsers() / mItemPrefNum.get(itemID);
        if (yPrefs.contains(itemID)) {
            intersection += weight;
            union -= weight;
        }
        union += weight;
    }
    for (LongPrimitiveIterator it_item = yPrefs.iterator(); it_item.hasNext();) {
        long itemID = (long) it_item.nextLong();
        double weight = (double) getDataModel().getNumUsers() / mItemPrefNum.get(itemID);
        union += weight;
    }

    return intersection / union;
}

From source file:com.msiiplab.recsys.implicit.TanimotoIDF3CoefficientSimilarity.java

License:Apache License

private double doItemSimilarity(long itemID1, long itemID2, FastIDSet preferring1) throws TasteException {
    double intersection = 0.0;
    double union = 0.0;

    for (Preference pref : getDataModel().getPreferencesForItem(itemID2)) {
        long userID = pref.getUserID();
        double weight = (double) getDataModel().getNumItems() / mUserPrefNum.get(userID);
        if (preferring1.contains(userID)) {
            intersection += weight;//from w ww  .j  a  v a2 s  . c o m
            union -= weight;
        }
        union += weight;
    }
    for (LongPrimitiveIterator it_user = preferring1.iterator(); it_user.hasNext();) {
        long userID = (long) it_user.nextLong();
        double weight = (double) getDataModel().getNumItems() / mUserPrefNum.get(userID);
        union += weight;
    }

    if (intersection == 0) {
        return Double.NaN;
    }

    return intersection / union;
}

From source file:com.msiiplab.recsys.implicit.TanimotoIDFCoefficientSimilarity.java

License:Apache License

@Override
public double userSimilarity(long userID1, long userID2) throws TasteException {

    DataModel dataModel = getDataModel();

    FastIDSet xPrefs = dataModel.getItemIDsFromUser(userID1);
    FastIDSet yPrefs = dataModel.getItemIDsFromUser(userID2);

    int xPrefsSize = xPrefs.size();
    int yPrefsSize = yPrefs.size();
    if (xPrefsSize == 0 && yPrefsSize == 0) {
        return Double.NaN;
    }//ww  w  .ja v a  2 s  .com
    if (xPrefsSize == 0 || yPrefsSize == 0) {
        return 0.0;
    }

    double intersection = 0.0;
    double union = 0.0;

    for (LongPrimitiveIterator it_item = xPrefs.iterator(); it_item.hasNext();) {
        long itemID = (long) it_item.nextLong();
        double weight = Math.log((double) getDataModel().getNumUsers() / mItemPrefNum.get(itemID));
        if (yPrefs.contains(itemID)) {
            intersection += weight;
            union -= weight;
        }
        union += weight;
    }
    for (LongPrimitiveIterator it_item = yPrefs.iterator(); it_item.hasNext();) {
        long itemID = (long) it_item.nextLong();
        double weight = Math.log((double) getDataModel().getNumUsers() / mItemPrefNum.get(itemID));
        union += weight;
    }

    return intersection / union;
}

From source file:com.msiiplab.recsys.implicit.TanimotoIDFCoefficientSimilarity.java

License:Apache License

private double doItemSimilarity(long itemID1, long itemID2, FastIDSet preferring1) throws TasteException {
    double intersection = 0.0;
    double union = 0.0;

    for (Preference pref : getDataModel().getPreferencesForItem(itemID2)) {
        long userID = pref.getUserID();
        double weight = Math.log((double) getDataModel().getNumItems() / mUserPrefNum.get(userID));
        if (preferring1.contains(userID)) {
            intersection += weight;/*w ww .j  ava2  s  . com*/
            union -= weight;
        }
        union += weight;
    }
    for (LongPrimitiveIterator it_user = preferring1.iterator(); it_user.hasNext();) {
        long userID = (long) it_user.nextLong();
        double weight = Math.log((double) getDataModel().getNumItems() / mUserPrefNum.get(userID));
        union += weight;
    }

    if (intersection == 0) {
        return Double.NaN;
    }

    return intersection / union;
}

From source file:com.msiiplab.recsys.implicit.TanimotoLFMCoefficientSimilarity.java

License:Apache License

@Override
public double userSimilarity(long userID1, long userID2) throws TasteException {

    DataModel dataModel = getDataModel();

    FastIDSet xPrefs = dataModel.getItemIDsFromUser(userID1);
    FastIDSet yPrefs = dataModel.getItemIDsFromUser(userID2);

    int xPrefsSize = xPrefs.size();
    int yPrefsSize = yPrefs.size();
    if (xPrefsSize == 0 && yPrefsSize == 0) {
        return Double.NaN;
    }//  w w w .j  a  va 2 s . c  om
    if (xPrefsSize == 0 || yPrefsSize == 0) {
        return 0.0;
    }

    double intersection = 0.0;
    double union = 0.0;

    for (LongPrimitiveIterator it_item = xPrefs.iterator(); it_item.hasNext();) {
        long itemID = (long) it_item.nextLong();
        double weight = mItemPrefEntropy.get(itemID);
        if (yPrefs.contains(itemID)) {
            intersection += weight;
            union -= weight;
        }
        union += weight;
    }
    for (LongPrimitiveIterator it_item = yPrefs.iterator(); it_item.hasNext();) {
        long itemID = (long) it_item.nextLong();
        double weight = mItemPrefEntropy.get(itemID);
        union += weight;
    }

    return intersection / union;
}

From source file:com.msiiplab.recsys.implicit.TanimotoLFMCoefficientSimilarity.java

License:Apache License

private double doItemSimilarity(long itemID1, long itemID2, FastIDSet preferring1) throws TasteException {
    double intersection = 0.0;
    double union = 0.0;

    for (Preference pref : getDataModel().getPreferencesForItem(itemID2)) {
        long userID = pref.getUserID();
        double weight = mUserPrefEntropy.get(userID);
        if (preferring1.contains(userID)) {
            intersection += weight;/* ww  w  . j a v a 2  s  . c  o m*/
            union -= weight;
        }
        union += weight;
    }
    for (LongPrimitiveIterator it_user = preferring1.iterator(); it_user.hasNext();) {
        long userID = (long) it_user.nextLong();
        double weight = mUserPrefEntropy.get(userID);
        union += weight;
    }

    if (intersection == 0) {
        return Double.NaN;
    }

    return intersection / union;
}