Esempio n. 1
0
        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);
        }