@Test public void testStrategy() throws TasteException { FastIDSet itemIDsFromUser123 = new FastIDSet(); itemIDsFromUser123.add(1L); FastIDSet itemIDsFromUser456 = new FastIDSet(); itemIDsFromUser456.add(1L); itemIDsFromUser456.add(2L); List<Preference> prefs = new ArrayList<Preference>(); prefs.add(new GenericPreference(123L, 1L, 1.0f)); prefs.add(new GenericPreference(456L, 1L, 1.0f)); PreferenceArray preferencesForItem1 = new GenericItemPreferenceArray(prefs); DataModel dataModel = EasyMock.createMock(DataModel.class); EasyMock.expect(dataModel.getPreferencesForItem(1L)).andReturn(preferencesForItem1); EasyMock.expect(dataModel.getItemIDsFromUser(123L)).andReturn(itemIDsFromUser123); EasyMock.expect(dataModel.getItemIDsFromUser(456L)).andReturn(itemIDsFromUser456); PreferenceArray prefArrayOfUser123 = new GenericUserPreferenceArray(Arrays.asList(new GenericPreference(123L, 1L, 1.0f))); CandidateItemsStrategy strategy = new PreferredItemsNeighborhoodCandidateItemsStrategy(); EasyMock.replay(dataModel); FastIDSet candidateItems = strategy.getCandidateItems(123L, prefArrayOfUser123, dataModel); assertEquals(1, candidateItems.size()); assertTrue(candidateItems.contains(2L)); EasyMock.verify(dataModel); }
private double doItemSimilarity(long itemID1, long itemID2, FastIDSet preferring1) throws TasteException { double intersection = 0.0; double union = 0.0; for (Preference pref : getDataModel().getPreferencesForItem(itemID2)) { long userID = pref.getUserID(); double weight = (double) getDataModel().getNumItems() / mUserPrefNum.get(userID); if (preferring1.contains(userID)) { intersection += weight; union -= weight; } union += weight; } for (LongPrimitiveIterator it_user = preferring1.iterator(); it_user.hasNext(); ) { long userID = (long) it_user.nextLong(); double weight = (double) getDataModel().getNumItems() / mUserPrefNum.get(userID); union += weight; } if (intersection == 0) { return Double.NaN; } return Math.log(intersection) / Math.log(union); }
@Override public Float getPreferenceValue(long userID, long itemID) throws NoSuchUserException { FastIDSet itemIDs = preferenceFromUsers.get(userID); if (itemIDs == null) { throw new NoSuchUserException(userID); } if (itemIDs.contains(itemID)) { return 1.0f; } return null; }
@Override public double userSimilarity(long userID1, long userID2) throws TasteException { DataModel dataModel = getDataModel(); FastIDSet xPrefs = dataModel.getItemIDsFromUser(userID1); FastIDSet yPrefs = dataModel.getItemIDsFromUser(userID2); int xPrefsSize = xPrefs.size(); int yPrefsSize = yPrefs.size(); if (xPrefsSize == 0 && yPrefsSize == 0) { return Double.NaN; } if (xPrefsSize == 0 || yPrefsSize == 0) { return 0.0; } double intersection = 0.0; double union = 0.0; for (LongPrimitiveIterator it_item = xPrefs.iterator(); it_item.hasNext(); ) { long itemID = (long) it_item.nextLong(); double weight = (double) getDataModel().getNumUsers() / mItemPrefNum.get(itemID); if (yPrefs.contains(itemID)) { intersection += weight; union -= weight; } union += weight; } for (LongPrimitiveIterator it_item = yPrefs.iterator(); it_item.hasNext(); ) { long itemID = (long) it_item.nextLong(); double weight = (double) getDataModel().getNumUsers() / mItemPrefNum.get(itemID); union += weight; } return Math.log(intersection) / Math.log(union); }