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

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

Introduction

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

Prototype

public Set<Map.Entry<Long, V>> entrySet() 

Source Link

Usage

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

License:Apache License

@Override
public void compact() {
    // ????/*from w  w  w. ja  va  2s  .c o m*/
    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.train.LocalFileTrainer.java

License:Apache License

public static void main(String[] args) {
    RomarConfig config = RomarConfig.getInstance();

    LOG.info("init dataModel");
    DataModel dataModel = PersistenceDataModelFactory.createDataModel(config);

    MahoutServiceFactory serviceFactory = config.getMahoutServiceFactory();
    LOG.info("init in memory similarity");
    ReadableSimilarity similarity = serviceFactory.createReadableSimilarity(dataModel);
    FastByIDMap<FastByIDMap<Double>> similarityDataMap = similarity.getSimilarityMaps();

    SimilarityFileWriter writer = null;//from   w ww .j ava2  s.c om
    LOG.info("start write to " + config.getSimilarityFile());
    try {
        writer = SimilarityFileWriterFactory.createFileWriter(config);
        for (Map.Entry<Long, FastByIDMap<Double>> idValueEntry : similarityDataMap.entrySet()) {
            long id1 = idValueEntry.getKey();

            for (Map.Entry<Long, Double> entry : idValueEntry.getValue().entrySet()) {
                long id2 = entry.getKey();
                double value = entry.getValue();
                writer.write(id1, id2, value);
            }
        }
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    } finally {
        Closeables.closeQuietly(writer);
    }
    LOG.info("train finish");
}

From source file:lib.eval.AbstractRecommenderEvaluator.java

License:Apache License

private double getEvaluation(FastByIDMap<PreferenceArray> testUserPrefs, Recommender recommender)
        throws TasteException {
    reset();// ww  w . j  a  v  a2  s .  co  m
    Collection<Callable<Void>> estimateCallables = new ArrayList<Callable<Void>>();
    AtomicInteger noEstimateCounter = new AtomicInteger();
    for (Map.Entry<Long, PreferenceArray> entry : testUserPrefs.entrySet()) {
        estimateCallables.add(new PreferenceEstimateCallable(recommender, entry.getKey(), entry.getValue(),
                noEstimateCounter));
    }
    log.info("Beginning evaluation of {} users", estimateCallables.size());
    execute(estimateCallables, noEstimateCounter);
    //scale evaluation result so it's a percentage of rating-spread
    double rawResult = computeFinalEvaluation();
    if (Double.isNaN(getMaxPreference()) || Double.isNaN(getMinPreference())) {
        return rawResult;
    } else {
        double scaling = getMaxPreference() - getMinPreference();
        double result = rawResult / scaling;
        return result;
    }

}

From source file:net.ufida.info.mahout.common.MemoryDiffStorage.java

License:Apache License

private void pruneInconsequentialDiffs() {
    // Go back and prune inconsequential diffs. "Inconsequential" means, here, only represented by one
    // data point, so possibly unreliable
    Iterator<Map.Entry<Long, FastByIDMap<RunningAverage>>> it1 = averageDiffs.entrySet().iterator();
    while (it1.hasNext()) {
        FastByIDMap<RunningAverage> map = it1.next().getValue();
        Iterator<Map.Entry<Long, RunningAverage>> it2 = map.entrySet().iterator();
        while (it2.hasNext()) {
            RunningAverage average = it2.next().getValue();
            if (average.getCount() <= 1) {
                it2.remove();/*from w w  w  .j a  v a  2  s. c  o  m*/
            }
        }
        if (map.isEmpty()) {
            it1.remove();
        } else {
            map.rehash();
        }
    }
    averageDiffs.rehash();
}

From source file:recsys.evaluator.TopKRecommenderEvaluator.java

License:Apache License

private Double getEvaluation(FastByIDMap<PreferenceArray> testPrefs, Recommender recommender)
        throws TasteException {
    reset();// ww  w. j  ava2 s .c om
    Collection<Callable<Void>> estimateCallables = Lists.newArrayList();
    noEstimateCounter = new AtomicInteger();
    estimateCounter = new AtomicInteger();
    for (Map.Entry<Integer, PreferenceArray> entry : testPrefs.entrySet()) {
        estimateCallables.add(new PreferenceEstimateCallable(recommender, entry.getKey(), entry.getValue(),
                noEstimateCounter, estimateCounter));
    }
    log.info("Beginning evaluation of {} users", estimateCallables.size());
    RunningAverageAndStdDev timing = new FullRunningAverageAndStdDev();
    execute(estimateCallables, noEstimateCounter, estimateCounter, timing);
    return computeFinalEvaluation();
}