-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFourthRatings.java
161 lines (125 loc) · 4.05 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
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
public class FourthRatings {
//private ArrayList<Rater> myRaters;
/*
public FourthRatings() {
// default constructor
this("ratings_short.csv");
}
public FourthRatings(String ratingsfile) {
//Create a ThirdRatings Object
System.out.println("I am in Fourth rating");
FirstRatings fr = new FirstRatings();
myRaters=fr.loadRaters(ratingsfile);
}
public int getRaterSize(){
System.out.println("Getting from fourth rating");
return myRaters.size();
}*/
private double getAveragebyId(String movieId,int minimalRaters){
int numOfRaters=0;
double sum =0.0;
ArrayList<Rater> myRaters = RaterDatabase.getRaters();
for (Rater rater:myRaters){
if(rater.hasRating(movieId)){
numOfRaters++;
}
}
if (numOfRaters>=minimalRaters){
for (Rater rater1:myRaters){
if(rater1.hasRating(movieId)){
sum+=rater1.getRating(movieId);
}
}
double ave= sum/numOfRaters;
return ave;
}
else{
return 0.0;
}
}
public ArrayList<Rating> getAverageRatings (int minimalRaters){
ArrayList<String> myMovies = MovieDatabase.filterBy(new TrueFilter());
ArrayList<Rating> rating = new ArrayList<Rating>();
//Rating rat = new Rating("",0.0);
for (String movie_id:myMovies){
//String movie_Id= movie.getID();
if (getAveragebyId(movie_id,minimalRaters)!=0.0){
Rating rat = new Rating(movie_id,getAveragebyId(movie_id,minimalRaters));
//rat = new Rating(movie_id,getAveragebyId(movie_id,minimalRaters));
rating.add(rat);
}
}
Collections.sort(rating);
return rating;
}
/*
public double averageRatingMovie(String movie_id){
double numOfRaters=0.0;
double sum =0.0;
for (Rater rater:myRaters){
if(rater.hasRating(movie_id)){
numOfRaters++;
sum+= rater.getRating(movie_id);
}
}
double ave= sum/numOfRaters;
return ave;
}
*/
public ArrayList<Rating> getAverateRatingsByFilter(int minimalRaters,Filter filterCriteria){
ArrayList<Rating> filterRating = new ArrayList<Rating>();
ArrayList<String> movie_ids = MovieDatabase.filterBy(filterCriteria);
//System.out.println("I am in averege filer "+movie_ids);
for (String movie_id:movie_ids){
//System.out.println(movie_id);
//String movie_Id= movie.getID();
//System.out.println(getAveragebyId(movie_id,minimalRaters));
if (getAveragebyId(movie_id,minimalRaters)!=0.0){
Rating rat = new Rating(movie_id,getAveragebyId(movie_id,minimalRaters));
//System.out.println("I am here "+rat.getItem());
//rat = new Rating(movie_id,getAveragebyId(movie_id,minimalRaters));
filterRating.add(rat);
}
}
Collections.sort(filterRating);
return filterRating;
}
private double dotProducts(Rater me, Rater r){
//Get the movie ids and ratings from the ratings database
HashMap<String,Rating> moviesRating = me.getMyRatings();
double merDotProduct =0.0;
for (String Id:moviesRating.keySet()){
double myScale = me.getRating(Id)-5;
if (r.hasRating(Id)){
merDotProduct += myScale*(r.getRating(Id)-5);
}
}
return merDotProduct;
}
private ArrayList<Rating> getSimilarities (String id){
//Create an ArrayList for similar ratings
ArrayList<Rating> similarRatings = new ArrayList<Rating>();
//Create a rater object me
Rater me = RaterDatabase.getRater(id);
for (Rater other:RaterDatabase.getRaters()){
if (me.getID().equals(other.getID())){
double meotherDotProduct = dotProducts(me,other);
if (meotherDotProduct>0){
Rating rate = new Rating(other.getID(),meotherDotProduct);
similarRatings.add(rate);
}
}
Collections.sort(similarRatings,Collections.reverseOrder());
}
return similarRatings;
}
public ArrayList<Rating> getSimilarRatings(String id,int numSimilarRaters, int minimalRaters){
ArrayList<Rating> ratingMovies = new ArrayList<Rating>();
ArrayList<Rating> ratingrater = new ArrayList<Rating>();
ArrayList<String> movies = MovieDatabase.filterBy(new TrueFilter());
return ratingMovies;
}
}