-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFourthRatings.java
179 lines (139 loc) · 5.92 KB
/
FourthRatings.java
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import java.util.*;
public class FourthRatings {
private double dotProduct(Rater currRater, Rater otherRater) {
ArrayList<String> currRatedItems = currRater.getItemsRated();
ArrayList<String> otherRatedItems = otherRater.getItemsRated();
double result = 0.0;
double scale = 5.0;
for (String currRatedItem : currRatedItems) {
if (otherRatedItems.contains(currRatedItem)) {
double scaledCurrRate = currRater.getRating(currRatedItem) - scale;
double scaledOtherRate = otherRater.getRating(currRatedItem) - scale;
result += scaledCurrRate * scaledOtherRate;
}
}
return result;
}
private ArrayList<Rating> getSimilarities(String id) {
ArrayList<Rating> list = new ArrayList<Rating>();
Rater me = RaterDatabase.getRater(id);
for (Rater r : RaterDatabase.getRaters()) {
if (!r.getID().equals(id)) {
double dot = dotProduct(me, r);
if (dot > 0.0) {
list.add(new Rating(r.getID(), dot));
}
}
}
Collections.sort(list, Collections.reverseOrder());
return list;
}
private double getAverageByIDMinRates(String id, Integer minimalRaters) {
ArrayList<Rater> myRaters = RaterDatabase.getRaters();
int numRates = 0;
double totalRating = 0.0;
for (Rater raterItem : myRaters) {
double rating = raterItem.getRating(id);
if (rating > 0) {
totalRating += rating;
numRates++;
}
}
if (numRates < minimalRaters) {
return 0.0;
}
return totalRating / numRates;
}
public ArrayList<Rating> getSimilarRatings(
String id, Integer numSimilarRaters, Integer minimalRaters
) {
ArrayList<Rating> movieWeightAvgs = new ArrayList<Rating>();
ArrayList<Rating> similiraties = getSimilarities(id);
ArrayList<Rating> topSimilarites = new ArrayList<Rating>();
for (int i = 0; i < numSimilarRaters; i++) {
String raterID = similiraties.get(i).getItem();
double raterSim = similiraties.get(i).getValue();
topSimilarites.add(new Rating(raterID, raterSim));
}
ArrayList<String> movies = MovieDatabase.filterBy(new TrueFilter());
for (String movieID : movies) {
double totScore = 0.0;
int numRaters = 0;
for (Rating simRater : topSimilarites) {
double simRating = simRater.getValue();
Rater raterRatings = RaterDatabase.getRater(simRater.getItem());
if (raterRatings.hasRating(movieID)) {
double movieRating = raterRatings.getRating(movieID);
totScore += simRating * movieRating;
numRaters++;
}
}
if (numRaters >= minimalRaters) {
double weightedAvg = totScore / numRaters;
movieWeightAvgs.add(new Rating(movieID, weightedAvg));
}
}
Collections.sort(movieWeightAvgs, Collections.reverseOrder());
return movieWeightAvgs;
}
public ArrayList<Rating> getSimilarRatingsByFilter(
String id, Integer numSimilarRaters, Integer minimalRaters,
Filter filterCriteria
) {
ArrayList<Rating> movieWeightAvgs = new ArrayList<Rating>();
ArrayList<Rating> similiraties = getSimilarities(id);
ArrayList<Rating> topSimilarites = new ArrayList<Rating>();
for (int i = 0; i < numSimilarRaters; i++) {
String raterID = similiraties.get(i).getItem();
double raterSim = similiraties.get(i).getValue();
topSimilarites.add(new Rating(raterID, raterSim));
}
ArrayList<String> movies = MovieDatabase.filterBy(filterCriteria);
for (String movieID : movies) {
double totScore = 0.0;
int numRaters = 0;
for (Rating simRater : topSimilarites) {
double simRating = simRater.getValue();
Rater raterRatings = RaterDatabase.getRater(simRater.getItem());
if (raterRatings.hasRating(movieID)) {
double movieRating = raterRatings.getRating(movieID);
totScore += simRating * movieRating;
numRaters++;
}
}
if (numRaters >= minimalRaters) {
double weightedAvg = totScore / numRaters;
movieWeightAvgs.add(new Rating(movieID, weightedAvg));
}
}
Collections.sort(movieWeightAvgs, Collections.reverseOrder());
return movieWeightAvgs;
}
public ArrayList<Rating> getAverageRatings(int minimalRaters) {
ArrayList<Rating> avgRatings = new ArrayList<Rating>();
ArrayList<String> movies = MovieDatabase.filterBy(new TrueFilter());
for (String movieID : movies) {
double avgRating = getAverageByIDMinRates(movieID, minimalRaters);
if (avgRating > 0.0) {
Rating selectedRating = new Rating(movieID, avgRating);
avgRatings.add(selectedRating);
}
}
return avgRatings;
}
public ArrayList<Rating> getAverageRatingsByFilter(
int minimalRaters, Filter filterCriteria
) {
ArrayList<Rating> moviesRatings = new ArrayList<Rating>();
ArrayList<String> movies = MovieDatabase.filterBy(filterCriteria);
for (String movieID : movies) {
double movieRating = getAverageByIDMinRates(movieID, minimalRaters);
if (movieRating > 0.0) {
moviesRatings.add(new Rating(movieID, movieRating));
}
}
// System.out.println("moviesRatings");
// System.out.println(moviesRatings);
return moviesRatings;
}
}