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 pathThirdRatings.java
71 lines (68 loc) · 2.29 KB
/
ThirdRatings.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
/**
* Write a description of ThirdRatings here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.*;
public class ThirdRatings {
private ArrayList<Rater> myRaters;
public ThirdRatings() {
// default constructor
this("ratings.csv");
}
public ThirdRatings(String ratingsfile) {
FirstRatings fr = new FirstRatings();
myRaters = fr.loadRaters(ratingsfile);
}
public int getRaterSize(){
return myRaters.size();
}
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>();
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;
}
}