Example usage for org.apache.mahout.cf.taste.model PreferenceArray hasPrefWithItemID

List of usage examples for org.apache.mahout.cf.taste.model PreferenceArray hasPrefWithItemID

Introduction

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

Prototype

boolean hasPrefWithItemID(long itemID);

Source Link

Usage

From source file:norbert.mynemo.core.recommendation.similarity.OriginalSpearmanCorrelationSimilarity.java

License:Apache License

@Override
public double userSimilarity(final long userId1, final long userId2) throws TasteException {

    final PreferenceArray xPrefs = dataModel.getPreferencesFromUser(userId1).clone();
    final PreferenceArray yPrefs = dataModel.getPreferencesFromUser(userId2).clone();

    final int numCommonPref = numCommonPreferences(userId1, userId2);
    if (numCommonPref < 2) {
        return Double.NaN;
    }//from   ww w  .j  av a2  s.  c  om

    xPrefs.sortByItem();
    yPrefs.sortByItem();

    final double[] xCommonPrefs = new double[numCommonPref];
    final double[] yCommonPrefs = new double[numCommonPref];

    // copy the common preferences
    int xNewPrefIndex = 0;
    for (int xIndex = 0; xIndex < xPrefs.length(); xIndex++) {
        if (yPrefs.hasPrefWithItemID(xPrefs.getItemID(xIndex))) {
            xCommonPrefs[xNewPrefIndex] = xPrefs.getValue(xIndex);
            xNewPrefIndex++;
        }
    }
    int yNewPrefIndex = 0;
    for (int yIndex = 0; yIndex < yPrefs.length(); yIndex++) {
        if (xPrefs.hasPrefWithItemID(yPrefs.getItemID(yIndex))) {
            yCommonPrefs[yNewPrefIndex] = yPrefs.getValue(yIndex);
            yNewPrefIndex++;
        }
    }

    return SPEARMAN_CORRELATION.correlation(xCommonPrefs, yCommonPrefs);
}