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 pathweek2-secondRatings(assignment).java
93 lines (90 loc) · 2.98 KB
/
week2-secondRatings(assignment).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
/**
* Write a description of SecondRatings here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.*;
public class SecondRatings {
private ArrayList<Movie> myMovies;
private ArrayList<Rater> myRaters;
public SecondRatings() {
// default constructor
this("ratedmoviesfull.csv", "ratings.csv");
}
public SecondRatings(String moviefile, String ratingsfile) {
FirstRatings fr = new FirstRatings();
myMovies = fr.loadMovies(moviefile);
myRaters = fr.loadRaters(ratingsfile);
}
public int getMovieSize(){
return myMovies.size();
}
public int getRaterSize(){
return myRaters.size();
}
public ArrayList<Rating> getAverageRatings(int minimalRaters){
ArrayList<Rating> avgList = new ArrayList<Rating>();
for(Movie m: myMovies){
String movieID = m.getID();
double avg = getAverageByID(movieID,minimalRaters);
if(avg > 0.0){
Rating rating = new Rating(movieID, avg);
avgList.add(rating);
}
}
return avgList;
}
public String getTitle(String id){
for(Movie m: myMovies){
String movieId = m.getID();
if(movieId.equals(id)){
return m.getTitle();
}
}
return "No such a movie";
}
public String getID(String title){
for(Movie m: myMovies){
String movieTitle = m.getTitle();
if(movieTitle.equals(title)){
return m.getID();
}
}
return "NO SUCH TITLE.";
}
private double getAverageByID(String movieId,int minimalRaters){
double avg = 0.0;
double sum = 0.0;
int count = 0;
ArrayList<Double> rateList = new ArrayList<Double>();
//HashMap<String, ArrayList<double>> movie_rating = new HashMap<String, ArrayList<Integer>>();
for(Rater r: myRaters){
//ArrayList<Rating> ratingList = new ArrayList<Rating>();
ArrayList<String> itemList = r.getItemsRated();
/*for(String item: itemList){
double rates = r.getRating(item);
if(movie_rating.containsKey(item)){
movie_rating.put(item, movie_rating.get(item).add(rates));
}else{
ArrayList<double> rateList = new ArrayList<double>();
rateList.add(rates);
movie_rating.put(item,rateList);
}
}*/
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;
}
}