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

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

Introduction

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

Prototype

void sortByItem();

Source Link

Document

Sorts underlying array by item ID, ascending.

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   w ww  .  java2  s  .c  o m*/

    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);
}