// Написать getTopNRecommendations public static List <PredictedItemRating> getTopNRecommendations(List <PredictedItemRating> recommendations, int topN) { PredictedItemRating.sort(recommendations); List <PredictedItemRating> topRecommendations = new List <PredictedItemRating>(); recommendations.ForEach(delegate(PredictedItemRating r) { if (topRecommendations.Count >= topN) { return; } topRecommendations.Add(r); }); return(topRecommendations); }
public List <PredictedItemRating> recommend(BookUser user, int topN) { List <PredictedItemRating> recommendations = new List <PredictedItemRating>(); double maxRating = -1.0d; for (int i = 0; i < dataSet.getBooks().Count; i++) { if (!skipItem(user, dataSet.getBooks()[i])) { double predictedRating = predictRating(user, dataSet.getBooks()[i]); if (maxRating < predictedRating) { maxRating = predictedRating; } if (!Double.IsNaN(predictedRating)) { recommendations.Add(new PredictedItemRating(user.UserId, dataSet.getBooks()[i].getId(), predictedRating)); } } else { if (verbose) { Console.WriteLine("Skipping item: " + dataSet.getBooks()[i].getName()); } } } this.maxPredictedRating.Add(user.UserId, maxRating); List <PredictedItemRating> topNRecommendations = PredictedItemRating.getTopNRecommendations(recommendations, topN); if (verbose) { PredictedItemRating.printUserRecommendations(user, dataSet, topNRecommendations); } return(topNRecommendations); }