aleheux-tc
/
Java-Programming-and-Software-Engineering-Fundamentals-Java-Programming-Build-a-Recommendation-Syst
Public
forked from mxc19912008/Java-Programming-and-Software-Engineering-Fundamentals-Java-Programming-Build-a-Recommendation-Syst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFourthRatings.java
175 lines (167 loc) · 5.95 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
/**
* Write a description of FourthRatings here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.*;
public class FourthRatings {
public ArrayList<Rating> getAverageRatings(int minimalRaters){
ArrayList<Rating> avgList = new ArrayList<Rating>();
ArrayList<String> movies = MovieDatabase.filterBy(new TrueFilter());
for(String m: movies){
String movieID = m;
double avg = getAverageByID(movieID,minimalRaters);
if(avg > 0.0){
Rating rating = new Rating(movieID, avg);
avgList.add(rating);
}
}
return avgList;
}
private double getAverageByID(String movieId,int minimalRaters){
double avg = 0.0;
double sum = 0.0;
int count = 0;
ArrayList<Double> rateList = new ArrayList<Double>();
ArrayList<Rater> myRaters = RaterDatabase.getRaters();
for(Rater r: myRaters){
ArrayList<String> itemList = r.getItemsRated();
for(String item: itemList){
if(item.equals(movieId)){
rateList.add(r.getRating(item));
}
}
}
for(double rate: rateList){
sum += rate;
count++;
}
if(count >= minimalRaters){
avg = sum / count;
}
return avg;
}
public ArrayList<Rating> getAverageRatingsByFilter(int minimalRaters,
Filter filterCriteria){
ArrayList<Rating> result = new ArrayList<Rating>();
ArrayList<String> filteredMovies = MovieDatabase.filterBy(filterCriteria);
for(String movieId: filteredMovies){
if(getAverageByID(movieId,minimalRaters) > 0.0){
Rating rating = new Rating(movieId,getAverageByID(movieId,minimalRaters));
result.add(rating);
}
}
return result;
}
public int getRaterSize(){
ArrayList<Rater> myRaters = RaterDatabase.getRaters();
return myRaters.size();
}
private double dotProduct(Rater me, Rater r) {
double dotProduct = 0;
ArrayList<String> myMovies = me.getItemsRated();
for (String id: myMovies)
{
if (r.hasRating(id))
{
double myRating = me.getRating(id);
double rRating = r.getRating(id);
myRating -= 5;
rRating -= 5;
dotProduct += myRating * rRating;
}
}
return dotProduct;
}
private ArrayList<Rating> getSimilarities(String id) {
ArrayList<Rating> list = new ArrayList<Rating>();
Rater me = RaterDatabase.getRater(id);
for(Rater r: RaterDatabase.getRaters()) {
// add dot_product(r, me) to list if r!= me
if(!r.equals(me)) {
double product = dotProduct(me, r);
if (product > 0)
list.add(new Rating(r.getID(), product));
}
}
Collections.sort(list, Collections.reverseOrder());
return list;
}
public ArrayList<Rating> getRecommendations(String id, int numRaters) {
ArrayList<Rating> list = getSimilarities(id);
ArrayList<Rating> res = new ArrayList<Rating>();
ArrayList<String> movies = MovieDatabase.filterBy(new TrueFilter());
for(String movieID: movies) {
double weightedAverage = 0;
double sum = 0;
int countRaters = 0;
for(int k=0; k < numRaters; k++) {
Rating r = list.get(k);
String raterID = r.getItem();
double weight = r.getValue();
Rater myRater = RaterDatabase.getRater(raterID);
if(myRater.hasRating(movieID)) {
countRaters++;
sum += weight * myRater.getRating(movieID);
}
}
weightedAverage = sum / countRaters;
res.add(new Rating(movieID, weightedAverage));
}
Collections.sort(res, Collections.reverseOrder());
return res;
}
public ArrayList<Rating> getSimilarRatings(String id, int numSimilarRaters, int minimalRaters) {
ArrayList<Rating> res = new ArrayList<Rating>();
ArrayList<Rating> list = getSimilarities(id);
ArrayList<String> movies = MovieDatabase.filterBy(new TrueFilter());
for (String movieID : movies) {
double weightedAverage = 0;
double sum = 0;
int countRaters = 0;
for (int i = 0; i < numSimilarRaters; i++) {
Rating r = list.get(i);
double weight = r.getValue();
String raterID = r.getItem();
Rater myRater = RaterDatabase.getRater(raterID);
if(myRater.hasRating(movieID)) {
countRaters++;
sum += weight * myRater.getRating(movieID);
}
}
if (countRaters >= minimalRaters) {
weightedAverage = sum / countRaters;
res.add(new Rating(movieID, weightedAverage));
}
}
Collections.sort(res, Collections.reverseOrder());
return res;
}
public ArrayList<Rating> getSimilarRatingsByFilter(String id, int numSimilarRaters, int minimalRaters, Filter filterCriteria) {
ArrayList<Rating> res = new ArrayList<Rating>();
ArrayList<Rating> list = getSimilarities(id);
ArrayList<String> movies = MovieDatabase.filterBy(filterCriteria);
for (String movieID : movies) {
double weightedAverage = 0;
double sum = 0;
int countRaters = 0;
for (int i = 0; i < numSimilarRaters; i++) {
Rating r = list.get(i);
double weight = r.getValue();
String raterID = r.getItem();
Rater myRater = RaterDatabase.getRater(raterID);
if(myRater.hasRating(movieID)) {
countRaters++;
sum += weight * myRater.getRating(movieID);
}
}
if (countRaters >= minimalRaters) {
weightedAverage = sum / countRaters;
res.add(new Rating(movieID, weightedAverage));
}
}
Collections.sort(res, Collections.reverseOrder());
return res;
}
}