-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecommender.cpp
200 lines (190 loc) · 8.01 KB
/
Recommender.cpp
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "Recommender.h"
#include "UserDatabase.h"
#include "MovieDatabase.h"
#include "User.h"
#include "Movie.h"
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
//given all the users && all the posible movies
Recommender::Recommender(const UserDatabase& user_database,
const MovieDatabase& movie_database)
{
m_userDatabase = &user_database;
m_movieDatabase = &movie_database;
}
vector<MovieAndRank> Recommender::recommend_movies(const string& user_email, int movie_count)
{
//gets a *User
User* user = m_userDatabase->get_user_from_email(user_email);
//if user doesn't exist
vector <MovieAndRank> emptyMovies;
if (user == nullptr || movie_count < 1)
return emptyMovies;
//gets User's watch history (vector of movie ID's)
vector<string> watchHistory = user->get_watch_history();
//looks through all unwatched movies and compares them to each watched movie
vector <string> :: iterator it = watchHistory.begin();
//vector of unwatched movies with their scores
vector <MovieWithScore> movieCompats;
//for all movies in User watch history
while (it != watchHistory.end())
{
string curUserMovieID = *it;
Movie* curUserMovie = m_movieDatabase->get_movie_from_id(curUserMovieID);
//compare each user movie and slowly ups scores of movies with similar attributes
vector <string> directors = curUserMovie->get_directors();
//for each director
for (int i =0; i<directors.size(); i++)
{
vector <Movie*> moviesWithDirector = m_movieDatabase->get_movies_with_director(directors[i]);
//for each Movie within database with that director, update their score
for (int j =0; j< moviesWithDirector.size(); j++)
{
//current movie from database (with matching director)
MovieWithScore curMovieInData (moviesWithDirector[j]->get_id(),moviesWithDirector[j]->get_rating(), moviesWithDirector[j]->get_title() );
//check if movieID already entered before
bool movieExists = false;
MovieWithScore* movieInVector = nullptr;
//already watched movies
if (curMovieInData.getID() == curUserMovieID)
continue;
//loops through vector to see if movie is already added
for (int k =0; k<movieCompats.size();k++)
{
//if movie was already added
if (movieCompats[k].getID() == curMovieInData.getID())
{
//pointer to movie which is already in vector
movieInVector = &movieCompats[k];
movieExists = true;
}
}
//if movie is not in vector yet
if (movieExists == false)
{
curMovieInData.addToScore(20);
movieCompats.push_back(curMovieInData);
}
//if movie is already in vector
else if (movieExists == true)
{
movieInVector->addToScore(20);
}
}
}
//for each actor
vector <string> actors = curUserMovie->get_actors();
for (int i =0; i<actors.size(); i++)
{
vector <Movie*> moviesWithActor = m_movieDatabase->get_movies_with_actor(actors[i]);
//for each Movie within database with that director, update their score
for (int j =0; j< moviesWithActor.size(); j++)
{
//current movie from database (with matching director)
MovieWithScore curMovieInData (moviesWithActor[j]->get_id(), moviesWithActor[j]->get_rating(), moviesWithActor[j]->get_title());
//check if movieID already entered before
bool movieExists = false;
MovieWithScore* movieInVector = nullptr;
//already watched movies
if (curMovieInData.getID() == curUserMovieID)
continue;
//loops through vector to see if movie is already added
for (int k =0; k<movieCompats.size();k++)
{
//if movie was already added
if (movieCompats[k].getID() == curMovieInData.getID())
{
//pointer to movie which is already in vector
movieInVector = &movieCompats[k];
movieExists = true;
}
}
//if movie is not in vector yet
if (movieExists == false)
{
curMovieInData.addToScore(30);
movieCompats.push_back(curMovieInData);
}
//if movie is already in vector
else if (movieExists == true)
{
movieInVector->addToScore(30);
}
}
}
//for each genre
vector <string> genres = curUserMovie->get_genres();
for (int i =0; i<genres.size(); i++)
{
vector <Movie*> moviesWithGenre = m_movieDatabase->get_movies_with_genre(genres[i]);
//for each Movie within database with that director, update their score
for (int j =0; j< moviesWithGenre.size(); j++)
{
//current movie from database (with matching director)
MovieWithScore curMovieInData (moviesWithGenre[j]->get_id(),moviesWithGenre[j]->get_rating(), moviesWithGenre[j]->get_title() );
//check if movieID already entered before
bool movieExists = false;
MovieWithScore* movieInVector = nullptr;
//already watched movies
if (curMovieInData.getID() == curUserMovieID)
continue;
//loops through vector to see if movie is already added
for (int k =0; k<movieCompats.size();k++)
{
//if movie was already added
if (movieCompats[k].getID() == curMovieInData.getID())
{
//pointer to movie which is already in vector
movieInVector = &movieCompats[k];
movieExists = true;
}
}
//if movie is not in vector yet
if (movieExists == false)
{
curMovieInData.addToScore(1);
movieCompats.push_back(curMovieInData);
}
//if movie is already in vector
else if (movieExists == true)
{
movieInVector->addToScore(1);
}
}
}
it++;
}
//sort vector (using STL sort)
sort(movieCompats.begin(), movieCompats.end());
//remove movies already watched
vector <MovieWithScore> :: iterator iters = movieCompats.begin();
while (iters != movieCompats.end())
{
for (int i =0; i<watchHistory.size();i++)
{
if (iters->getID()==watchHistory[i])
{
movieCompats.erase(iters);
iters = movieCompats.begin();
}
}
iters++;
}
//put movies from vector into a MovieAndRank vector (which we return)
vector <MovieAndRank> rankedMovies;
vector <MovieWithScore> :: iterator iter = movieCompats.end();
int numberOfMoviesToAdd = 0;
//while there are compatible movies and keeping track of how many movies to add
while (iter != movieCompats.begin() && numberOfMoviesToAdd != movie_count)
{
iter--;
//created a MovieAndRank object from MovieWithScore vector
MovieAndRank addedMovie (iter->getID(), iter->getScore());
//adds object to new vector
rankedMovies.push_back(addedMovie);
numberOfMoviesToAdd++;
}
return rankedMovies;
}