-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.cpp
More file actions
88 lines (72 loc) · 1.71 KB
/
common.cpp
File metadata and controls
88 lines (72 loc) · 1.71 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 <fstream>
#include <map>
#include <cmath>
#include "common.h"
using namespace std;
const int USERS = 71567;
const int MOVIES = 65133;
map<int, map<int, float>> ratings; //the data structure to store the ratings
int parser()
{
ifstream f("ratings.dat");
if (!f.is_open())
{
cerr << "Error opening file: ratings.dat" << endl;
return -1;
}
string line;
int userID, movieID;
float rating;
int count = 0;
while (getline(f, line))
{
if (sscanf(line.c_str(), "%d::%d::%f::%*s", &userID, &movieID, &rating) == 3)
{
ratings[userID][movieID] = rating;
count++;
}
else
{
cerr << "Error parsing" << endl;
f.close();
return -1;
}
}
f.close();
return count;
}
//implementation of the rating distance function
float ratingDistance(int userID1, int userID2)
{
map<int, float>& user1Ratings = ratings[userID1];
map<int, float>& user2Ratings = ratings[userID2];
float totalDifference = 0.0;
int commonMovies = 0;
for (const auto& pair : user1Ratings)
{
int movieID = pair.first;
float rating1 = pair.second;
if (user2Ratings.find(movieID) != user2Ratings.end())
{
totalDifference += fabs(rating1 - user2Ratings[movieID]);
commonMovies++;
}
}
if (commonMovies > 0)
return totalDifference / commonMovies;
else
return -1.0;
}
int isUserID(int userID)
{
if(!(userID > 0 && userID <= USERS))
return 0;
return 1;
}
int isMovieID(int movieID)
{
if(!(movieID > 0 && movieID <= MOVIES))
return 0;
return 1;
}