public double predictRating(BookUser user, BookItem item) { switch (type) { case RecommendationType.USER_BASED: return(estimateUserBasedRating(user, item)); case RecommendationType.IMPROVED_USER_BASED: return(estimateUserBasedRating(user, item)); case RecommendationType.ITEM_BASED: return(estimateItemBasedRating(user, item)); case RecommendationType.ITEM_PENALTY_BASED: return(estimateItemBasedRating(user, item)); case RecommendationType.USER_CONTENT_BASED: throw new Exception("Not valid for current similarity type:" + type); case RecommendationType.USER_ITEM_CONTENT_BASED: return(MAX_RATING * similarityMatrix.getValue(user.UserId, item.getId())); default: throw new Exception("Unknown recommendation type:" + type); } }
public double getSimilarity(BookItem i1, BookItem i2) { double sim = similarityMatrix.getValue(i1.getId(), i2.getId()); // verbose - флаг, отвечающий за пояснение всего (многословный дословно) if (verbose) { Console.WriteLine("Item similarity between"); Console.WriteLine(" ItemID: " + i1.getId()); Console.WriteLine(" and"); Console.WriteLine(" ItemID: " + i2.getId()); Console.WriteLine(" is equal to " + sim); } return(sim); }
private double estimateItemBasedRating(BookUser user, BookItem item) { double estimatedRating = item.getAverageRating(); int itemId = item.getId(); int userId = user.UserId; double similaritySum = 0.0; double weightedRatingSum = 0.0; GeneralRating existingRatingByUser = user.getItemRating(item.getId()); if (existingRatingByUser != null) { estimatedRating = existingRatingByUser.getRating(); } else { double similarityBetweenItems = 0; double weightedRating = 0; dataSet.getBooks().ForEach(delegate(BookItem anotherItem) { // Рассматриваются только те элементы, которые оценивал пользователь GeneralRating anotherItemRating = anotherItem.getUserRating(userId); if (anotherItemRating != null) { similarityBetweenItems = similarityMatrix.getValue(itemId, anotherItem.getId()); if (similarityBetweenItems > similarityThreshold) { weightedRating = similarityBetweenItems * anotherItemRating.getRating(); weightedRatingSum += weightedRating; similaritySum += similarityBetweenItems; } } }); if (similaritySum > 0.0) { estimatedRating = weightedRatingSum / similaritySum; } } return(estimatedRating); }
//-------------------------------------------------------------------- // Приватные вспомогательные методы //-------------------------------------------------------------------- private double estimateUserBasedRating(BookUser user, BookItem item) { double estimatedRating = user.getAverageRating(); int itemId = item.getId(); int userId = user.UserId; double similaritySum = 0.0; double weightedRatingSum = 0.0; // Проверка, оценивал ли пользователь этот элемент GeneralRating existingRatingByUser = user.getItemRating(item.getId()); if (existingRatingByUser != null) { estimatedRating = existingRatingByUser.getRating(); } else { dataSet.getUsers().ForEach(delegate(BookUser anotherUser){ GeneralRating itemRating = anotherUser.getItemRating(itemId); if (itemRating != null) { double similarityBetweenUsers = similarityMatrix.getValue(userId, anotherUser.UserId); double ratingByNeighbor = itemRating.getRating(); double weightedRating = similarityBetweenUsers * ratingByNeighbor; weightedRatingSum += weightedRating; similaritySum += similarityBetweenUsers; } }); if (similaritySum > 0.0) { estimatedRating = weightedRatingSum / similaritySum; } } return(estimatedRating); }
private bool skipItem(BookUser user, BookItem item) { bool skipItem = true; //if (isContentBased()) { // if (user.getUserContent(item.getItemContent().getId()) ==null){ // skipItem = false; // } //} if (user.getItemRating(item.getId()) == null) { skipItem = false; } return(skipItem); }