예제 #1
0
        private List <ProductRating> GetNearestNeighbors(ObjectId productId, int numProducts)
        {
            List <ProductRating> neighbors = new List <ProductRating>();
            int mainProductIndex           = ratings.ProductIndexToID.IndexOf(productId);

            for (int productIndex = 0; productIndex < ratings.ProductIndexToID.Count; productIndex++)
            {
                ObjectId searchProductId = ratings.ProductIndexToID[productIndex];
                if (productIndex != mainProductIndex)
                {
                    double score = comparer.CompareVectors(transposedRatings[mainProductIndex], transposedRatings[productIndex]);

                    neighbors.Add(new ProductRating(searchProductId, score));
                }
                else
                {
                    double score = double.NegativeInfinity;
                    neighbors.Add(new ProductRating(searchProductId, score));
                }
            }

            neighbors.Sort((c, n) => n.Rating.CompareTo(c.Rating));

            return(neighbors.Take(numProducts).ToList());
        }
        private List <ArticleRating> GetNearestNeighbors(int articleId, int numArticles)
        {
            List <ArticleRating> neighbors = new List <ArticleRating>();
            int mainArticleIndex           = ratings.ArticleIndexToID.IndexOf(articleId);

            for (int articleIndex = 0; articleIndex < ratings.ArticleIndexToID.Count; articleIndex++)
            {
                int searchArticleId = ratings.ArticleIndexToID[articleIndex];

                double score = comparer.CompareVectors(transposedRatings[mainArticleIndex], transposedRatings[articleIndex]);

                neighbors.Add(new ArticleRating(searchArticleId, score));
            }

            neighbors.Sort((c, n) => n.Rating.CompareTo(c.Rating));

            return(neighbors.Take(numArticles).ToList());
        }
        private List <MenuRating> GetNearestNeighbors(int menuId, int numOfMenus)
        {
            List <MenuRating> neighbors = new List <MenuRating>();
            int mainMenuIndex           = ratings.MenuIndexToId.IndexOf(menuId);

            for (int menuIndex = 0; menuIndex < ratings.MenuIndexToId.Count; menuIndex++)
            {
                long searchMenuId = ratings.MenuIndexToId[menuIndex];

                double score = comparer.CompareVectors(transposedRatings[mainMenuIndex], transposedRatings[menuIndex]);

                neighbors.Add(new MenuRating(searchMenuId, score));
            }

            neighbors.Sort((c, n) => n.Rating.CompareTo(c.Rating));

            return(neighbors.Take(numOfMenus).ToList());
        }
        private List <UserArticleRatings> GetNearestNeighbors(UserArticleRatings user, int numUsers)
        {
            List <UserArticleRatings> neighbors = new List <UserArticleRatings>();

            for (int i = 0; i < ratings.Users.Count; i++)
            {
                if (ratings.Users[i].UserID == user.UserID)
                {
                    ratings.Users[i].Score = double.NegativeInfinity;
                }
                else
                {
                    ratings.Users[i].Score = comparer.CompareVectors(ratings.Users[i].ArticleRatings, user.ArticleRatings);
                }
            }

            var similarUsers = ratings.Users.OrderByDescending(x => x.Score);

            return(similarUsers.Take(numUsers).ToList());
        }