Example usage for org.apache.mahout.cf.taste.model DataModel getPreferencesForItem

List of usage examples for org.apache.mahout.cf.taste.model DataModel getPreferencesForItem

Introduction

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

Prototype

PreferenceArray getPreferencesForItem(long itemID) throws TasteException;

Source Link

Usage

From source file:com.anjuke.romar.mahout.RecommenderWrapper.java

License:Apache License

@Override
public void removeItem(long itemID) throws TasteException {
    DataModel dataModel = _recommender.getDataModel();
    for (Preference p : dataModel.getPreferencesForItem(itemID)) {
        removePreference(p.getUserID(), itemID);
    }/*from   w w w . j a  v  a2s . co m*/
}

From source file:lib.similarity.AbstractUnboundedSimilarity.java

License:Apache License

@Override
public final double itemSimilarity(long itemID1, long itemID2) throws TasteException {
    DataModel dataModel = getDataModel();
    PreferenceArray xPrefs = dataModel.getPreferencesForItem(itemID1);
    PreferenceArray yPrefs = dataModel.getPreferencesForItem(itemID2);
    int xLength = xPrefs.length();
    int yLength = yPrefs.length();

    if (xLength == 0 || yLength == 0) {
        return Double.NaN;
    }//from   w  ww .ja va  2  s .co m

    long xIndex = xPrefs.getUserID(0);
    long yIndex = yPrefs.getUserID(0);
    int xPrefIndex = 0;
    int yPrefIndex = 0;

    // No, pref inferrers and transforms don't appy here. I think.

    SimilarityAccumulator accum = getSimilarityAccumulator();

    while (true) {
        int compare = xIndex < yIndex ? -1 : xIndex > yIndex ? 1 : 0;
        if (compare == 0) {
            // Both users expressed a preference for the item
            double x = xPrefs.getValue(xPrefIndex);
            double y = yPrefs.getValue(yPrefIndex);

            accum.processPrefPair(x, y);
        }
        if (compare <= 0) {
            if (++xPrefIndex == xLength) {
                break;
            }
            xIndex = xPrefs.getUserID(xPrefIndex);
        }
        if (compare >= 0) {
            if (++yPrefIndex == yLength) {
                break;
            }
            yIndex = yPrefs.getUserID(yPrefIndex);
        }
    }

    double result;
    result = accum.computeResult();

    if (similarityTransform != null) {
        result = similarityTransform.transformSimilarity(itemID1, itemID2, result);
    }

    return result;
}

From source file:org.easyrec.mahout.EasyrecInMemoryDataModelTest.java

License:Open Source License

@Test
public void testEasyrecDataModel_getPreferencesForItem() throws TasteException {
    DataModel easyrecDataModel = new EasyrecDataModel(TENANT_ID, RATE_ACTION_TYPE_ID, HAS_RATING_VALUES,
            mahoutDataModelMappingDAO);//from  www . j a  v a 2 s  . co m
    easyrecDataModel = new EasyrecInMemoryDataModel(easyrecDataModel);

    PreferenceArray preferences = easyrecDataModel.getPreferencesForItem(1);

    String ids = "";
    for (long id : preferences.getIDs()) {
        ids += id;
    }

    assertEquals("12", ids);

}

From source file:org.easyrec.mahout.EasyrecInMemoryDataModelTest.java

License:Open Source License

@Test
public void testEasyrecDataModel_getBooleanPreferencesForItem() throws TasteException {
    DataModel easyrecDataModel = new EasyrecDataModel(TENANT_ID, BUY_ACTION_TYPE_ID, HAS_NO_RATING_VALUES,
            mahoutDataModelMappingDAO);//from  ww w  .j av  a 2  s.  c  o m
    easyrecDataModel = new EasyrecInMemoryDataModel(easyrecDataModel);

    PreferenceArray preferences = easyrecDataModel.getPreferencesForItem(10);

    String ids = "";
    for (long id : preferences.getIDs()) {
        ids += id;
    }

    assertEquals("12", ids);

}