-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
MoviesController.cs
123 lines (105 loc) · 4.21 KB
/
MoviesController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.ML;
using Microsoft.Extensions.Options;
using Microsoft.ML;
using movierecommender.Models;
using movierecommender.Services;
using MovieRecommender.DataStructures;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text;
namespace movierecommender.Controllers
{
public class MoviesController : Controller
{
private readonly IMovieService _movieService;
private readonly IProfileService _profileService;
private readonly AppSettings _appSettings;
private readonly ILogger<MoviesController> _logger;
private readonly PredictionEnginePool<MovieRating, MovieRatingPrediction> _model;
public MoviesController(PredictionEnginePool<MovieRating, MovieRatingPrediction> model,
ILogger<MoviesController> logger,
IOptions<AppSettings> appSettings,
IMovieService movieService,
IProfileService profileService)
{
_movieService = movieService;
_profileService = profileService;
_logger = logger;
_appSettings = appSettings.Value;
_model = model;
}
public ActionResult Choose()
{
return View(_movieService.GetSomeSuggestions());
}
public ActionResult Recommend(int id)
{
var activeprofile = _profileService.GetProfileByID(id);
// 1. Create the ML.NET environment and load the already trained model
MLContext mlContext = new MLContext();
List<(int movieId, float normalizedScore)> ratings = new List<(int movieId, float normalizedScore)>();
var MovieRatings = _profileService.GetProfileWatchedMovies(id);
List<Movie> WatchedMovies = new List<Movie>();
foreach ((int movieId, int movieRating) in MovieRatings)
{
WatchedMovies.Add(_movieService.Get(movieId));
}
MovieRatingPrediction prediction = null;
foreach (var movie in _movieService.GetTrendingMovies)
{
// Call the Rating Prediction for each movie prediction
prediction = _model.Predict(new MovieRating
{
userId = id.ToString(),
movieId = movie.MovieID.ToString()
});
// Normalize the prediction scores for the "ratings" b/w 0 - 100
float normalizedscore = Sigmoid(prediction.Score);
// Add the score for recommendation of each movie in the trending movie list
ratings.Add((movie.MovieID, normalizedscore));
}
//3. Provide rating predictions to the view to be displayed
ViewData["watchedmovies"] = WatchedMovies;
ViewData["ratings"] = ratings;
ViewData["trendingmovies"] = _movieService.GetTrendingMovies;
return View(activeprofile);
}
public float Sigmoid(float x)
{
return (float) (100/(1 + Math.Exp(-x)));
}
public ActionResult Watch()
{
return View();
}
public ActionResult Profiles()
{
var profiles = _profileService.GetProfiles;
return View(profiles);
}
public ActionResult Watched(int id)
{
var activeprofile = _profileService.GetProfileByID(id);
var MovieRatings = _profileService.GetProfileWatchedMovies(id);
List<Movie> WatchedMovies = new List<Movie>();
foreach ((int movieId, float normalizedScore) in MovieRatings)
{
WatchedMovies.Add(_movieService.Get(movieId));
}
ViewData["watchedmovies"] = WatchedMovies;
ViewData["trendingmovies"] = _movieService.GetTrendingMovies;
return View(activeprofile);
}
public class JsonContent : StringContent
{
public JsonContent(object obj) :
base(JsonConvert.SerializeObject(obj), Encoding.UTF8, "application/json")
{ }
}
}
}