Пример #1
0
        public void GetAllFilmsByGenreMethodOk()
        {
            clsFilmGenreCollection AllFilmGenres = new clsFilmGenreCollection();
            Int32 numberofFilmsByGenre           = 1830;
            Int32 genreId = 1;

            AllFilmGenres.GetAllFilmsByGenre(genreId);
            Assert.AreEqual(AllFilmGenres.AllFilmsByGenre.Count, numberofFilmsByGenre);
        }
        void GenerateRecommendations(int genreId)
        {
            //create a new instance of the MLContext class
            MLContext mlContext = new MLContext();

            (IDataView trainingDataView, IDataView testDataView) = LoadData(mlContext);
            ITransformer model = BuildAndTrainModel(mlContext, trainingDataView);

            //create an instance of the class which represents the IDataView/ DataViewRow schema
            DataViewSchema modelSchema;

            //find the saved model
            var path = Server.MapPath(@"~/Model.zip");

            //load the saved model
            ITransformer trainedModel = mlContext.Model.Load(path,
                                                             out modelSchema);

            //create a prediction engine for film recommendations
            var predictionEngine = mlContext.Model.CreatePredictionEngine <clsFilmRating, MovieRatingPrediction>(trainedModel);

            var     tempUserId = Session["UserId"];
            Single  userId;
            Boolean signedIn = false;

            //check if user is signed in
            if (tempUserId == null)
            {
                userId = 1;
            }
            else
            {
                userId   = Convert.ToSingle(tempUserId);
                signedIn = true;
            }

            //get all films from the database
            clsFilmGenreCollection AllFilms = new clsFilmGenreCollection();

            AllFilms.GetAllFilmsByGenre(genreId);

            List <clsFilmPrediction> AllPredictions  = new List <clsFilmPrediction>();
            clsFilmPrediction        aFilmPrediction = new clsFilmPrediction();

            foreach (clsFilmGenre aFilm in AllFilms.AllFilmsByGenre)
            {
                var potentialRecommendation = new clsFilmRating {
                    UserId = userId, FilmId = aFilm.FilmId
                };
                var movieRatingPrediction = predictionEngine.Predict(potentialRecommendation);
                //if a rating is high enough, add it to film predictions
                if (Math.Round(movieRatingPrediction.Score, 1) > 4.4)
                {
                    aFilmPrediction        = new clsFilmPrediction();
                    aFilmPrediction.FilmId = aFilm.FilmId;
                    aFilmPrediction.Score  = movieRatingPrediction.Score;

                    AllPredictions.Add(aFilmPrediction);
                }
            }

            //sort them by score
            AllPredictions.Sort();

            //get the ten best recommendations
            var topTenPredictions = AllPredictions.Take(10);

            clsFilmRecommendationCollection FilmRecommendations  = new clsFilmRecommendationCollection();
            clsFilmRecommendation           aRecommendationToAdd = new clsFilmRecommendation();

            clsMostRecommendedFilmsCollection AllMostRecommendedFilms = new clsMostRecommendedFilmsCollection();
            clsMostRecommendedFilms           aMostRecommendedFilm    = new clsMostRecommendedFilms();

            foreach (clsFilmPrediction aTopTenPrediction in topTenPredictions)
            {
                if (signedIn)
                {
                    //save the recommendations for future use
                    aRecommendationToAdd        = new clsFilmRecommendation();
                    aRecommendationToAdd.FilmId = aTopTenPrediction.FilmId;
                    aRecommendationToAdd.UserId = Convert.ToInt32(userId);
                    FilmRecommendations.ThisFilmRecommendation = aRecommendationToAdd;
                    FilmRecommendations.Add();
                }

                aMostRecommendedFilm = new clsMostRecommendedFilms();
                AllMostRecommendedFilms.ThisMostRecommendedFilm.FilmId = aTopTenPrediction.FilmId;

                if (AllMostRecommendedFilms.ThisMostRecommendedFilm.Find(aTopTenPrediction.FilmId) == true)
                {
                    AllMostRecommendedFilms.IncreaseTimesRecommended();
                }
                else
                {
                    AllMostRecommendedFilms.Add();
                }
                //get imdb information for film
                pnlRecommendations.Controls.Add(anImdbApi.GetImdbInformation(aTopTenPrediction.FilmId));
            }
            pnlRecommendations.Visible = true;

            //save the model for later use
            mlContext.Model.Save(trainedModel, modelSchema, path);
        }