Example usage for org.apache.mahout.cf.taste.impl.model GenericDataModel GenericDataModel

List of usage examples for org.apache.mahout.cf.taste.impl.model GenericDataModel GenericDataModel

Introduction

In this page you can find the example usage for org.apache.mahout.cf.taste.impl.model GenericDataModel GenericDataModel.

Prototype

@Deprecated
public GenericDataModel(DataModel dataModel) throws TasteException 

Source Link

Document

Creates a new GenericDataModel containing an immutable copy of the data from another given DataModel .

Usage

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

License:Apache License

private void buildModel() throws UnknownHostException {
    userIsObject = false;//from   w  w  w .  j  a v  a2  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;/*from w  w  w.  ja  v a 2  s.  co m*/
                    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  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);
            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.GenericReloadDataModel.java

License:Apache License

public GenericReloadDataModel() {
    super();/*ww w. ja v  a 2 s  . co  m*/
    _currentModel = new GenericDataModel(new FastByIDMap<PreferenceArray>());
    _data = new LinkedList<PreferenceValue>();
}

From source file:com.anjuke.romar.mahout.GenericReloadDataModel.java

License:Apache License

@Override
public synchronized void refresh(Collection<Refreshable> alreadyRefreshed) {
    FastByIDMap<PreferenceArray> data = _currentModel.getRawUserData().clone();
    for (PreferenceValue value : _data) {
        if (value.isAdd()) {
            Util.applyAdd(data, value);
        } else {//ww  w  . jav  a 2 s  .  c o m
            Util.applyRemove(data, value);
        }
    }

    _currentModel = new GenericDataModel(data);
}

From source file:com.anjuke.romar.mahout.GenericReloadDataModel.java

License:Apache License

@Override
public void reload(FastByIDMap<PreferenceArray> data) {
    _currentModel = new GenericDataModel(data);
}

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 w w  . ja  va 2  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.paradigma.recommender.db.MongoDBDataModel.java

License:Apache License

private void buildModel() throws UnknownHostException, MongoException {
    userIsObject = false;//w  ww  . j  av  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 || (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;//from  w w w .j  a v a2s  . 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);
            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:de.apaxo.bedcon.AnimalFoodRecommender.java

License:Open Source License

public void initDataModel() {
    FastByIDMap<PreferenceArray> preferenceMap = new FastByIDMap<PreferenceArray>();
    for (int i = 0; i < animals.size(); i++) {
        List<Preference> userPreferences = new ArrayList<Preference>();
        long userId = id2thing.toLongID(animals.get(i));
        for (int j = 0; j < foods.size(); j++) {
            if (preferences[i][j] != null) {
                userPreferences/* w  w  w  .j av  a 2s . c o  m*/
                        .add(new GenericPreference(userId, id2thing.toLongID(foods.get(j)), preferences[i][j]));
            }
        }
        GenericUserPreferenceArray userArray = new GenericUserPreferenceArray(userPreferences);
        preferenceMap.put(userId, userArray);
    }
    model = new GenericDataModel(preferenceMap);
}