Example usage for org.apache.mahout.cf.taste.impl.common FastByIDMap get

List of usage examples for org.apache.mahout.cf.taste.impl.common FastByIDMap get

Introduction

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

Prototype

public V get(long key) 

Source Link

Usage

From source file:alto.plugin.webradio.recommender.MongoDBDataModel.java

License:Apache License

private void buildModel() throws UnknownHostException {
    userIsObject = false;/*from w ww .j a  v a2s.c om*/
    itemIsObject = false;
    idCounter = 0;
    preferenceIsString = true;
    Mongo mongoDDBB = new Mongo(mongoHost, mongoPort);
    DB db = mongoDDBB.getDB(mongoDB);
    mongoTimestamp = new Date(0);
    FastByIDMap<Collection<Preference>> userIDPrefMap = new FastByIDMap<Collection<Preference>>();
    if (!mongoAuth || db.authenticate(mongoUsername, mongoPassword.toCharArray())) {
        collection = db.getCollection(mongoCollection);
        collectionMap = db.getCollection(mongoMapCollection);
        DBObject indexObj = new BasicDBObject();
        indexObj.put("element_id", 1);
        collectionMap.ensureIndex(indexObj);
        indexObj = new BasicDBObject();
        indexObj.put("long_value", 1);
        collectionMap.ensureIndex(indexObj);
        collectionMap.remove(new BasicDBObject());
        DBCursor cursor = collection.find();
        while (cursor.hasNext()) {
            Map<String, Object> user = (Map<String, Object>) cursor.next().toMap();
            if (!user.containsKey("deleted_at")) {
                long userID = Long.parseLong(fromIdToLong(getID(user.get(mongoUserID), true), true));
                long itemID = Long.parseLong(fromIdToLong(getID(user.get(mongoItemID), false), false));
                float ratingValue = getPreference(user.get(mongoPreference));
                Collection<Preference> userPrefs = userIDPrefMap.get(userID);
                if (userPrefs == null) {
                    userPrefs = Lists.newArrayListWithCapacity(2);
                    userIDPrefMap.put(userID, userPrefs);
                }
                userPrefs.add(new GenericPreference(userID, itemID, ratingValue));
                if (user.containsKey("created_at")
                        && mongoTimestamp.compareTo(getDate(user.get("created_at"))) < 0) {
                    mongoTimestamp = getDate(user.get("created_at"));
                }
            }
        }
    }
    delegate = new GenericDataModel(GenericDataModel.toDataMap(userIDPrefMap, true));
}

From source file:alto.plugin.webradio.recommender.MongoDBDataModel.java

License:Apache License

private DataModel removeUserItem(long userID, Iterable<List<String>> items) {
    FastByIDMap<PreferenceArray> rawData = ((GenericDataModel) delegate).getRawUserData();
    for (List<String> item : items) {
        PreferenceArray prefs = rawData.get(userID);
        long itemID = Long.parseLong(item.get(0));
        if (prefs != null) {
            boolean exists = false;
            int length = prefs.length();
            for (int i = 0; i < length; i++) {
                if (prefs.getItemID(i) == itemID) {
                    exists = true;/*  w w w .j  a v  a  2  s . com*/
                    break;
                }
            }
            if (exists) {
                rawData.remove(userID);
                if (length > 1) {
                    PreferenceArray newPrefs = new GenericUserPreferenceArray(length - 1);
                    for (int i = 0, j = 0; i < length; i++, j++) {
                        if (prefs.getItemID(i) == itemID) {
                            j--;
                        } else {
                            newPrefs.set(j, prefs.get(i));
                        }
                    }
                    rawData.put(userID, newPrefs);
                }
                log.info("Removing userID: {} itemID: {}", userID, itemID);
                if (mongoManage) {
                    removeMongoUserItem(Long.toString(userID), Long.toString(itemID));
                }
            }
        }
    }
    return new GenericDataModel(rawData);
}

From source file:alto.plugin.webradio.recommender.MongoDBDataModel.java

License:Apache License

private DataModel addUserItem(long userID, Iterable<List<String>> items) {
    FastByIDMap<PreferenceArray> rawData = ((GenericDataModel) delegate).getRawUserData();
    PreferenceArray prefs = rawData.get(userID);
    for (List<String> item : items) {
        long itemID = Long.parseLong(item.get(0));
        float preferenceValue = Float.parseFloat(item.get(1));
        boolean exists = false;
        if (prefs != null) {
            for (int i = 0; i < prefs.length(); i++) {
                if (prefs.getItemID(i) == itemID) {
                    exists = true;//from   w w w  .j a  va 2  s  . c o m
                    prefs.setValue(i, preferenceValue);
                    break;
                }
            }
        }
        if (!exists) {
            if (prefs == null) {
                prefs = new GenericUserPreferenceArray(1);
            } else {
                PreferenceArray newPrefs = new GenericUserPreferenceArray(prefs.length() + 1);
                for (int i = 0, j = 1; i < prefs.length(); i++, j++) {
                    newPrefs.set(j, prefs.get(i));
                }
                prefs = newPrefs;
            }
            prefs.setUserID(0, userID);
            prefs.setItemID(0, itemID);
            prefs.setValue(0, preferenceValue);
            log.info("Adding userID: {} itemID: {} preferenceValue: {}", userID, itemID, preferenceValue);
            rawData.put(userID, prefs);
            if (mongoManage) {
                addMongoUserItem(Long.toString(userID), Long.toString(itemID), Float.toString(preferenceValue));
            }
        }
    }
    return new GenericDataModel(rawData);
}

From source file:com.anjuke.romar.mahout.persistence.FilePreferenceSourceTest.java

License:Apache License

@Test
public void testGetPreferenceData() {
    source.setPreference(2, 3, 2);//from w  w  w  .  j av a2s .c  o  m
    source.removePreference(2, 3);
    source.setPreference(1, 1, 2);
    source.setPreference(3, 4, 2);
    source.setPreference(3, 5, 3);
    source.commit();
    source.setPreference(5, 4, 1.0f);
    source.compact();
    source.close();
    source = new FilePreferenceSource(path);
    FastByIDMap<PreferenceArray> data = source.getPreferenceUserData();
    PreferenceArray id2 = data.get(2);
    assertNull(id2);
    PreferenceArray id1 = data.get(1);
    assertNotNull(id1);
    assertEquals(1, id1.getItemID(0));
    assertEquals(2.0f, id1.getValue(0), 0f);

    PreferenceArray id3 = data.get(3);
    assertNotNull(id3);
    assertEquals(4, id3.getItemID(0));
    assertEquals(2.0f, id3.getValue(0), 0f);
    assertEquals(5, id3.getItemID(1));
    assertEquals(3.0f, id3.getValue(1), 0f);

    PreferenceArray id5 = data.get(5);
    assertNotNull(id5);
    assertEquals(4, id5.getItemID(0));
    assertEquals(1.0f, id5.getValue(0), 0f);

}

From source file:com.anjuke.romar.mahout.similarity.ReadableGenericSimilarityProxyTest.java

License:Apache License

@Test
public void testProxySimilarityGenericItemSimilarity() {
    Iterator<ItemItemSimilarity> it = Iterators.singletonIterator(new ItemItemSimilarity(1, 2, 0.8));
    GenericItemSimilarity similarity = new GenericItemSimilarity(Util.iterable(it));
    ReadableGenericItemSimilarity readable = ReadableGenericSimilarityProxy.proxySimilarity(similarity);

    FastByIDMap<FastByIDMap<Double>> idIdValueMap = readable.getSimilarityMaps();
    FastByIDMap<Double> id2ValueMap = idIdValueMap.get(1);

    assertNotNull(id2ValueMap);//  ww  w  .  j ava2  s .  c  o  m
    assertEquals(0.8, id2ValueMap.get(2).doubleValue(), 0.00001);
}

From source file:com.anjuke.romar.mahout.similarity.ReadableGenericSimilarityProxyTest.java

License:Apache License

@Test
public void testProxySimilarityGenericUserSimilarity() {
    Iterator<UserUserSimilarity> it = Iterators.singletonIterator(new UserUserSimilarity(1, 2, 0.8));
    GenericUserSimilarity similarity = new GenericUserSimilarity(Util.iterable(it));
    ReadableGenericUserSimilarity readable = ReadableGenericSimilarityProxy.proxySimilarity(similarity);

    FastByIDMap<FastByIDMap<Double>> idIdValueMap = readable.getSimilarityMaps();
    FastByIDMap<Double> id2ValueMap = idIdValueMap.get(1);

    assertNotNull(id2ValueMap);/*from   w ww . j a  v a2  s.  c  o m*/
    assertEquals(0.8, id2ValueMap.get(2).doubleValue(), 0.00001);
}

From source file:com.anjuke.romar.mahout.util.Util.java

License:Apache License

public static void applyAdd(FastByIDMap<PreferenceArray> data, Preference addPreference) {
    long userID = addPreference.getUserID();
    long itemID = addPreference.getItemID();
    float preferenceValue = addPreference.getValue();
    PreferenceArray prefs = data.get(userID);
    boolean exists = false;
    if (prefs != null) {
        for (int i = 0; i < prefs.length(); i++) {
            if (prefs.getItemID(i) == itemID) {
                exists = true;/*from ww  w  . j  a v a  2 s .  co  m*/
                prefs.setValue(i, preferenceValue);
                break;
            }
        }
    }

    if (!exists) {
        if (prefs == null) {
            prefs = new GenericUserPreferenceArray(1);
        } else {
            PreferenceArray newPrefs = new GenericUserPreferenceArray(prefs.length() + 1);
            for (int i = 0, j = 1; i < prefs.length(); i++, j++) {
                newPrefs.set(j, prefs.get(i));
            }
            prefs = newPrefs;
        }
        prefs.setUserID(0, userID);
        prefs.setItemID(0, itemID);
        prefs.setValue(0, preferenceValue);
        data.put(userID, prefs);
    }

}

From source file:com.anjuke.romar.mahout.util.Util.java

License:Apache License

public static void applyRemove(FastByIDMap<PreferenceArray> data, Preference removePreference) {
    long userID = removePreference.getUserID();
    long itemID = removePreference.getItemID();
    PreferenceArray prefs = data.get(userID);
    if (prefs != null) {
        boolean exists = false;
        int length = prefs.length();
        for (int i = 0; i < length; i++) {
            if (prefs.getItemID(i) == itemID) {
                exists = true;/*from  w  w  w.j  a  v a  2  s .com*/
                break;
            }
        }
        if (exists) {
            if (length == 1) {
                data.remove(userID);
            } else {
                PreferenceArray newPrefs = new GenericUserPreferenceArray(length - 1);
                for (int i = 0, j = 0; i < length; i++, j++) {
                    if (prefs.getItemID(i) == itemID) {
                        j--;
                    } else {
                        newPrefs.set(j, prefs.get(i));
                    }
                }
                data.put(userID, newPrefs);
            }
        }
    }

}

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

License:Apache License

protected double computeRNDCG(PreferenceArray userPreferences, PreferenceArray userPredictions,
        FastIDSet relevantItemIDs, FastByIDMap<Preference> userPreferenceMap, int at) {
    double rCumulativeGain = 0.0;
    double rIdealizedGain = 0.0;
    // compute IDCG
    Iterator<Preference> it_pref = userPreferences.iterator();
    int i = 0;//from  w w  w  .ja  v a2  s. com
    while (it_pref.hasNext()) {
        Preference pref = it_pref.next();
        if (i < at) {
            double discount = 1.0 / log2(i + 2.0);
            float rel = pref.getValue();
            rIdealizedGain += rel * discount;
        } else {
            break;
        }
        i++;
    }
    // compute DCG
    it_pref = userPredictions.iterator();
    i = 0;
    while (it_pref.hasNext()) {
        Preference pref = it_pref.next();
        long itemID = pref.getItemID();
        if (relevantItemIDs.contains(itemID) && i < at) {
            double discount = 1.0 / log2(i + 2.0);
            float rel = userPreferenceMap.get(itemID).getValue();
            rCumulativeGain += rel * discount;
        }
        i++;
    }
    if (rIdealizedGain > 0) {
        return rCumulativeGain / rIdealizedGain;
    } else {
        return 0;
    }
}

From source file:com.paradigma.recommender.db.MongoDBDataModel.java

License:Apache License

private void buildModel() throws UnknownHostException, MongoException {
    userIsObject = false;//from w w  w .  j a  va  2  s.c o  m
    itemIsObject = false;
    idCounter = 0;
    preferenceIsString = true;
    Mongo mongoDDBB = new Mongo(mongoHost, mongoPort);
    DB db = mongoDDBB.getDB(mongoDB);
    mongoTimestamp = new Date(0);
    FastByIDMap<Collection<Preference>> userIDPrefMap = new FastByIDMap<Collection<Preference>>();
    if (!mongoAuth || (mongoAuth && db.authenticate(mongoUsername, mongoPassword.toCharArray()))) {
        collection = db.getCollection(mongoCollection);
        collectionMap = db.getCollection(MONGO_MAP_COLLECTION);
        DBObject indexObj = new BasicDBObject();
        indexObj.put("element_id", 1);
        collectionMap.ensureIndex(indexObj);
        indexObj = new BasicDBObject();
        indexObj.put("long_value", 1);
        collectionMap.ensureIndex(indexObj);
        collectionMap.remove(new BasicDBObject());
        DBCursor cursor = collection.find();
        while (cursor.hasNext()) {
            Map<String, Object> user = (Map<String, Object>) cursor.next().toMap();
            if (!user.containsKey("deleted_at")) {
                long userID = Long.parseLong(fromIdToLong(getID(user.get(mongoUserID), true), true));
                long itemID = Long.parseLong(fromIdToLong(getID(user.get(mongoItemID), false), false));
                float ratingValue = getPreference(user.get(mongoPreference));
                Collection<Preference> userPrefs = userIDPrefMap.get(userID);
                if (userPrefs == null) {
                    userPrefs = Lists.newArrayListWithCapacity(2);
                    userIDPrefMap.put(userID, userPrefs);
                }
                userPrefs.add(new GenericPreference(userID, itemID, ratingValue));
                if (user.containsKey("created_at")
                        && mongoTimestamp.compareTo(getDate(user.get("created_at"))) < 0) {
                    mongoTimestamp = getDate(user.get("created_at"));
                }
            }
        }
    }
    delegate = new GenericDataModel(GenericDataModel.toDataMap(userIDPrefMap, true));
}