-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovieRate.cpp
More file actions
88 lines (69 loc) · 1.79 KB
/
MovieRate.cpp
File metadata and controls
88 lines (69 loc) · 1.79 KB
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
#include <iostream>
#include <map>
#include <limits>
#include "common.h"
using namespace std;
extern map<int, map<int, float>> ratings;
float movieRate(int userID, int movieID);
int main(int argc, char* argv[])
{
if(argc != 3)
{
cerr << "Format: MovieRate [UserID] [MovieID]" << endl;
return 1;
}
int userID = atoi(argv[1]);
int movieID = atoi(argv[2]);
if(!isUserID(userID))
{
cerr << "User id out of bounds." << endl;
return 1;
}
if (!isMovieID(movieID))
{
cerr << "Movie id out of bounds." << endl;
return 1;
}
if(parser() == -1)
{
cerr << "Parsing error." << endl;
return 1;
}
movieRate(userID, movieID);
return 0;
}
float movieRate(int userID, int movieID)
{
int simUser = -1;
float minDist = numeric_limits<float>::max();
for (const auto& pair : ratings)
{
int userID1 = pair.first;
map<int, float>& userRatings1 = ratings[userID1];
if (userID1 == userID)
continue;
if (userRatings1.find(movieID) != userRatings1.end())
{
float dist = ratingDistance(userID, userID1);
if(dist < minDist && dist >= 0)
{
simUser = userID1;
minDist = dist;
}
}
}
if(simUser == -1)
{
cout << "No similar user found." << endl;
return -1.0;
}
else
{
float predictedRating = ratings[simUser][movieID];
cout << "The predicted rating for UserID: " << userID
<< " and MovieID: " << movieID << " is " << predictedRating
<< " based on the most similar user: " << simUser <<
" with a rating distance: " << minDist << endl;
return predictedRating;
}
}