Exemplo n.º 1
0
        public static ScoreResults Score(this IRecommender classifier, UserBehaviorDatabase db, IRater rater)
        {
            UserBehaviorTransformer ubt           = new UserBehaviorTransformer(db);
            UserArticleRatingsTable actualRatings = ubt.GetUserArticleRatingsTable(rater);

            var distinctUserArticlePairs = db.UserActions.GroupBy(x => new { x.UserID, x.ArticleID }).ToList();

            double score = 0.0;
            int    count = 0;

            foreach (var userArticle in distinctUserArticlePairs)
            {
                int userIndex    = actualRatings.UserIndexToID.IndexOf(userArticle.Key.UserID);
                int articleIndex = actualRatings.ArticleIndexToID.IndexOf(userArticle.Key.ArticleID);

                double actualRating = actualRatings.Users[userIndex].ArticleRatings[articleIndex];

                if (actualRating != 0)
                {
                    double predictedRating = classifier.GetRating(userArticle.Key.UserID, userArticle.Key.ArticleID);

                    score += Math.Pow(predictedRating - actualRating, 2);
                    count++;
                }
            }

            if (count > 0)
            {
                score = Math.Sqrt(score / count);
            }

            return(new ScoreResults(score));
        }
Exemplo n.º 2
0
        public ActionResult Details(int id)
        {
            UserBehaviorDatabaseParser parser = new UserBehaviorDatabaseParser();

            UserBehaviorDatabase db1 = parser.LoadUserBehaviorDatabase("/Data/NewBehavior.txt");

            UserBehaviorTransformer ubt = new UserBehaviorTransformer(db1);
            int    userid = Convert.ToInt32(Session["id"].ToString());
            string email  = context.login.Where(m => m.Id == userid).FirstOrDefault().Email;
            int    realid = context.students.Where(m => m.Email == email).FirstOrDefault().Id;

            string           name  = context.students.Where(m => m.Id == realid).FirstOrDefault().FirstName;
            UplodedFile      admin = context.ufiles.Find(id);
            SimilarViewModel sam   = new SimilarViewModel();

            sam.Description     = admin.Description;
            sam.Id              = admin.Id;
            sam.Name            = admin.Name;
            sam.UpdatedFileName = admin.UpdatedFileName;
            sam.UplodedBy       = admin.UplodedBy;
            sam.UplodedDate     = admin.UplodedDate;
            sam.path            = context.ufiles.Where(m => m.Id == id).FirstOrDefault().Filename;
            UserArticleRatingsTable ratings1;
            IRater rate = new LinearRater(-4, 2, 0.5, 1);

            ratings1 = ubt.GetUserArticleRatingsTable(rate);
            List <SuggestedArticlePoints> SAT = ratings1.suggestArticle(ratings1.art, id);

            List <UplodedFile> up  = new List <UplodedFile>();
            UplodedFile        upa = new UplodedFile();
            int           i        = 0;
            List <double> p        = new List <double>();

            foreach (var item in SAT)
            {
                p.Add(item.Points);



                upa = context.ufiles.Where(m => m.Id == item.ArticleId).FirstOrDefault();
                up.Add(upa);
            }
            sam.point        = p;
            sam.uplodedFiles = up;
            double        a     = sam.point[0];
            var           text  = System.IO.File.ReadAllText("/Data/NewBehavior.txt");
            List <string> lines = System.IO.File.ReadAllLines("/Data/NewBehavior.txt").ToList();
            int           index = text.IndexOf("# End");

            text = text.Insert(index, "1,View," + realid + "," + name + "," + admin.Id + "," + admin.Name + Environment.NewLine);
            System.IO.File.WriteAllText("/Data/NewBehavior.txt", text);



            return(View(sam));
        }
        public void Train(UserBehavior db)
        {
            UserBehaviorTransformer ubt = new UserBehaviorTransformer(db);

            ratings = ubt.GetUserMenuRatingsTable(rater);

            List <MenuCategoryCounts> menuCategories = ubt.GetMenuCategoryCounts();

            ratings.AppendMenuFeatures(menuCategories);

            FillTransposedRatings();
        }
Exemplo n.º 4
0
        public void Train(UserBehaviorDatabase db)
        {
            UserBehaviorTransformer ubt = new UserBehaviorTransformer(db);

            ratings = ubt.GetUserProductRatingsTable(rater);

            List <ProductCategoryCount> productCategories = ubt.GetProductCategoryCounts();

            ratings.AppendProductFeatures(productCategories);

            FillTransposedRatings();
        }
Exemplo n.º 5
0
        public void Train(UserBehaviorDatabase db)
        {
            UserBehaviorTransformer ubt = new UserBehaviorTransformer(db);

            ratings = ubt.GetUserArticleRatingsTable(rater);

            List <ArticleTagCounts> articleTags = ubt.GetArticleTagCounts();

            ratings.AppendArticleFeatures(articleTags);

            FillTransposedRatings();
        }
        public static TestResults Test(this IRecommender classifier, UserBehaviorDatabase db, int numSuggestions)
        {
            // We're only using the ratings to check for existence of a rating, so we can use a simple rater for everything
            SimpleRater             rater   = new SimpleRater();
            UserBehaviorTransformer ubt     = new UserBehaviorTransformer(db);
            UserArticleRatingsTable ratings = ubt.GetUserArticleRatingsTable(rater);

            int    correctUsers     = 0;
            double averagePrecision = 0.0;
            double averageRecall    = 0.0;

            // Get a list of users in this database who interacted with an article for the first time
            List <int> distinctUsers = db.UserActions.Select(x => x.UserID).Distinct().ToList();

            var distinctUserArticles = db.UserActions.GroupBy(x => new { x.UserID, x.ArticleID });

            // Now get suggestions for each of these users
            foreach (int user in distinctUsers)
            {
                List <Suggestion> suggestions = classifier.GetSuggestions(user, numSuggestions);
                bool foundOne  = false;
                int  userIndex = ratings.UserIndexToID.IndexOf(user);

                int userCorrectArticles = 0;
                int userTotalArticles   = distinctUserArticles.Count(x => x.Key.UserID == user);

                foreach (Suggestion s in suggestions)
                {
                    int articleIndex = ratings.ArticleIndexToID.IndexOf(s.ArticleID);

                    // If one of the top N suggestions is what the user ended up reading, then we're golden
                    if (ratings.Users[userIndex].ArticleRatings[articleIndex] != 0)
                    {
                        userCorrectArticles++;

                        if (!foundOne)
                        {
                            correctUsers++;
                            foundOne = true;
                        }
                    }
                }

                averagePrecision += (double)userCorrectArticles / numSuggestions;
                averageRecall    += (double)userCorrectArticles / userTotalArticles;
            }

            averagePrecision /= distinctUsers.Count;
            averageRecall    /= distinctUsers.Count;

            return(new TestResults(distinctUsers.Count, correctUsers, averageRecall, averagePrecision));
        }
Exemplo n.º 7
0
        public void Train(UserBehaviorDatabase db)
        {
            UserBehaviorTransformer ubt = new UserBehaviorTransformer(db);

            ratings = ubt.GetUserArticleRatingsTable(rater);

            SingularValueDecomposition factorizer = new SingularValueDecomposition(numFeatures, learningIterations);

            svd = factorizer.FactorizeMatrix(ratings);

            numUsers    = ratings.UserIndexToID.Count;
            numArticles = ratings.ArticleIndexToID.Count;
        }
        public void Train(UserBehaviorDatabase db)
        {
            UserBehaviorTransformer ubt = new UserBehaviorTransformer(db);

            ratings = ubt.GetUserArticleRatingsTable(rater);

            if (latentUserFeatureCount > 0)
            {
                SingularValueDecomposition svd = new SingularValueDecomposition(latentUserFeatureCount, 100);
                SvdResult results = svd.FactorizeMatrix(ratings);

                ratings.AppendUserFeatures(results.UserFeatures);
            }
        }
        public void Train(UserBehaviorDatabase db)
        {
            UserBehaviorTransformer ubt = new UserBehaviorTransformer(db);

            ratings = ubt.GetUserArticleRatingsTable(rater);

            List <ArticleTagCounts> articleTags = ubt.GetArticleTagCounts();
            //train article
            List <ArticleAndTag> articles1 = ubt.Angualr2();

            ratings.AppendArticleFeatures(articleTags);
            // ratings.suggestArticle(articles1, 3);

            FillTransposedRatings();
        }
        public void Train(UserBehaviorDatabase db)
        {
            UserBehaviorTransformer ubt = new UserBehaviorTransformer(db);

            ratings     = ubt.GetUserArticleRatingsTable(rater);
            ratings.art = ubt.Angualr2();
            List <ArticleAndTag> articles1 = ubt.Angualr2();

            //if (latentUserFeatureCount > 0)
            //{
            //    SingularValueDecomposition svd = new SingularValueDecomposition(latentUserFeatureCount, 100);
            //    SvdResult results = svd.FactorizeMatrix(ratings);

            //    ratings.AppendUserFeatures(results.UserFeatures);
            //}
        }
Exemplo n.º 11
0
        public void Train(UserBehaviorDatabase db)
        {
            UserBehaviorTransformer ubt = new UserBehaviorTransformer(db);

            ratings = ubt.GetUserProductRatingsTable(rater);
        }
Exemplo n.º 12
0
        public ActionResult Index(string search = "")
        {
            if (search == "")
            {
                int       id      = Convert.ToInt32(Session["id"].ToString());
                string    email   = context.login.Where(m => m.Id == id).FirstOrDefault().Email;
                int       realid  = context.students.Where(m => m.Email == email).FirstOrDefault().Id;
                IRater    rate    = new LinearRater(-4, 2, 0.5, 1);
                IComparer compare = new CorrelationUserComparer();
                recommender = new UserCollaborativeFilterRecommender(compare, rate, 200);
                UserBehaviorDatabaseParser parser = new UserBehaviorDatabaseParser();
                UserBehaviorDatabase       db1    = parser.LoadUserBehaviorDatabase("/Data/NewBehavior.txt");
                UserBehaviorTransformer    ubt    = new UserBehaviorTransformer(db1);
                recommender.Train(db1);



                int userId;
                int ratings;
                userId  = realid;
                ratings = 2;
                List <Suggestion>         result  = new List <Suggestion>();
                List <RecomendedArticles> rem     = new List <RecomendedArticles>();
                List <Suggestion>         result2 = new List <Suggestion>();

                RecomendedArticles recom;
                if (ratings >= 1 && ratings <= 100)
                {
                    new GetRecommendation {
                        UserID = userId, Ratings = ratings
                    };
                    result  = recommender.GetSuggestions(userId, ratings);
                    result2 = recommender.GetSuggestions(userId, 6);
                }

                foreach (Suggestion suggestion in result)
                {
                    var ye = context.ufiles.Where(m => m.Id == suggestion.ArticleID).FirstOrDefault();
                    recom = new RecomendedArticles()
                    {
                        Name            = ye.Name,
                        UpdatedFileName = ye.UpdatedFileName,
                        UplodedBy       = ye.UplodedBy,
                        Description     = ye.Description,
                        Filename        = ye.Filename,
                        imagepath       = ye.imagepath,
                        UplodedDate     = ye.UplodedDate,
                        Rating          = suggestion.Rating,
                        Id = ye.Id,
                    };
                    rem.Add(recom);
                }
                NRViewModel recomendedArticles = new NRViewModel();

                recomendedArticles.uplodedFiles       = context.ufiles.OrderByDescending(m => m.Id).Take(6).ToList();
                recomendedArticles.RecomendedArticles = rem;
                return(View(recomendedArticles));
            }
            else
            {
                NRViewModel recomendedArticles = new NRViewModel();

                recomendedArticles.uplodedFiles = context.ufiles.OrderByDescending(m => m.Id).Where(m => m.Name.Contains(search)).Take(6).ToList();
                if (recomendedArticles.uplodedFiles == null)
                {
                    ViewBag.messagea = "no item found";
                }
                ViewBag.message = "search";
                return(View(recomendedArticles));
            }
        }