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

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

Introduction

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

Prototype

public V put(long key, V value) 

Source Link

Usage

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

License:Apache License

private void buildModel() throws UnknownHostException {
    userIsObject = false;/* w  w  w .  j  a v  a 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 || 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;/* ww w .java  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  v  a  2s. 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.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 w  w  w  .  j  av  a 2 s  . com*/
                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  va 2s . c  o m
                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.buddycloud.channeldirectory.search.handler.common.mahout.MemoryRecommenderDataModel.java

License:Apache License

private void createDataModel() throws IOException {
    CSVReader reader = new CSVReader(new FileReader(properties.getProperty("mahout.dumpfile")));
    reader.readNext(); // Read header

    Map<Long, List<Preference>> preferences = new HashMap<Long, List<Preference>>();

    Long userId = -1L;//from   w ww .  j  a va2  s  .c o m
    Long itemId = -1L;

    while (true) {

        String[] nextLine = reader.readNext();
        if (nextLine == null) {
            break;
        }

        String item = nextLine[0];
        String title = nextLine[1];
        String user = nextLine[2];

        if (!userToId.containsKey(user)) {
            userId++;
            userToId.put(user, userId);
            idToUser.put(userId, user);
        }

        if (!itemToId.containsKey(item)) {
            itemId++;
            ChannelData chData = new ChannelData();
            chData.setId(item);
            chData.setTitle(title);

            itemToId.put(item, itemId);
            idToItem.put(itemId, chData);
        }

        Long currentUserId = userToId.get(user);
        BooleanPreference booleanPreference = new BooleanPreference(currentUserId, itemToId.get(item));

        List<Preference> prefList = preferences.get(currentUserId);

        if (prefList == null) {
            prefList = new LinkedList<Preference>();
            preferences.put(currentUserId, prefList);
        }

        prefList.add(booleanPreference);
    }

    FastByIDMap<PreferenceArray> userData = new FastByIDMap<PreferenceArray>();
    for (Entry<Long, List<Preference>> entry : preferences.entrySet()) {
        userData.put(entry.getKey(), new BooleanUserPreferenceArray(entry.getValue()));
    }

    this.dataModel = new GenericDataModel(userData);
}

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

License:Apache License

protected FastByIDMap<Preference> getPrefereceMap(PreferenceArray userPreferences) {
    FastByIDMap<Preference> map = new FastByIDMap<Preference>();
    for (Preference pref : userPreferences) {
        map.put(pref.getItemID(), pref);
    }/* ww  w.  j  av  a  2 s.c  om*/
    return map;
}

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

License:Apache License

private void buildModel() throws UnknownHostException, MongoException {
    userIsObject = false;/* w ww  .  j  a  v a2s  . 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));
}

From source file:com.paradigma.recommender.db.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;/*  w  ww  . ja v  a 2s .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: {}",
                    new Object[] { 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.recsys.factorizer.CustomAbstractFactorizer.java

License:Apache License

private static FastByIDMap<Integer> createIDMapping(int size, LongPrimitiveIterator idIterator) {
    FastByIDMap<Integer> mapping = new FastByIDMap<Integer>(size);
    int index = 0;
    while (idIterator.hasNext()) {
        mapping.put(idIterator.nextLong(), index++);
    }/*w  w  w .  j  ava2  s .  c  o  m*/
    return mapping;
}