Esempio n. 1
0
        public MainForm()
        {
            InitializeComponent();

            IRater    rate    = new LinearRater(-4, 2, 3, 1);
            IComparer compare = new CorrelationUserComparer();

            recommender = new UserCollaborativeFilterRecommender(compare, rate, 50);

            if (File.Exists(savedModel))
            {
                try
                {
                    recommender.Load(savedModel);
                    rtbOutput.Text = "Loaded model from file";
                    EnableForm(true);
                }
                catch
                {
                    rtbOutput.Text = "Saved model is corrupt";
                }
            }

            //RecommenderTests.TestAllRecommenders();
            //RecommenderTests.FindBestRaterWeights();
        }
        public static void FindBestRaterWeights()
        {
            using (StreamWriter w = new StreamWriter("rater-weights.csv", true))
            {
                w.WriteLine("down,up,view,dl,rmse,solved,total,precision,rank");
            }

            var once    = new User(0, "");
            var options = new List <dynamic>();

            for (double up = 0.0; up < 5.0; up += 1)
            {
                for (double down = -5.0; down <= 0.0; down += 1)
                {
                    for (double dl = 0.0; dl < 5.0; dl += 1)
                    {
                        for (double view = 0.0; view < 5.0; view += 1)
                        {
                            options.Add(new { up, down, dl, view });
                        }
                    }
                }
            }

            var dbp = new UserBehaviorDatabaseParser();
            var db  = dbp.LoadUserBehaviorDatabase("UserBehaviour.txt");
            var sp  = new DaySplitter(db, 3);
            var cp  = new CorrelationUserComparer();

            Parallel.ForEach(options, set =>
            {
                try
                {
                    var rate = new LinearRater(set.down, set.up, set.view, set.dl);
                    var mfr  = new MatrixFactorizationRecommender(20, rate);
                    //var mfr = new UserCollaborativeFilterRecommender(cp, rate, set.features);

                    mfr.Train(sp.TrainingDB);

                    var score   = mfr.Score(sp.TestingDB, rate);
                    var results = mfr.Test(sp.TestingDB, 100);

                    lock (once)
                    {
                        using (StreamWriter w = new StreamWriter("rater-weights.csv", true))
                        {
                            w.WriteLine(set.down + "," + set.up + "," + set.view + "," + set.dl + "," + score.RootMeanSquareDifference + "," + results.UsersSolved + "," + results.TotalUsers + "," + results.AveragePrecision + "," + results.AverageRecall);
                        }
                    }
                }
                catch (Exception ex)
                {
                    File.WriteAllText("errors.txt", ex.ToString());
                }
            });
        }
        public static void TestAllRecommenders()
        {
            UserBehaviorDatabaseParser dbp = new UserBehaviorDatabaseParser();
            UserBehaviorDatabase       db  = dbp.LoadUserBehaviorDatabase("UserBehaviour.txt");

            //var ubt = new UserBehaviorTransformer(db);
            //var uart = ubt.GetUserArticleRatingsTable();
            //uart.SaveSparcityVisual("sparcity.bmp");
            //uart.SaveUserRatingDistribution("distrib.csv");
            //uart.SaveArticleRatingDistribution("distriba.csv");

            var rate = new LinearRater();
            var sp   = new DaySplitter(db, 5);
            var uc   = new CorrelationUserComparer();

            //var rr = new RandomRecommender();
            //rr.Train(sp.TrainingDB);
            //ScoreResults scores5 = rr.Score(sp.TestingDB, rate);
            //TestResults results5 = rr.Test(sp.TestingDB, 30);

            var ubc = new UserCollaborativeFilterRecommender(uc, rate, 30);
            var mfr = new MatrixFactorizationRecommender(30, rate);
            var icf = new ItemCollaborativeFilterRecommender(uc, rate, 30);
            var hbr = new HybridRecommender(ubc, mfr, icf);

            hbr.Train(sp.TrainingDB);
            ScoreResults scores1  = hbr.Score(sp.TestingDB, rate);
            TestResults  results1 = hbr.Test(sp.TestingDB, 30);

            ubc = new UserCollaborativeFilterRecommender(uc, rate, 30);
            mfr = new MatrixFactorizationRecommender(30, rate);
            icf = new ItemCollaborativeFilterRecommender(uc, rate, 30);

            ubc.Train(sp.TrainingDB);
            ScoreResults scores2  = ubc.Score(sp.TestingDB, rate);
            TestResults  results2 = ubc.Test(sp.TestingDB, 30);

            mfr.Train(sp.TrainingDB);
            ScoreResults scores3  = mfr.Score(sp.TestingDB, rate);
            TestResults  results3 = mfr.Test(sp.TestingDB, 30);

            icf.Train(sp.TrainingDB);
            ScoreResults scores4  = icf.Score(sp.TestingDB, rate);
            TestResults  results4 = icf.Test(sp.TestingDB, 30);

            using (StreamWriter w = new StreamWriter("results.csv"))
            {
                w.WriteLine("model,rmse,users,user-solved,precision,recall");
                w.WriteLine("UCF," + scores2.RootMeanSquareDifference + "," + results2.TotalUsers + "," + results2.UsersSolved + "," + results2.AveragePrecision + "," + results2.AverageRecall);
                w.WriteLine("SVD," + scores3.RootMeanSquareDifference + "," + results3.TotalUsers + "," + results3.UsersSolved + "," + results3.AveragePrecision + "," + results3.AverageRecall);
                w.WriteLine("ICF," + scores4.RootMeanSquareDifference + "," + results4.TotalUsers + "," + results4.UsersSolved + "," + results4.AveragePrecision + "," + results4.AverageRecall);
                w.WriteLine("HR," + scores1.RootMeanSquareDifference + "," + results1.TotalUsers + "," + results1.UsersSolved + "," + results1.AveragePrecision + "," + results1.AverageRecall);
            }
        }
Esempio n. 4
0
        public List <Suggestion> GetSuggest(UserBehavior db, long userId)
        {
            IRater    rater    = new SimpleRater();
            IComparer comparer = new CorrelationUserComparer();

            recommender = new ItemCollaborativeFilterRecommender(comparer, rater, 50);
            recommender.Train(db);

            var suggestion = recommender.GetSuggestions(userId, 500);

            return(suggestion);
        }
        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));
            }
        }