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

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

Introduction

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

Prototype

public FastByIDMap() 

Source Link

Document

Creates a new FastByIDMap with default capacity.

Usage

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

License:Apache License

private void buildModel() throws UnknownHostException {
    userIsObject = false;/*from ww  w. ja va 2 s  . co  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:com.anjuke.romar.mahout.GenericReloadDataModel.java

License:Apache License

public GenericReloadDataModel() {
    super();/*from   w ww .  j  a  va 2s . c om*/
    _currentModel = new GenericDataModel(new FastByIDMap<PreferenceArray>());
    _data = new LinkedList<PreferenceValue>();
}

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

License:Apache License

@Override
public void compact() {
    // ????/* w  ww . ja  v a2  s .c  om*/
    final long version = getCurrentVersion() - 1;
    if (version < 0) {
        return;
    }
    File latestSnapshotFile = getLatestSnapshotFile();
    if (latestSnapshotFile != null && getSnapshotFileVersion(latestSnapshotFile) == version) {
        return;
    }

    synchronized (_snapshotWriterLock) {
        PrintWriter snapshotWriter = createWriter(getSnapshotFile(version));
        try {
            final List<File> logFileList = getLogFileListUntilVersion(version);
            List<File> fileToIt = new ArrayList<File>(logFileList);
            if (latestSnapshotFile != null) {
                fileToIt.add(0, latestSnapshotFile);
            }
            PreferenceIterator it = new LogFileIterator(fileToIt);
            FastByIDMap<PreferenceArray> data = new FastByIDMap<PreferenceArray>();
            while (it.hasNext()) {
                Preference pref = it.next();
                if (it.getType() == PreferenceType.ADD) {
                    Util.applyAdd(data, pref);
                } else if (it.getType() == PreferenceType.DELETE) {
                    Util.applyRemove(data, pref);
                }
            }

            for (Entry<Long, PreferenceArray> entry : data.entrySet()) {
                PreferenceArray array = entry.getValue();
                for (int i = 0, length = array.length(); i < length; i++) {
                    long userID = array.getUserID(i);
                    long itemID = array.getItemID(i);
                    float value = array.getValue(i);
                    snapshotWriter.print(userID);
                    snapshotWriter.print(',');
                    snapshotWriter.print(itemID);
                    snapshotWriter.print(',');
                    snapshotWriter.println(value);
                }
            }

            snapshotWriter.flush();
            snapshotWriter.close();
            removeFile();
        } finally {
            snapshotWriter.close();
        }
    }
}

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

License:Apache License

@Override
public FastByIDMap<PreferenceArray> getPreferenceUserData() {
    File snapshotFile = getLatestSnapshotFile();
    long version;
    if (snapshotFile == null) {
        version = -1;/*from  w w  w.  j  a v  a  2  s  .c  o m*/
    } else {
        version = getSnapshotFileVersion(snapshotFile);
    }
    final List<File> list = new ArrayList<File>(getLogFileListFromVersion(version));
    if (snapshotFile != null) {
        list.add(0, snapshotFile);
    }
    PreferenceIterator it = new LogFileIterator(list);
    FastByIDMap<PreferenceArray> data = new FastByIDMap<PreferenceArray>();
    while (it.hasNext()) {
        Preference pref = it.next();
        if (it.getType() == PreferenceType.ADD) {
            Util.applyAdd(data, pref);
        } else if (it.getType() == PreferenceType.DELETE) {
            Util.applyRemove(data, pref);
        }
    }
    return 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. j  a v  a  2s .c  om*/
    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.implicit.TanimotoIDF2CoefficientSimilarity.java

License:Apache License

private void refreshUserAndItemPrefNum() throws TasteException {
    mUserPrefNum = new FastByIDMap<Integer>();
    mItemPrefNum = new FastByIDMap<Integer>();
    LongPrimitiveIterator it_user = getDataModel().getUserIDs();
    while (it_user.hasNext()) {
        long userID = it_user.nextLong();
        mUserPrefNum.put(userID, getDataModel().getPreferencesFromUser(userID).length());
    }/*from w  w w .  j  av  a 2s  . c om*/
    LongPrimitiveIterator it_item = getDataModel().getItemIDs();
    while (it_item.hasNext()) {
        long itemID = it_item.nextLong();
        mItemPrefNum.put(itemID, getDataModel().getNumUsersWithPreferenceFor(itemID));
    }
}

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

License:Apache License

private void refreshAspectModel() throws TasteException {
    mAspectModelRecommender = new AspectModelRecommender(getDataModel(), mFactor);
    mUserPrefEntropy = new FastByIDMap<Double>();
    mItemPrefEntropy = new FastByIDMap<Double>();
    int numOfLatentFactor = mAspectModelRecommender.getNumOfLatentFactor();
    ArrayList<FastByIDMap<Double>> userConditional = mAspectModelRecommender.getUserConditional();
    ArrayList<FastByIDMap<Double>> itemConditional = mAspectModelRecommender.getItemConditional();
    ArrayList<Double> latentPrior = mAspectModelRecommender.getLatentPrior();
    double[] distribution = new double[numOfLatentFactor];
    for (LongPrimitiveIterator it_user = getDataModel().getUserIDs(); it_user.hasNext();) {
        long userID = it_user.nextLong();
        for (int i = 0; i < distribution.length; i++) {
            distribution[i] = userConditional.get(i).get(userID) * latentPrior.get(i);
        }/*  www .ja  v a 2  s  .  com*/
        double entropy = getEntropy(distribution);
        mUserPrefEntropy.put(userID, entropy);
    }
    for (LongPrimitiveIterator it_item = getDataModel().getItemIDs(); it_item.hasNext();) {
        long itemID = it_item.nextLong();
        for (int i = 0; i < distribution.length; i++) {
            distribution[i] = itemConditional.get(i).get(itemID) * latentPrior.get(i);
        }
        double entropy = getEntropy(distribution);
        mItemPrefEntropy.put(itemID, entropy);
    }
}

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);
    }//from   w w  w.j  a  v a2s.c  om
    return map;
}

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

License:Apache License

private void buildModel() throws UnknownHostException, MongoException {
    userIsObject = false;/*from  w  w  w .  ja va  2 s.co  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.webir.popcornsaver.cluster.TreeClusteringRecommender.java

License:Apache License

private void buildClusters() throws TasteException {
    DataModel model = getDataModel();/*from  w ww .  ja va  2s . co m*/
    int numUsers = model.getNumUsers();
    if (numUsers > 0) {
        List<FastIDSet> newClusters = new ArrayList<FastIDSet>(numUsers);
        // Begin with a cluster for each user:
        LongPrimitiveIterator it = model.getUserIDs();
        while (it.hasNext()) {
            FastIDSet newCluster = new FastIDSet();
            newCluster.add(it.nextLong());
            newClusters.add(newCluster);
        }
        if (numUsers > 1) {
            findClusters(newClusters);
        }
        topRecsByUserID = computeTopRecsPerUserID(newClusters);
        clustersByUserID = computeClustersPerUserID(newClusters);
        allClusters = newClusters.toArray(new FastIDSet[newClusters.size()]);
    } else {
        topRecsByUserID = new FastByIDMap<List<RecommendedItem>>();
        clustersByUserID = new FastByIDMap<FastIDSet>();
        allClusters = NO_CLUSTERS;
    }
}