Exemplo n.º 1
0
        public static void main(String[] args)
        {
            DataModel model = new FileDataModel(new File(args[0]));

            int howMany = 10;
            if (args.Length > 1) {
              howMany = Integer.parseInt(args[1]);
            }

            System.out.println("Run Items");
            ItemSimilarity similarity = new EuclideanDistanceSimilarity(model);
            Recommender recommender = new GenericItemBasedRecommender(model, similarity); // Use an item-item recommender
            for (int i = 0; i < LOOPS; i++) {
              LoadStatistics loadStats = LoadEvaluator.runLoad(recommender, howMany);
              System.out.println(loadStats);
            }

            System.out.println("Run Users");
            UserSimilarity userSim = new EuclideanDistanceSimilarity(model);
            UserNeighborhood neighborhood = new NearestNUserNeighborhood(10, userSim, model);
            recommender = new GenericUserBasedRecommender(model, neighborhood, userSim);
            for (int i = 0; i < LOOPS; i++) {
              LoadStatistics loadStats = LoadEvaluator.runLoad(recommender, howMany);
              System.out.println(loadStats);
            }
        }
Exemplo n.º 2
0
        public ActionResult GetRecommendedFilms(string filmIdsJson)
        {
            var filmIds = JsonConvert.DeserializeObject <long[]>(filmIdsJson);

            var dataModel = GetDataModel();

            // recommendation is performed for the user that is missed in the preferences data
            var plusAnonymModel = new PlusAnonymousUserDataModel(dataModel);
            var prefArr         = new GenericUserPreferenceArray(filmIds.Length);

            prefArr.SetUserID(0, PlusAnonymousUserDataModel.TEMP_USER_ID);
            for (int i = 0; i < filmIds.Length; i++)
            {
                prefArr.SetItemID(i, filmIds[i]);

                // in this example we have no ratings of movies preferred by the user
                prefArr.SetValue(i, 5); // lets assume max rating
            }
            plusAnonymModel.SetTempPrefs(prefArr);

            var similarity       = new LogLikelihoodSimilarity(plusAnonymModel);
            var neighborhood     = new NearestNUserNeighborhood(15, similarity, plusAnonymModel);
            var recommender      = new GenericUserBasedRecommender(plusAnonymModel, neighborhood, similarity);
            var recommendedItems = recommender.Recommend(PlusAnonymousUserDataModel.TEMP_USER_ID, 5, null);

            return(Json(recommendedItems.Select(ri => new Dictionary <string, object>()
            {
                { "film_id", ri.GetItemID() },
                { "rating", ri.GetValue() },
            }).ToArray()));
        }
        public static void main(String[] args)
        {
            DataModel model = new FileDataModel(new File(args[0]));

            int howMany = 10;

            if (args.Length > 1)
            {
                howMany = Integer.parseInt(args[1]);
            }

            System.out.println("Run Items");
            ItemSimilarity similarity  = new EuclideanDistanceSimilarity(model);
            Recommender    recommender = new GenericItemBasedRecommender(model, similarity); // Use an item-item recommender

            for (int i = 0; i < LOOPS; i++)
            {
                LoadStatistics loadStats = LoadEvaluator.runLoad(recommender, howMany);
                System.out.println(loadStats);
            }

            System.out.println("Run Users");
            UserSimilarity   userSim      = new EuclideanDistanceSimilarity(model);
            UserNeighborhood neighborhood = new NearestNUserNeighborhood(10, userSim, model);

            recommender = new GenericUserBasedRecommender(model, neighborhood, userSim);
            for (int i = 0; i < LOOPS; i++)
            {
                LoadStatistics loadStats = LoadEvaluator.runLoad(recommender, howMany);
                System.out.println(loadStats);
            }
        }
Exemplo n.º 4
0
        static void GenericUserBasedRecommenderTest()
        {
            Console.WriteLine("GenericUserBasedRecommenderTest");
            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            watch.Start();

            var model        = new FileDataModel(filePath);
            var similarity   = new PearsonCorrelationSimilarity(model);
            var neighborhood = new NearestNUserNeighborhood(4, similarity, model);
            var recommender  = new GenericUserBasedRecommender(model, neighborhood, similarity);
            var iter         = model.getUserIDs();

            while (iter.MoveNext())
            {
                var userId           = iter.Current;
                var recommendedItems = recommender.recommend(userId, 5);
                Console.Write("uid:" + userId);
                foreach (var ritem in recommendedItems)
                {
                    Console.Write("(" + ritem.getItemID() + "," + ritem.getValue() + ")");
                }
                Console.WriteLine();
            }
            watch.Stop();
            Console.WriteLine(watch.ElapsedMilliseconds);
        }
        private GenericUserBasedRecommender CreateRecommender(string pathToDataFile)
        {
            dataModel = new FileDataModel(pathToDataFile, false, FileDataModel.DEFAULT_MIN_RELOAD_INTERVAL_MS, false);
            var similarity   = new LogLikelihoodSimilarity(dataModel);
            var neighborhood = new NearestNUserNeighborhood(3, similarity, dataModel);
            var recommender  = new GenericUserBasedRecommender(dataModel, neighborhood, similarity);

            return(recommender);
        }
Exemplo n.º 6
0
		public void TestFile()  
        {
			UserCorrelation userCorrelation = new PearsonCorrelation(model);
			UserNeighborhood neighborhood = new NearestNUserNeighborhood(2, userCorrelation, model);
			Recommender recommender = new GenericUserBasedRecommender(model, neighborhood, userCorrelation);
			Assert.AreEqual(2, recommender.Recommend("A123", 3).Count);
			Assert.AreEqual(2, recommender.Recommend("B234", 3).Count);
			Assert.AreEqual(1, recommender.Recommend("C345", 3).Count);

			// Make sure this doesn't throw an exception
			model.Refresh();
		}
Exemplo n.º 7
0
        public void TestDatabase()
        {
            UserCorrelation  userCorrelation = new PearsonCorrelation(model);
            UserNeighborhood neighborhood    = new NearestNUserNeighborhood(2, userCorrelation, model);
            Recommender      recommender     = new GenericUserBasedRecommender(model, neighborhood, userCorrelation);

            Assert.AreEqual(2, recommender.Recommend("A123", 3).Count);
            Assert.AreEqual(2, recommender.Recommend("B234", 3).Count);
            Assert.AreEqual(1, recommender.Recommend("C345", 3).Count);

            // Make sure this doesn't throw an exception
            model.Refresh();
        }
Exemplo n.º 8
0
        public void testFile()
        {
            IUserSimilarity   userSimilarity = new PearsonCorrelationSimilarity(model);
            IUserNeighborhood neighborhood   = new NearestNUserNeighborhood(3, userSimilarity, model);
            IRecommender      recommender    = new GenericUserBasedRecommender(model, neighborhood, userSimilarity);

            Assert.AreEqual(1, recommender.Recommend(123, 3).Count);
            Assert.AreEqual(0, recommender.Recommend(234, 3).Count);
            Assert.AreEqual(1, recommender.Recommend(345, 3).Count);

            // Make sure this doesn't throw an exception
            model.Refresh(null);
        }
Exemplo n.º 9
0
        public void TestIsolatedUser()
        {
            List <User> users = new List <User>(3);

            users.Add(GetUser("test1", 0.1, 0.2));
            users.Add(GetUser("test2", 0.2, 0.3, 0.3, 0.6));
            users.Add(GetUser("test3", 0.4, 0.4, 0.5, 0.9));
            users.Add(GetUser("test4"));
            DataModel            dataModel    = new GenericDataModel(users);
            UserCorrelation      correlation  = new PearsonCorrelation(dataModel);
            UserNeighborhood     neighborhood = new NearestNUserNeighborhood(3, correlation, dataModel);
            UserBasedRecommender recommender  = new GenericUserBasedRecommender(dataModel, neighborhood, correlation);
            ICollection <User>   mostSimilar  = recommender.MostSimilarUsers("test4", 3);

            Assert.IsNotNull(mostSimilar);
            Assert.AreEqual(0, mostSimilar.Count);
        }
Exemplo n.º 10
0
        public long[] GetNNearestNeighborsUsersRecommendations(int numNeighbours, int userId)
        {
            GenericDataModel model = GetUserBasedDataModel();

            EuclideanDistanceSimilarity similarity = new EuclideanDistanceSimilarity(
                model);

            IUserNeighborhood neighborhood = new NearestNUserNeighborhood(
                20, similarity, model);

            long[] neighbors = neighborhood.GetUserNeighborhood(userId);

            var recommender =
                new GenericUserBasedRecommender(model, neighborhood, similarity);
            var recommendedItems = recommender.Recommend(userId, 8);

            return(neighbors);
        }
Exemplo n.º 11
0
        public List <MovieRecommendationDto> GetRecommendedMovies(int[] preferredMovieIds)
        {
            var dataModel = GetDataModel();

            // recommendation is performed for the user that is missed in the preferences data
            var plusAnonymModel = new PlusAnonymousUserDataModel(dataModel);
            var prefArr         = new GenericUserPreferenceArray(preferredMovieIds.Length);

            prefArr.SetUserID(0, PlusAnonymousUserDataModel.TEMP_USER_ID);
            for (int i = 0; i < preferredMovieIds.Length; i++)
            {
                prefArr.SetItemID(i, preferredMovieIds[i]);

                // in this example we have no ratings of movies preferred by the user
                prefArr.SetValue(i, 5); // lets assume max rating
            }
            plusAnonymModel.SetTempPrefs(prefArr);

            var similarity       = new LogLikelihoodSimilarity(plusAnonymModel);
            var neighborhood     = new NearestNUserNeighborhood(15, similarity, plusAnonymModel);
            var recommender      = new GenericUserBasedRecommender(plusAnonymModel, neighborhood, similarity);
            var recommendedItems = recommender.Recommend(PlusAnonymousUserDataModel.TEMP_USER_ID, 5, null);

            var movieIds = recommendedItems.Select(ri => (int)ri.GetItemID()).ToArray();
            var movieIdToTitleDictionary = _unitOfWork.MovieRepository.GetMovieIdToTileDictionary(movieIds);

            var recommendedMovies = new List <MovieRecommendationDto>();

            foreach (var item in recommendedItems)
            {
                var movieId    = (int)item.GetItemID();
                var movieTitle = movieIdToTitleDictionary[movieId];

                recommendedMovies.Add(
                    new MovieRecommendationDto
                {
                    MovieId    = movieId,
                    MovieTitle = movieTitle,
                    Rating     = item.GetValue()
                });
            }

            return(recommendedMovies);
        }
 public void TestHowMany()
 {
     List<User> users = new List<User>(3);
     users.Add(GetUser("test1", 0.1, 0.2));
     users.Add(GetUser("test2", 0.2, 0.3, 0.3, 0.6));
     users.Add(GetUser("test3", 0.4, 0.4, 0.5, 0.9));
     users.Add(GetUser("test4", 0.1, 0.4, 0.5, 0.8, 0.9, 1.0));
     users.Add(GetUser("test5", 0.2, 0.3, 0.6, 0.7, 0.1, 0.2));
     DataModel dataModel = new GenericDataModel(users);
     UserCorrelation correlation = new PearsonCorrelation(dataModel);
     UserNeighborhood neighborhood = new NearestNUserNeighborhood(2, correlation, dataModel);
     Recommender recommender = new GenericUserBasedRecommender(dataModel, neighborhood, correlation);
     IList<RecommendedItem> fewRecommended = recommender.Recommend("test1", 2);
     IList<RecommendedItem> moreRecommended = recommender.Recommend("test1", 4);
     for (int i = 0; i < fewRecommended.Count; i++)
     {
         Assert.AreEqual(fewRecommended[i].Item, moreRecommended[i].Item);
     }
 }
Exemplo n.º 13
0
        public List <int> GetRecommendations(int userId)
        {
            GenericDataModel model = GetUserBasedDataModel();

            EuclideanDistanceSimilarity similarity = new EuclideanDistanceSimilarity(
                model);

            IUserNeighborhood neighborhood = new NearestNUserNeighborhood(
                15, similarity, model);

            long[] neighbors = neighborhood.GetUserNeighborhood(userId);

            var recommender =
                new GenericUserBasedRecommender(model, neighborhood, similarity);
            var recommendedItems = recommender.Recommend(userId, 12);

            var bookIds = recommendedItems.Select(ri => (int)ri.GetItemID()).ToList();

            return(bookIds);
        }
 public void TestRescorer()
 {
     List<User> users = new List<User>(3);
     users.Add(GetUser("test1", 0.1, 0.2));
     users.Add(GetUser("test2", 0.2, 0.3, 0.3, 0.6));
     users.Add(GetUser("test3", 0.4, 0.4, 0.5, 0.9));
     DataModel dataModel = new GenericDataModel(users);
     UserCorrelation correlation = new PearsonCorrelation(dataModel);
     UserNeighborhood neighborhood = new NearestNUserNeighborhood(1, correlation, dataModel);
     Recommender recommender = new GenericUserBasedRecommender(dataModel, neighborhood, correlation);
     IList<RecommendedItem> originalRecommended = recommender.Recommend("test1", 2);
     IList<RecommendedItem> rescoredRecommended =
        recommender.Recommend("test1", 2, new ReversingRescorer<Item>());
     Assert.IsNotNull(originalRecommended);
     Assert.IsNotNull(rescoredRecommended);
     Assert.AreEqual(2, originalRecommended.Count);
     Assert.AreEqual(2, rescoredRecommended.Count);
     Assert.AreEqual(originalRecommended[0].Item, rescoredRecommended[1].Item);
     Assert.AreEqual(originalRecommended[1].Item, rescoredRecommended[0].Item);
 }
Exemplo n.º 15
0
        public void TestHowMany()
        {
            List <User> users = new List <User>(3);

            users.Add(GetUser("test1", 0.1, 0.2));
            users.Add(GetUser("test2", 0.2, 0.3, 0.3, 0.6));
            users.Add(GetUser("test3", 0.4, 0.4, 0.5, 0.9));
            users.Add(GetUser("test4", 0.1, 0.4, 0.5, 0.8, 0.9, 1.0));
            users.Add(GetUser("test5", 0.2, 0.3, 0.6, 0.7, 0.1, 0.2));
            DataModel               dataModel       = new GenericDataModel(users);
            UserCorrelation         correlation     = new PearsonCorrelation(dataModel);
            UserNeighborhood        neighborhood    = new NearestNUserNeighborhood(2, correlation, dataModel);
            Recommender             recommender     = new GenericUserBasedRecommender(dataModel, neighborhood, correlation);
            IList <RecommendedItem> fewRecommended  = recommender.Recommend("test1", 2);
            IList <RecommendedItem> moreRecommended = recommender.Recommend("test1", 4);

            for (int i = 0; i < fewRecommended.Count; i++)
            {
                Assert.AreEqual(fewRecommended[i].Item, moreRecommended[i].Item);
            }
        }
Exemplo n.º 16
0
        public void TestRescorer()
        {
            List <User> users = new List <User>(3);

            users.Add(GetUser("test1", 0.1, 0.2));
            users.Add(GetUser("test2", 0.2, 0.3, 0.3, 0.6));
            users.Add(GetUser("test3", 0.4, 0.4, 0.5, 0.9));
            DataModel               dataModel           = new GenericDataModel(users);
            UserCorrelation         correlation         = new PearsonCorrelation(dataModel);
            UserNeighborhood        neighborhood        = new NearestNUserNeighborhood(1, correlation, dataModel);
            Recommender             recommender         = new GenericUserBasedRecommender(dataModel, neighborhood, correlation);
            IList <RecommendedItem> originalRecommended = recommender.Recommend("test1", 2);
            IList <RecommendedItem> rescoredRecommended =
                recommender.Recommend("test1", 2, new ReversingRescorer <Item>());

            Assert.IsNotNull(originalRecommended);
            Assert.IsNotNull(rescoredRecommended);
            Assert.AreEqual(2, originalRecommended.Count);
            Assert.AreEqual(2, rescoredRecommended.Count);
            Assert.AreEqual(originalRecommended[0].Item, rescoredRecommended[1].Item);
            Assert.AreEqual(originalRecommended[1].Item, rescoredRecommended[0].Item);
        }
Exemplo n.º 17
0
        public ActionResult Test()
        {
            //构建用户行为数组

            string filmIdsJson = "[6090,5461,5426]";

            var filmIds        = (new JavaScriptSerializer()).Deserialize <long[]>(filmIdsJson);
            var pathToDataFile =
                Path.Combine(System.Web.HttpRuntime.AppDomainAppPath, "data/ratings.dat");

            if (dataModel == null)
            {
                dataModel = new FileDataModel(pathToDataFile, false, FileDataModel.DEFAULT_MIN_RELOAD_INTERVAL_MS, false);
            }

            var plusAnonymModel = new PlusAnonymousUserDataModel(dataModel);
            var prefArr         = new GenericUserPreferenceArray(filmIds.Length);

            prefArr.SetUserID(0, PlusAnonymousUserDataModel.TEMP_USER_ID);
            for (int i = 0; i < filmIds.Length; i++)
            {
                prefArr.SetItemID(i, filmIds[i]);
                prefArr.SetValue(i, 5); // lets assume max rating
            }
            plusAnonymModel.SetTempPrefs(prefArr);

            var similarity       = new LogLikelihoodSimilarity(plusAnonymModel);
            var neighborhood     = new NearestNUserNeighborhood(15, similarity, plusAnonymModel);
            var recommender      = new GenericUserBasedRecommender(plusAnonymModel, neighborhood, similarity);
            var recommendedItems = recommender.Recommend(PlusAnonymousUserDataModel.TEMP_USER_ID, 5, null);

            return(Json(recommendedItems.Select(ri => new Dictionary <string, object>()
            {
                { "film_id", ri.GetItemID() },
                { "rating", ri.GetValue() },
            }).ToArray(), JsonRequestBehavior.AllowGet));
        }
 public void testHowMany()
 {
     IDataModel dataModel = getDataModel(
     new long[] {1, 2, 3, 4, 5},
     new Double?[][] {
             new double?[]{0.1, 0.2},
             new double?[]{0.2, 0.3, 0.3, 0.6},
             new double?[]{0.4, 0.4, 0.5, 0.9},
             new double?[]{0.1, 0.4, 0.5, 0.8, 0.9, 1.0},
             new double?[]{0.2, 0.3, 0.6, 0.7, 0.1, 0.2},
     });
     IUserSimilarity similarity = new PearsonCorrelationSimilarity(dataModel);
     IUserNeighborhood neighborhood = new NearestNUserNeighborhood(2, similarity, dataModel);
     IRecommender recommender = new GenericUserBasedRecommender(dataModel, neighborhood, similarity);
     IList<IRecommendedItem> fewRecommended = recommender.Recommend(1, 2);
     IList<IRecommendedItem> moreRecommended = recommender.Recommend(1, 4);
     for (int i = 0; i < fewRecommended.Count; i++) {
       Assert.AreEqual(fewRecommended[i].GetItemID(), moreRecommended[i].GetItemID());
     }
     recommender.Refresh(null);
     for (int i = 0; i < fewRecommended.Count; i++) {
       Assert.AreEqual(fewRecommended[i].GetItemID(), moreRecommended[i].GetItemID());
     }
 }
 public void testRescorer()
 {
     IDataModel dataModel = getDataModel(
     new long[] {1, 2, 3},
     new Double?[][] {
             new double?[]{0.1, 0.2},
             new double?[]{0.2, 0.3, 0.3, 0.6},
             new double?[]{0.4, 0.5, 0.5, 0.9},
     });
     IUserSimilarity similarity = new PearsonCorrelationSimilarity(dataModel);
     IUserNeighborhood neighborhood = new NearestNUserNeighborhood(2, similarity, dataModel);
     IRecommender recommender = new GenericUserBasedRecommender(dataModel, neighborhood, similarity);
     IList<IRecommendedItem> originalRecommended = recommender.Recommend(1, 2);
     IList<IRecommendedItem> rescoredRecommended =
     recommender.Recommend(1, 2, new ReversingRescorer<long>());
     Assert.NotNull(originalRecommended);
     Assert.NotNull(rescoredRecommended);
     Assert.AreEqual(2, originalRecommended.Count);
     Assert.AreEqual(2, rescoredRecommended.Count);
     Assert.AreEqual(originalRecommended[0].GetItemID(), rescoredRecommended[1].GetItemID());
     Assert.AreEqual(originalRecommended[1].GetItemID(), rescoredRecommended[0].GetItemID());
 }
 public void TestIsolatedUser()
 {
     List<User> users = new List<User>(3);
     users.Add(GetUser("test1", 0.1, 0.2));
     users.Add(GetUser("test2", 0.2, 0.3, 0.3, 0.6));
     users.Add(GetUser("test3", 0.4, 0.4, 0.5, 0.9));
     users.Add(GetUser("test4"));
     DataModel dataModel = new GenericDataModel(users);
     UserCorrelation correlation = new PearsonCorrelation(dataModel);
     UserNeighborhood neighborhood = new NearestNUserNeighborhood(3, correlation, dataModel);
     UserBasedRecommender recommender = new GenericUserBasedRecommender(dataModel, neighborhood, correlation);
     ICollection<User> mostSimilar = recommender.MostSimilarUsers("test4", 3);
     Assert.IsNotNull(mostSimilar);
     Assert.AreEqual(0, mostSimilar.Count);
 }
Exemplo n.º 21
0
        static void Main(string[] args)
        {
            // load movies
            var movies = new List <MovieRecord>();

            using (TextReader reader = File.OpenText(@"data/movies.csv"))
            {
                CsvReader csv = new CsvReader(reader);
                csv.Configuration.Delimiter         = ",";
                csv.Configuration.MissingFieldFound = null;
                while (csv.Read())
                {
                    movies.Add(csv.GetRecord <MovieRecord>());
                }
            }

            // load users
            List <int> users = new List <int>();

            using (TextReader reader = File.OpenText(@"data/ratings.csv"))
            {
                CsvReader csv = new CsvReader(reader);
                csv.Configuration.Delimiter         = ",";
                csv.Configuration.MissingFieldFound = null;
                while (csv.Read())
                {
                    if (int.TryParse(csv.GetField(0), out int id))
                    {
                        if (!users.Contains(id))
                        {
                            users.Add(id);
                        }
                    }
                }
            }


            // load data model
            var model = new FileDataModel("data/ratings.csv", false, FileDataModel.DEFAULT_MIN_RELOAD_INTERVAL_MS, false);


            //init values
            string     similarityType   = "Cosine Similarity";
            string     recommenderType  = "Userbased";
            int        neighborhoodSize = 125;
            int        resultCount      = 10;
            List <int> movieIdPredList  = new List <int>();
            List <int> userIdPredList   = new List <int>();

            //default recommender
            AbstractSimilarity similarity = new UncenteredCosineSimilarity(model);
            var neighborhood = new NearestNUserNeighborhood(neighborhoodSize, similarity, model);
            AbstractRecommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);


            string input = "";

            while (input != "X")
            {
                Console.WriteLine();
                Console.WriteLine("Was möchten Sie tun?");
                Console.WriteLine("I - Umschalten auf Item-based Ansatz");
                Console.WriteLine("U - Umschalten auf User-based Ansatz");
                Console.WriteLine("DE - Umschalten auf Cosine Similarity");
                Console.WriteLine("PE - Umschalten auf Pearson Correlation");
                Console.WriteLine("N - Anzahl der Nachbarn festlegen");
                Console.WriteLine("C - Konfiguration neu erstellen und Datendatei neu lesen");
                Console.WriteLine("O - Aktuelle Konfiguration ausgeben");
                Console.WriteLine("E - Empfehlung durchführen");
                Console.WriteLine("P - Prediction für spezifischen Film durchführen");
                Console.WriteLine("UADD - Benutzer zur Liste für Vorschläge hinzufügen");
                Console.WriteLine("MADD - Film zur Filmliste hinzufügen");
                Console.WriteLine("UCLEAR - Benutzerliste leeren");
                Console.WriteLine("MCLEAR - Filmliste leeren");
                Console.WriteLine("X - Programm beenden");

                input = Console.ReadLine();

                if (input == "I")
                {
                    recommenderType = "Itembased";
                    recommender     = new GenericItemBasedRecommender(model, similarity);
                    Console.WriteLine("Recommendertyp auf \"itembased\" geändert.");
                }

                else if (input == "U")
                {
                    recommenderType = "Userbased";
                    recommender     = new GenericUserBasedRecommender(model, neighborhood, similarity);
                    Console.WriteLine("Recommendertyp auf \"userbased\" geändert.");
                }

                else if (input == "DE")
                {
                    similarityType = "Cosine Similarity";
                    similarity     = new UncenteredCosineSimilarity(model);
                    Console.WriteLine("Similaritytyp auf \"Cosine Similarity\" geändert.");
                }
                else if (input == "PE")
                {
                    similarityType = "Pearson Correlation Similarity";
                    similarity     = new PearsonCorrelationSimilarity(model);
                    Console.WriteLine("Similaritytyp auf \"Pearson Correlation Similarity\" geändert.");
                }
                else if (input == "N")
                {
                    Console.WriteLine("Wie groß soll die Nachbarschaft sein?");
                    string neighborhoodSizeInput = Console.ReadLine();
                    if (int.TryParse(neighborhoodSizeInput, out int neighborhoodSizeParsed))
                    {
                        neighborhoodSize = neighborhoodSizeParsed;
                        neighborhood     = new NearestNUserNeighborhood(neighborhoodSize, similarity, model);
                        Console.WriteLine("Nachbarschaftsgröße auf " + neighborhoodSize + " geändert.");
                    }
                    else
                    {
                        Console.WriteLine("Error: Es wurde keine Zahl eingegeben.");
                        Console.WriteLine("Die Nachbarschaftsgröße bleibt bei " + neighborhoodSize + ".");
                    }
                }
                else if (input == "C")
                {
                    Console.WriteLine("Wie groß soll die Nachbarschaft sein?");
                    string neighborhoodSizeInput = Console.ReadLine();
                    if (int.TryParse(neighborhoodSizeInput, out int neighborhoodSizeParsed))
                    {
                        neighborhoodSize = neighborhoodSizeParsed;
                        neighborhood     = new NearestNUserNeighborhood(neighborhoodSize, similarity, model);
                        Console.WriteLine("Nachbarschaftsgröße auf " + neighborhoodSize + " geändert.");
                    }
                    else
                    {
                        Console.WriteLine("Error: Es wurde keine Zahl eingegeben.");
                        Console.WriteLine("Die Nachbarschaftsgröße bleibt bei " + neighborhoodSize + ".");
                    }

                    Console.WriteLine("Wieviele Filme sollen vorgeschlagen werden?");

                    string resultCountInput = Console.ReadLine();
                    if (int.TryParse(resultCountInput, out int resultCountParsed))
                    {
                        resultCount = resultCountParsed;
                    }
                    else
                    {
                        Console.WriteLine("Error: Es wurde keine Zahl eingegeben.");
                        Console.WriteLine("Die Anzahl der vorgeschlagenen Filme bleibt bei " + resultCount + ".");
                    }

                    foreach (int userId in userIdPredList)
                    {
                        Console.WriteLine("Vorgeschlagene Filme für User mit UserId " + userId);
                        IList <IRecommendedItem> recommendedItems = recommender.Recommend(userId, resultCount);

                        foreach (var recItem in recommendedItems)
                        {
                            Console.WriteLine("Item: " + recItem.GetItemID() + " (" + movies.First(x => x.movieId == recItem.GetItemID()).title + ") ===> " + recItem.GetValue());
                        }
                        Console.WriteLine();
                    }

                    // load movies
                    movies = new List <MovieRecord>();
                    using (TextReader reader = File.OpenText(@"data/movies.csv"))
                    {
                        CsvReader csv = new CsvReader(reader);
                        csv.Configuration.Delimiter         = ",";
                        csv.Configuration.MissingFieldFound = null;
                        while (csv.Read())
                        {
                            movies.Add(csv.GetRecord <MovieRecord>());
                        }
                    }

                    // load users
                    users = new List <int>();
                    using (TextReader reader = File.OpenText(@"data/ratings.csv"))
                    {
                        CsvReader csv = new CsvReader(reader);
                        csv.Configuration.Delimiter         = ",";
                        csv.Configuration.MissingFieldFound = null;
                        while (csv.Read())
                        {
                            if (int.TryParse(csv.GetField(0), out int id))
                            {
                                if (!users.Contains(id))
                                {
                                    users.Add(id);
                                }
                            }
                        }
                    }

                    model = new FileDataModel("data/ratings.csv", false, FileDataModel.DEFAULT_MIN_RELOAD_INTERVAL_MS, false);

                    if (similarityType == "Cosine Similarity")
                    {
                        similarity = new UncenteredCosineSimilarity(model);
                    }
                    else
                    {
                        similarity = new PearsonCorrelationSimilarity(model);
                    }

                    neighborhood = new NearestNUserNeighborhood(neighborhoodSize, similarity, model);

                    if (recommenderType == "Itembased")
                    {
                        recommender = new GenericItemBasedRecommender(model, similarity);
                    }
                    else
                    {
                        recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);
                    }
                }
                else if (input == "O")
                {
                    int countPredUser   = userIdPredList.Count;
                    int countPredMovies = movieIdPredList.Count;

                    Console.WriteLine("-----------------------------------------------");
                    Console.WriteLine("Recommender Typ: " + recommenderType);
                    Console.WriteLine("Similarity Typ: " + similarityType);
                    Console.WriteLine("Nachbarschaftsgröße: " + neighborhoodSize);
                    Console.WriteLine("Anzahl Ergebnisse: " + resultCount);
                    Console.WriteLine("Anzahl Benutzer: " + countPredUser);
                    Console.WriteLine("Anzahl vorgeschlagene Filme: " + countPredMovies);
                    Console.WriteLine("-----------------------------------------------");
                }
                else if (input == "E")
                {
                    Console.WriteLine("Wieviele Filme sollen vorgeschlagen werden?");

                    string resultCountInput = Console.ReadLine();
                    if (int.TryParse(resultCountInput, out int resultCountParsed))
                    {
                        resultCount = resultCountParsed;
                    }
                    else
                    {
                        Console.WriteLine("Error: Es wurde keine Zahl eingegeben.");
                        Console.WriteLine("Die Anzahl der vorgeschlagenen Filme bleibt bei " + resultCount + ".");
                    }

                    foreach (int userId in userIdPredList)
                    {
                        Console.WriteLine("Vorgeschlagene Filme für User mit UserId " + userId);
                        IList <IRecommendedItem> recommendedItems = recommender.Recommend(userId, resultCount);

                        foreach (var recItem in recommendedItems)
                        {
                            Console.WriteLine("Item: " + recItem.GetItemID() + " (" + movies.First(x => x.movieId == recItem.GetItemID()).title + ") ===> " + recItem.GetValue());
                        }
                        Console.WriteLine();
                    }
                }
                else if (input == "P")
                {
                    foreach (int userId in userIdPredList)
                    {
                        Console.WriteLine("Prediction für User mit Id" + userId + ":");
                        foreach (int movieId in movieIdPredList)
                        {
                            float predictionValue = recommender.EstimatePreference(userId, movieId);
                            Console.WriteLine(movieId + " -> " + movies.First(x => x.movieId == movieId).title + " ===> " + predictionValue);
                        }
                        Console.WriteLine();
                    }
                }
                else if (input == "UADD")
                {
                    Console.WriteLine("Welcher Benutzer (Id) soll zur Liste für Vorschläge hinzugefügt werden?");

                    string userIdInput = Console.ReadLine();
                    if (int.TryParse(userIdInput, out int userIdParsed))
                    {
                        if (users.Contains(userIdParsed))
                        {
                            userIdPredList.Add(userIdParsed);
                        }
                        else
                        {
                            Console.WriteLine("Error: UserId existiert nicht.");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Error: Es wurde keine Zahl eingegeben.");
                    }
                }
                else if (input == "MADD")
                {
                    Console.WriteLine("Welcher Film (Id) soll zur Liste für Vorschläge hinzugefügt werden?");
                    string movieIdInput = Console.ReadLine();
                    if (int.TryParse(movieIdInput, out int movieIdParsed))
                    {
                        bool movieIdFound = false;
                        foreach (MovieRecord movie in movies)
                        {
                            if (movie.movieId == movieIdParsed)
                            {
                                movieIdPredList.Add(movieIdParsed);
                                movieIdFound = true;
                                break;
                            }
                        }
                        if (!movieIdFound)
                        {
                            Console.WriteLine("Error: movieId existiert nicht. Es wurde kein Film zur Liste hinzugefügt");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Error: Es wurde keine Zahl eingegeben.");
                    }
                }
                else if (input == "UCLEAR")
                {
                    userIdPredList.Clear();
                }
                else if (input == "MCLEAR")
                {
                    movieIdPredList.Clear();
                }
            }

            Console.WriteLine("Programm beendet");

            Console.ReadLine();
        }
        public void testFile()
        {
            IUserSimilarity userSimilarity = new PearsonCorrelationSimilarity(model);
            IUserNeighborhood neighborhood = new NearestNUserNeighborhood(3, userSimilarity, model);
            IRecommender recommender = new GenericUserBasedRecommender(model, neighborhood, userSimilarity);
            Assert.AreEqual(1, recommender.Recommend(123, 3).Count);
            Assert.AreEqual(0, recommender.Recommend(234, 3).Count);
            Assert.AreEqual(1, recommender.Recommend(345, 3).Count);

            // Make sure this doesn't throw an exception
            model.Refresh(null);
        }
 public void testIsolatedUser()
 {
     IDataModel dataModel = getDataModel(
     new long[] {1, 2, 3, 4},
     new Double?[][] {
             new double?[]{0.1, 0.2},
             new double?[]{0.2, 0.3, 0.3, 0.6},
             new double?[]{0.4, 0.4, 0.5, 0.9},
             new double?[]{null, null, null, null, 1.0},
     });
     IUserSimilarity similarity = new PearsonCorrelationSimilarity(dataModel);
     IUserNeighborhood neighborhood = new NearestNUserNeighborhood(3, similarity, dataModel);
     IUserBasedRecommender recommender = new GenericUserBasedRecommender(dataModel, neighborhood, similarity);
     long[] mostSimilar = recommender.MostSimilarUserIDs(4, 3);
     Assert.NotNull(mostSimilar);
     Assert.AreEqual(0, mostSimilar.Length);
 }
        public ActionResult GetRecommendedbooks()
        {
            var csv            = new CsvReader(new StreamReader(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/books.csv")));
            var records        = csv.GetRecords <BookRecord>().ToList();
            int favouriteGenre = db.Users.Where(u => u.UserName == User.Identity.Name).Select(u => u.genreID).SingleOrDefault();
            var userRentals    = db.Rentals.Where(m => m.rentalUser == User.Identity.Name).Join(db.Books,
                                                                                                r => r.rentalBook,
                                                                                                m => m.bookName,
                                                                                                (r, m) => new
            {
                genreId   = m.genreID,
                bookName  = m.bookName,
                rentalUer = r.rentalUser
            });

            if (userRentals.Count() == 0)
            {
                return(Json(new Dictionary <string, object>()
                {
                    { "book_id", 0 },
                    { "rating", 0 },
                }));
            }
            else if (userRentals.Where(m => m.genreId == favouriteGenre).Count() != 0)
            {
                userRentals = userRentals.Where(m => m.genreId == favouriteGenre);
            }

            long[] bookIdsTemp = new long[userRentals.Count()];
            int    bookCounter = 0;

            foreach (var item in userRentals)
            {
                foreach (var record in records)
                {
                    if (item.bookName == record.title)
                    {
                        if (!bookIdsTemp.Contains(record.bookId))
                        {
                            bookIdsTemp[bookCounter] = record.bookId;
                            bookCounter++;
                        }
                    }
                }
            }
            long[] bookIds = new long[bookCounter];
            for (int i = 0; i < bookCounter; i++)
            {
                bookIds[i] = bookIdsTemp[i];
            }

            var dataModel = GetDataModel();

            // recommendation is performed for the user that is missed in the preferences data
            var plusAnonymModel = new PlusAnonymousUserDataModel(dataModel);
            var prefArr         = new GenericUserPreferenceArray(userRentals.Count());

            prefArr.SetUserID(0, PlusAnonymousUserDataModel.TEMP_USER_ID);
            for (int i = 0; i < bookIds.Length; i++)
            {
                prefArr.SetItemID(i, bookIds[i]);

                // in this example we have no ratings of books preferred by the user
                prefArr.SetValue(i, 5); // lets assume max rating
            }
            plusAnonymModel.SetTempPrefs(prefArr);

            var similarity       = new LogLikelihoodSimilarity(plusAnonymModel);
            var neighborhood     = new NearestNUserNeighborhood(15, similarity, plusAnonymModel);
            var recommender      = new GenericUserBasedRecommender(plusAnonymModel, neighborhood, similarity);
            var recommendedItems = recommender.Recommend(PlusAnonymousUserDataModel.TEMP_USER_ID, 1, null);

            if (recommendedItems.Count() == 0)
            {
                return(Json(new Dictionary <string, object>()
                {
                    { "book_id", 0 },
                    { "rating", 0 },
                }));
            }

            return(Json(recommendedItems.Select(ri => new Dictionary <string, object>()
            {
                { "book_id", ri.GetItemID() },
                { "rating", ri.GetValue() },
            }).ToArray()[0]));
        }
Exemplo n.º 25
0
        /// <summary>
        /// 推荐
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="showCount">显示数量</param>
        /// <returns></returns>
        public List <Books> RecommendBooks(int pageIndex, int pageSize, int showCount)
        {
            #region 推荐
            List <Books> books = null;
            if (Session["user"] != null)
            {
                Users user = Session["user"] as Users;

                #region 构建用户行为数组
                var           loglist = logbll.LoadEntities(c => c.userID == user.Id).ToList();
                StringBuilder sb      = new StringBuilder();
                if (loglist.Count > 0)
                {
                    sb.Append("[");
                    int j = 0;
                    foreach (var item in loglist)
                    {
                        j++;
                        sb.Append(item.itemID.ToString());
                        if (j != loglist.Count)
                        {
                            sb.Append(",");
                        }
                    }
                    sb.Append("]");
                }
                #endregion

                if (string.IsNullOrEmpty(sb.ToString()))
                {
                    //冷启动
                    books = booksbll.LoadEntities(c => true).OrderByDescending(c => c.rating).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                }
                else
                {
                    var filmIds = (new JavaScriptSerializer()).Deserialize <long[]>(sb.ToString());

                    var    logmodel = settingbll.LoadEntities(c => c.id == 16).FirstOrDefault();
                    string path     = "";
                    if (logmodel != null && logmodel.value == "true")
                    {
                        path = "data/ratings1.dat";
                    }
                    else
                    {
                        path = "data/ratings.dat";
                    }

                    var pathToDataFile =
                        Path.Combine(System.Web.HttpRuntime.AppDomainAppPath, path);

                    if (dataModel == null)
                    {
                        dataModel = new FileDataModel(pathToDataFile, false, FileDataModel.DEFAULT_MIN_RELOAD_INTERVAL_MS, false);
                    }

                    var plusAnonymModel = new PlusAnonymousUserDataModel(dataModel);
                    var prefArr         = new GenericUserPreferenceArray(filmIds.Length);
                    prefArr.SetUserID(0, PlusAnonymousUserDataModel.TEMP_USER_ID);
                    for (int i = 0; i < filmIds.Length; i++)
                    {
                        prefArr.SetItemID(i, filmIds[i]);
                        prefArr.SetValue(i, 5); // lets assume max rating
                    }
                    plusAnonymModel.SetTempPrefs(prefArr);

                    var          similarity       = new LogLikelihoodSimilarity(plusAnonymModel);
                    var          neighborhood     = new NearestNUserNeighborhood(15, similarity, plusAnonymModel);
                    var          recommender      = new GenericUserBasedRecommender(plusAnonymModel, neighborhood, similarity);
                    var          recommendedItems = recommender.Recommend(PlusAnonymousUserDataModel.TEMP_USER_ID, showCount, null);
                    List <Books> newbooks         = new List <Books>();
                    foreach (var item in recommendedItems)
                    {
                        int bid = Convert.ToInt32(item.GetItemID());
                        newbooks.Add(booksbll.LoadEntities(c => c.Id == bid).FirstOrDefault());
                    }

                    books = newbooks.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                }
            }
            else //不推荐
            {
                books = booksbll.LoadEntities(c => true).OrderByDescending(c => c.rating).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            }
            #endregion

            return(books.Count() <= 0 ? booksbll.LoadEntities(c => true).OrderByDescending(c => c.rating).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList() : books);
        }