fork download
  1. #include <algorithm>
  2. #include <fstream>
  3. #include <functional>
  4. #include <iostream>
  5. #include <list>
  6. #include <vector>
  7. #include "MovieRecommender.h"
  8.  
  9. using std::list; using std::vector;
  10.  
  11. struct RatingInformation {
  12. int userId, movieId, rating;
  13.  
  14. RatingInformation(int u, int m, int r) : userId(u), movieId(m), rating(r) {};
  15.  
  16. // overloading operator to make calls to sort simpler
  17. bool operator<(const RatingInformation& rhs) const {
  18. return (this->movieId < rhs.movieId);
  19. }
  20. };
  21.  
  22.  
  23. int main() {
  24. return 0;
  25. }
  26.  
  27. list<RatingInformation> readFromFile(char* file) {
  28. // assumption: input is properly formatted with 3 integer values per line
  29. list<RatingInformation> ratings;
  30. std::ifstream in(file);
  31.  
  32. if (in) {
  33. int userId, movieId, rating;
  34. while (in >> userId >> movieId >> rating) {
  35. ratings.push_back(RatingInformation(userId, movieId, rating));
  36. }
  37. } else {
  38. std::cerr << "Cannot open file " << file << std::endl;
  39. }
  40.  
  41. in.close();
  42.  
  43. return ratings;
  44. }
  45.  
  46. template<class InputIterator>
  47. void print(InputIterator start, InputIterator end) {
  48. for (InputIterator i = start; i != end; i++) {
  49. std::cout << "UserId is " << i->userId
  50. << ", MovieId is " << i->movieId
  51. << ", and Rating is " << i->rating
  52. << std::endl;
  53. }
  54. }
  55.  
  56. void transform(vector<RatingInformation>& user, int shift) {
  57. for (int i = 0, n = user.size(); i < n; i++) {
  58. user[i].rating += shift;
  59. }
  60. }
  61.  
  62.  
  63. int dotProduct(vector<RatingInformation>& a, vector<RatingInformation>& b) {
  64. // Assumptions:
  65. // - size of both vectors is the same
  66. // - vectors are sorted according to movie id
  67. // - vectors contain same movie collections
  68. int dotProduct = 0;
  69.  
  70. for (int i = 0, n = a.size(); i < n; i++) {
  71. dotProduct += a[i].rating * b[i].rating;
  72. }
  73.  
  74. return dotProduct;
  75. }
  76.  
  77.  
  78. template<class InputIterator>
  79. bool containsMovie(InputIterator start, InputIterator finish, int movieId) {
  80. for (InputIterator i = start; i != finish; i++) {
  81. if (i->movieId == movieId) return true;
  82. }
  83. return false;
  84. }
  85.  
  86.  
  87. template<class InputIterator>
  88. vector<RatingInformation> getSubset(InputIterator user1Start, InputIterator user1Finish,
  89. InputIterator user2Start, InputIterator user2Finish) {
  90. vector<RatingInformation> subset;
  91.  
  92. for (InputIterator i = user1Start; i != user1Finish; i++) {
  93. if (containsMovie(user2Start, user2Finish, i->movieId)) {
  94. subset.push_back(*i);
  95. }
  96. }
  97.  
  98. return subset;
  99. }
  100.  
  101.  
  102. int guser = 0;
  103.  
  104. bool userSeparation(RatingInformation set1) {
  105. return (set1.userId != guser);
  106. }
  107.  
  108. int computeSimilarity(list<RatingInformation> ratings, int userId1, int userId2) {
  109. list<RatingInformation> user1, user2;
  110.  
  111. // yank ratings associated with user 1 and user 2 from input RatingInformation list
  112. guser = userId1;
  113. std::remove_copy_if(ratings.begin(), ratings.end(), user1.begin(), userSeparation);
  114. guser = userId2;
  115. std::remove_copy_if(ratings.begin(), ratings.end(), user2.begin(), userSeparation);
  116.  
  117. // determined overlap regions between two users based on movieIds
  118. vector<RatingInformation> ratings1 = getSubset(user1.begin(), user1.end(),
  119. user2.begin(), user2.end());
  120. vector<RatingInformation> ratings2 = getSubset(user2.begin(), user2.end(),
  121. user1.begin(), user1.end());
  122.  
  123. transform(ratings1, -5);
  124. transform(ratings2, -5);
  125.  
  126. // note, < operator is overloaded
  127. std::sort(ratings1.begin(), ratings1.end());
  128. std::sort(ratings2.begin(), ratings2.end());
  129.  
  130. return dotProduct(ratings1, ratings2);
  131. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:7:30: error: MovieRecommender.h: No such file or directory
stdout
Standard output is empty