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

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

Introduction

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

Prototype

public boolean contains(long key) 

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 . ja v  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.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;
    }// w w  w.ja va2 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 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  .  j av a 2s .  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;
    }// ww  w .  j  a v  a  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;/*w  ww.j a  v  a  2 s  .co 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;
    }//from w  w w. j a va2s . 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 = 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;/*from w ww .  j  a  va  2s  .  c  o  m*/
            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;
    }//from ww  w .  j a va 2s  . co  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 = 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 a2 s  .co  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;
}

From source file:com.msiiplab.recsys.rwr.GLRecommenderIRStatsEvaluator.java

License:Apache License

public void predict(RecommenderBuilder recommenderBuilder, List<DataModel> trainingDataModels,
        List<DataModel> testingDataModels, List<File> outputFileList) throws TasteException {

    // num of train/test pair: num of cross validation folds
    int numFolds = trainingDataModels.size();

    for (int i_folds = 0; i_folds < numFolds; i_folds++) {
        DataModel trainDataModel = trainingDataModels.get(i_folds);
        DataModel testDataModel = testingDataModels.get(i_folds);

        // Build recommender
        Recommender recommender = recommenderBuilder.buildRecommender(trainDataModel);

        LongPrimitiveIterator it_item;// w w w. ja  v  a  2s .  co m
        FastIDSet allItems = new FastIDSet();
        it_item = trainDataModel.getItemIDs();
        while (it_item.hasNext()) {
            allItems.add(it_item.nextLong());
        }
        it_item = testDataModel.getItemIDs();
        while (it_item.hasNext()) {
            allItems.add(it_item.nextLong());
        }

        if (outputFileList != null) {
            File file = outputFileList.get(i_folds);
            log.info("Writing recommender output to file: " + file.getPath());
            try {
                FileOutputStream out = new FileOutputStream(file);
                LongPrimitiveIterator it_user = testDataModel.getUserIDs();
                while (it_user.hasNext()) {
                    long userID = it_user.nextLong();
                    FastIDSet ratedItems = trainDataModel.getItemIDsFromUser(userID);
                    StringBuilder sb = new StringBuilder();
                    Iterator<Long> it = allItems.iterator();
                    while (it.hasNext()) {
                        long itemID = it.next();
                        if (!ratedItems.contains(itemID)) {
                            float pref = 0;
                            try {
                                pref = recommender.estimatePreference(userID, itemID);
                            } catch (NoSuchItemException e) {
                                pref = Float.NaN;
                            }
                            sb.append(userID + "," + itemID + "," + pref + "\n");
                        }
                    }
                    out.write(sb.toString().getBytes());
                    out.flush();
                }
                out.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}