Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Recommender.h"
- #include "UserDatabase.h"
- #include "MovieDatabase.h"
- #include <string>
- #include <vector>
- using namespace std;
- Recommender::Recommender(const UserDatabase& user_database,
- const MovieDatabase& movie_database)
- {
- m_user_database = &user_database;
- m_movie_database = &movie_database;
- // Replace this line with correct code.
- }
- unordered_map<string, int> Recommender::create_unordered_map_linking_movies_with_compatibility(const string& user_email, int moive_count) const
- {
- // Unordered map connecting Movie IDs and compatbility scores
- unordered_map<string, int> m_possible_recommend_movies;
- // Grab user from their email
- User* current_user = m_user_database->get_user_from_email(user_email);
- vector<string> current_user_watch_history = current_user->get_watch_history();
- for (int i = 0; i < current_user_watch_history.size(); i++)
- {
- Movie* current_movie = m_movie_database->get_movie_from_id(current_user_watch_history[i]);
- // Getting movies with the same director should take m + log d
- vector<Movie*> movies_with_same_director;
- for (int j = 0; j < current_movie->get_directors().size(); j++)
- {
- string jth_director = (*current_movie).get_directors()[j];
- movies_with_same_director = m_movie_database->get_movies_with_director(jth_director);
- for (int k = 0; k < movies_with_same_director.size(); k++)
- {
- string kth_movie_id_with_same_director = movies_with_same_director[k]->get_id();
- m_possible_recommend_movies[kth_movie_id_with_same_director] += SAME_DIRECTOR_POINTS;
- }
- }
- vector<Movie*> movies_with_same_actor;
- for (int j = 0; j < current_movie->get_actors().size(); j++)
- {
- string jth_actor = (*current_movie).get_actors()[j];
- movies_with_same_actor = m_movie_database->get_movies_with_actor(jth_actor);
- for (int k = 0; k < movies_with_same_actor.size(); k++)
- {
- string kth_movie_id_with_same_actor = movies_with_same_actor[k]->get_id();
- m_possible_recommend_movies[kth_movie_id_with_same_actor] += SAME_ACTOR_POINTS;
- }
- }
- vector<Movie*> movies_with_same_genre;
- for (int j = 0; j < current_movie->get_genres().size(); j++)
- {
- string jth_genre = (*current_movie).get_genres()[j];
- movies_with_same_genre = m_movie_database->get_movies_with_actor(jth_genre);
- for (int k = 0; k < movies_with_same_genre.size(); k++)
- {
- string kth_movie_id_with_same_genre = movies_with_same_genre[k]->get_id();
- m_possible_recommend_movies[kth_movie_id_with_same_genre] += SAME_GENRE_POINTS;
- }
- }
- }
- return m_possible_recommend_movies;
- }
- bool Recommender::compare_MovieAndRank(MovieAndRank& a, MovieAndRank& b) const
- {
- // First check based on compatibility
- if (a.compatibility_score != b.compatibility_score)
- return a.compatibility_score > b.compatibility_score;
- // Then check based on rating
- else if (m_movie_database->get_movie_from_id(a.movie_id)->get_rating() != m_movie_database->get_movie_from_id(b.movie_id)->get_rating())
- return m_movie_database->get_movie_from_id(a.movie_id)->get_rating() > m_movie_database->get_movie_from_id(b.movie_id)->get_rating();
- // Then check based on alpha sorting
- else
- return a.movie_id < b.movie_id;
- }
- std::vector<MovieAndRank> Recommender::create_descending_order_vector_given_unordered_map(const User* ¤t_user, int movie_count, unordered_map<string, int> possible_recommend_movies) const
- {
- vector<MovieAndRank> compiled_MovieAndRank;
- for (auto itr = possible_recommend_movies.begin(); itr != possible_recommend_movies.end(); itr++)
- {
- string itr_id = itr->first;
- int itr_score = itr->second;
- MovieAndRank itr_MovieAndRank(itr_id, itr_score);
- vector<string> temp_watch_history = current_user->get_watch_history();
- bool already_seen_movie = (find(temp_watch_history.begin(), temp_watch_history.end(), itr_id) == temp_watch_history.end());
- if (already_seen_movie)
- continue;
- // Base Case, we will initially start with an empty vector and have to populate it
- else if (compiled_MovieAndRank.empty())
- {
- compiled_MovieAndRank.push_back(itr_MovieAndRank);
- continue;
- }
- for (int i = 0; i < compiled_MovieAndRank.size(); i++)
- {
- // Compare the iterated movie with the ith movie already in the compiled_MovieAndRank
- if (compare_MovieAndRank(itr_MovieAndRank, compiled_MovieAndRank[i]))
- {
- compiled_MovieAndRank.insert(compiled_MovieAndRank.begin() + i, itr_MovieAndRank);
- break;
- }
- else if (i == compiled_MovieAndRank.size() - 1)
- {
- compiled_MovieAndRank.push_back(itr_MovieAndRank);
- break;
- }
- }
- }
- }
- vector<MovieAndRank> Recommender::recommend_movies(const string& user_email, int movie_count) const
- {
- if (movie_count <= 0)
- {
- vector<MovieAndRank> empty_vector;
- return empty_vector;
- }
- // Unordered map connecting Movie IDs and compatbility scores
- unordered_map<string, int> m_possible_recommend_movies = create_unordered_map_linking_movies_with_compatibility(user_email, movie_count);
- return vector<MovieAndRank>(); // Replace this line with correct code.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement