Advertisement
Cinestra

Recommender.cpp (Attempt 1)

Jun 20th, 2023
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. #include "Recommender.h"
  2. #include "UserDatabase.h"
  3. #include "MovieDatabase.h"
  4.  
  5. #include <string>
  6. #include <vector>
  7. using namespace std;
  8.  
  9. Recommender::Recommender(const UserDatabase& user_database,
  10. const MovieDatabase& movie_database)
  11. {
  12. m_user_database = &user_database;
  13. m_movie_database = &movie_database;
  14. // Replace this line with correct code.
  15. }
  16.  
  17.  
  18. unordered_map<string, int> Recommender::create_unordered_map_linking_movies_with_compatibility(const string& user_email, int moive_count) const
  19. {
  20. // Unordered map connecting Movie IDs and compatbility scores
  21. unordered_map<string, int> m_possible_recommend_movies;
  22.  
  23. // Grab user from their email
  24. User* current_user = m_user_database->get_user_from_email(user_email);
  25. vector<string> current_user_watch_history = current_user->get_watch_history();
  26. for (int i = 0; i < current_user_watch_history.size(); i++)
  27. {
  28. Movie* current_movie = m_movie_database->get_movie_from_id(current_user_watch_history[i]);
  29.  
  30. // Getting movies with the same director should take m + log d
  31. vector<Movie*> movies_with_same_director;
  32. for (int j = 0; j < current_movie->get_directors().size(); j++)
  33. {
  34. string jth_director = (*current_movie).get_directors()[j];
  35. movies_with_same_director = m_movie_database->get_movies_with_director(jth_director);
  36.  
  37. for (int k = 0; k < movies_with_same_director.size(); k++)
  38. {
  39. string kth_movie_id_with_same_director = movies_with_same_director[k]->get_id();
  40. m_possible_recommend_movies[kth_movie_id_with_same_director] += SAME_DIRECTOR_POINTS;
  41. }
  42. }
  43.  
  44. vector<Movie*> movies_with_same_actor;
  45. for (int j = 0; j < current_movie->get_actors().size(); j++)
  46. {
  47. string jth_actor = (*current_movie).get_actors()[j];
  48. movies_with_same_actor = m_movie_database->get_movies_with_actor(jth_actor);
  49.  
  50. for (int k = 0; k < movies_with_same_actor.size(); k++)
  51. {
  52. string kth_movie_id_with_same_actor = movies_with_same_actor[k]->get_id();
  53. m_possible_recommend_movies[kth_movie_id_with_same_actor] += SAME_ACTOR_POINTS;
  54. }
  55. }
  56.  
  57. vector<Movie*> movies_with_same_genre;
  58. for (int j = 0; j < current_movie->get_genres().size(); j++)
  59. {
  60. string jth_genre = (*current_movie).get_genres()[j];
  61. movies_with_same_genre = m_movie_database->get_movies_with_actor(jth_genre);
  62.  
  63. for (int k = 0; k < movies_with_same_genre.size(); k++)
  64. {
  65. string kth_movie_id_with_same_genre = movies_with_same_genre[k]->get_id();
  66. m_possible_recommend_movies[kth_movie_id_with_same_genre] += SAME_GENRE_POINTS;
  67. }
  68. }
  69.  
  70. }
  71.  
  72. return m_possible_recommend_movies;
  73. }
  74.  
  75. bool Recommender::compare_MovieAndRank(MovieAndRank& a, MovieAndRank& b) const
  76. {
  77. // First check based on compatibility
  78. if (a.compatibility_score != b.compatibility_score)
  79. return a.compatibility_score > b.compatibility_score;
  80.  
  81. // Then check based on rating
  82. 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())
  83. 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();
  84.  
  85. // Then check based on alpha sorting
  86. else
  87. return a.movie_id < b.movie_id;
  88. }
  89.  
  90. std::vector<MovieAndRank> Recommender::create_descending_order_vector_given_unordered_map(const User* &current_user, int movie_count, unordered_map<string, int> possible_recommend_movies) const
  91. {
  92. vector<MovieAndRank> compiled_MovieAndRank;
  93. for (auto itr = possible_recommend_movies.begin(); itr != possible_recommend_movies.end(); itr++)
  94. {
  95. string itr_id = itr->first;
  96. int itr_score = itr->second;
  97. MovieAndRank itr_MovieAndRank(itr_id, itr_score);
  98.  
  99. vector<string> temp_watch_history = current_user->get_watch_history();
  100. bool already_seen_movie = (find(temp_watch_history.begin(), temp_watch_history.end(), itr_id) == temp_watch_history.end());
  101. if (already_seen_movie)
  102. continue;
  103.  
  104. // Base Case, we will initially start with an empty vector and have to populate it
  105. else if (compiled_MovieAndRank.empty())
  106. {
  107. compiled_MovieAndRank.push_back(itr_MovieAndRank);
  108. continue;
  109. }
  110.  
  111. for (int i = 0; i < compiled_MovieAndRank.size(); i++)
  112. {
  113. // Compare the iterated movie with the ith movie already in the compiled_MovieAndRank
  114. if (compare_MovieAndRank(itr_MovieAndRank, compiled_MovieAndRank[i]))
  115. {
  116. compiled_MovieAndRank.insert(compiled_MovieAndRank.begin() + i, itr_MovieAndRank);
  117. break;
  118. }
  119.  
  120. else if (i == compiled_MovieAndRank.size() - 1)
  121. {
  122. compiled_MovieAndRank.push_back(itr_MovieAndRank);
  123. break;
  124. }
  125. }
  126. }
  127.  
  128. }
  129.  
  130.  
  131.  
  132.  
  133. vector<MovieAndRank> Recommender::recommend_movies(const string& user_email, int movie_count) const
  134. {
  135. if (movie_count <= 0)
  136. {
  137. vector<MovieAndRank> empty_vector;
  138. return empty_vector;
  139. }
  140.  
  141. // Unordered map connecting Movie IDs and compatbility scores
  142. unordered_map<string, int> m_possible_recommend_movies = create_unordered_map_linking_movies_with_compatibility(user_email, movie_count);
  143.  
  144.  
  145.  
  146.  
  147. return vector<MovieAndRank>(); // Replace this line with correct code.
  148. }
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement