public int Compare(IRecommendedItem o1, IRecommendedItem o2) { double rescored1; double rescored2; if (rescorer == null) { rescored1 = o1.GetValue(); rescored2 = o2.GetValue(); } else { rescored1 = rescorer.rescore(o1.GetItemID(), o1.GetValue()); rescored2 = rescorer.rescore(o2.GetItemID(), o2.GetValue()); } if (rescored1 < rescored2) { return(1); } else if (rescored1 > rescored2) { return(-1); } else { return(0); } }
public void testUserRescorer() { IDRescorer rescorer = NullRescorer.getUserInstance(); Assert.NotNull(rescorer); Assert.AreEqual(1.0, rescorer.rescore(1L, 1.0), EPSILON); Assert.AreEqual(1.0, rescorer.rescore(0L, 1.0), EPSILON); Assert.AreEqual(0.0, rescorer.rescore(1L, 0.0), EPSILON); Assert.True(Double.IsNaN(rescorer.rescore(1L, Double.NaN))); }
public static List <IRecommendedItem> GetTopItems(int howMany, IEnumerator <long> possibleItemIDs, IDRescorer rescorer, IEstimator <long> estimator) { //Preconditions.checkArgument(possibleItemIDs != null, "possibleItemIDs is null"); //Preconditions.checkArgument(estimator != null, "estimator is null"); var topItems = new SortedSet <IRecommendedItem>(ByValueRecommendedItemComparator.getReverseInstance()); bool full = false; double lowestTopValue = Double.NegativeInfinity; while (possibleItemIDs.MoveNext()) { long itemID = possibleItemIDs.Current; if (rescorer == null || !rescorer.isFiltered(itemID)) { double preference; try { preference = estimator.Estimate(itemID); } catch (NoSuchItemException nsie) { continue; } double rescoredPref = rescorer == null ? preference : rescorer.rescore(itemID, preference); if (!Double.IsNaN(rescoredPref) && (!full || rescoredPref > lowestTopValue)) { topItems.Add(new GenericRecommendedItem(itemID, (float)rescoredPref)); if (full) { topItems.Remove(topItems.Min); } else if (topItems.Count > howMany) { full = true; topItems.Remove(topItems.Min); // topItems.poll(); } lowestTopValue = topItems.Min.GetValue(); } } } int size = topItems.Count; if (size == 0) { return(new List <IRecommendedItem>()); } List <IRecommendedItem> result = new List <IRecommendedItem>(size); result.AddRange(topItems); result.Reverse(); //Collections.sort(result, ByValueRecommendedItemComparator.getInstance()); return(result); }
public static List <RecommendedItem> getTopItems(int howMany, IEnumerator <long> possibleItemIDs, IDRescorer rescorer, Estimator <long> estimator) { SortedSet <RecommendedItem> collection = new SortedSet <RecommendedItem>(ByValueRecommendedItemComparator.getReverseInstance()); bool flag = false; double negativeInfinity = double.NegativeInfinity; while (possibleItemIDs.MoveNext()) { long current = possibleItemIDs.Current; if ((rescorer == null) || !rescorer.isFiltered(current)) { double num3; try { num3 = estimator.estimate(current); } catch (NoSuchItemException) { continue; } double d = (rescorer == null) ? num3 : rescorer.rescore(current, num3); if (!double.IsNaN(d) && (!flag || (d > negativeInfinity))) { collection.Add(new GenericRecommendedItem(current, (float)d)); if (flag) { collection.Remove(collection.Min); } else if (collection.Count > howMany) { flag = true; collection.Remove(collection.Min); } negativeInfinity = collection.Min.getValue(); } } } int count = collection.Count; if (count == 0) { return(new List <RecommendedItem>()); } List <RecommendedItem> list = new List <RecommendedItem>(count); list.AddRange(collection); list.Reverse(); return(list); }
public static long[] GetTopUsers(int howMany, IEnumerator <long> allUserIDs, IDRescorer rescorer, IEstimator <long> estimator) { var topUsers = new SortedSet <SimilarUser>(); bool full = false; double lowestTopValue = Double.NegativeInfinity; while (allUserIDs.MoveNext()) { long userID = allUserIDs.Current; if (rescorer != null && rescorer.isFiltered(userID)) { continue; } double similarity; try { similarity = estimator.Estimate(userID); } catch (NoSuchUserException nsue) { continue; } double rescoredSimilarity = rescorer == null ? similarity : rescorer.rescore(userID, similarity); if (!Double.IsNaN(rescoredSimilarity) && (!full || rescoredSimilarity > lowestTopValue)) { topUsers.Add(new SimilarUser(userID, rescoredSimilarity)); if (full) { topUsers.Remove(topUsers.Max); // topUsers.poll(); } else if (topUsers.Count > howMany) { full = true; topUsers.Remove(topUsers.Max); // topUsers.poll(); } lowestTopValue = topUsers.Max.getSimilarity(); } } int size = topUsers.Count; if (size == 0) { return(NO_IDS); } List <SimilarUser> sorted = new List <SimilarUser>(size); return(topUsers.Select(s => s.getUserID()).ToArray()); }
public static long[] getTopUsers(int howMany, IEnumerator <long> allUserIDs, IDRescorer rescorer, Estimator <long> estimator) { SortedSet <SimilarUser> set = new SortedSet <SimilarUser>(); bool flag = false; double negativeInfinity = double.NegativeInfinity; while (allUserIDs.MoveNext()) { long current = allUserIDs.Current; if ((rescorer == null) || !rescorer.isFiltered(current)) { double num3; try { num3 = estimator.estimate(current); } catch (NoSuchUserException) { continue; } double d = (rescorer == null) ? num3 : rescorer.rescore(current, num3); if (!double.IsNaN(d) && (!flag || (d > negativeInfinity))) { set.Add(new SimilarUser(current, d)); if (flag) { set.Remove(set.Max); } else if (set.Count > howMany) { flag = true; set.Remove(set.Max); } negativeInfinity = set.Max.getSimilarity(); } } } int count = set.Count; if (count == 0) { return(NO_IDS); } List <SimilarUser> list = new List <SimilarUser>(count); return((from s in set select s.getUserID()).ToArray <long>()); }
public override List <RecommendedItem> recommend(long userID, int howMany, IDRescorer rescorer) { // Preconditions.checkArgument(howMany >= 1, "howMany must be at least 1"); if (howMany < 1) { throw new Exception("howMany must be at least 1"); } ; buildClusters(); log.debug("Recommending items for user ID '{}'", userID); List <RecommendedItem> recommended = topRecsByUserID.get(userID); if (recommended == null) { return(new List <RecommendedItem>()); } DataModel dataModel = getDataModel(); List <RecommendedItem> rescored = new List <RecommendedItem>(); //Lists.newArrayListWithCapacity(recommended.size()); // Only add items the user doesn't already have a preference for. // And that the rescorer doesn't "reject". foreach (RecommendedItem recommendedItem in recommended) { long itemID = recommendedItem.getItemID(); if (rescorer != null && rescorer.isFiltered(itemID)) { continue; } if (dataModel.getPreferenceValue(userID, itemID) == null && (rescorer == null || !Double.IsNaN(rescorer.rescore(itemID, recommendedItem.getValue())))) { rescored.Add(recommendedItem); } } rescored.Sort(new ByRescoreComparator(rescorer)); return(rescored); }