Example usage for org.apache.mahout.cf.taste.impl.common SamplingLongPrimitiveIterator maybeWrapIterator

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

Introduction

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

Prototype

public static LongPrimitiveIterator maybeWrapIterator(LongPrimitiveIterator delegate, double samplingRate) 

Source Link

Usage

From source file:com.webir.popcornsaver.cluster.FarthestNeighborClusterSimilarity.java

License:Apache License

@Override
public double getSimilarity(FastIDSet cluster1, FastIDSet cluster2) throws TasteException {
    if (cluster1.isEmpty() || cluster2.isEmpty()) {
        return Double.NaN;
    }/*from   w w  w. j  a  v  a2  s.  c o  m*/
    double leastSimilarity = Double.POSITIVE_INFINITY;
    LongPrimitiveIterator someUsers = SamplingLongPrimitiveIterator.maybeWrapIterator(cluster1.iterator(),
            samplingRate);
    while (someUsers.hasNext()) {
        long userID1 = someUsers.next();
        LongPrimitiveIterator it2 = cluster2.iterator();
        while (it2.hasNext()) {
            double theSimilarity = similarity.userSimilarity(userID1, it2.next());
            if (theSimilarity < leastSimilarity) {
                leastSimilarity = theSimilarity;
            }
        }
    }
    // We skipped everything? well, at least try comparing the first Users to get some value
    if (leastSimilarity == Double.POSITIVE_INFINITY) {
        return similarity.userSimilarity(cluster1.iterator().next(), cluster2.iterator().next());
    }
    return leastSimilarity;
}