-
Notifications
You must be signed in to change notification settings - Fork 0
/
FourthRatings.java
149 lines (118 loc) · 4.22 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
import edu.duke.*;
import java.util.*;
import org.apache.commons.csv.*;
/**
* Write a description of FourthRatings here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class FourthRatings {
private double getAverageByID(String id,int minimalRaters){
double average =0;
double total=0;
int countRaters= 0;
ArrayList<Rater> myRaters = RaterDatabase.getRaters();
for(Rater rater:myRaters){
if(rater.hasRating(id)){
countRaters++;
total = total + rater.getRating(id);
}
}
if(countRaters >= minimalRaters){
average = total/countRaters;
}
return average;
}
public ArrayList<Rating> getAverageRatings(int minimalRaters){
ArrayList<Rating> avgRatingList = new ArrayList<Rating>();
ArrayList<String> movies = MovieDatabase.filterBy(new TrueFilter());
for(String id:movies){
double avg = getAverageByID(id,minimalRaters);
if(avg>0.0){
Rating currRating = new Rating(id,avg);
avgRatingList.add(currRating);
}
}
return avgRatingList;
}
public ArrayList<Rating> getAverageRatingsByFilter(int minimalRaters,Filter filterCriteria)
{
ArrayList<Rating> avgRatingListByFilter = new ArrayList<Rating>();
ArrayList<String> movies = MovieDatabase.filterBy(filterCriteria);
for(String id:movies){
double avg = getAverageByID(id, minimalRaters);
if(avg>0.0){
Rating currRating = new Rating(id,avg);
avgRatingListByFilter.add(currRating);
}
}
return avgRatingListByFilter;
}
private double dotProduct(Rater me,Rater r){
double similarValue=0;
ArrayList<String> itemsRatedByMe = me.getItemsRated();
for(String movieID:itemsRatedByMe){
if(r.hasRating(movieID)){
double rRating = r.getRating(movieID)-5;
double myRating = me.getRating(movieID)-5;
similarValue = similarValue + (rRating*myRating);
}
}
return similarValue;
}
private ArrayList<Rating> getSimilarities(String id){
ArrayList<Rating> list = new ArrayList<Rating>();
ArrayList<Rater> allRaters = RaterDatabase.getRaters();
// input id implied raterByMe
Rater me = RaterDatabase.getRater(id);
for(Rater r: allRaters){
String raterID = r.getID();
// add dot_product(r,me) to list if r !=me
if(raterID.equals(id)== false){
double similarValue = dotProduct(me,r);
Rating similarRating = new Rating(raterID,similarValue);
list.add(similarRating);
}
}
Collections.sort(list, Collections.reverseOrder());
return list;
}
public ArrayList<Rating> getSimilarRatings(String id, int numSimilarRaters, int minimalRaters){
return getSimilarRatingsByFilter(id, numSimilarRaters, minimalRaters, new TrueFilter());
}
public ArrayList<Rating> getSimilarRatingsByFilter(String id,int numSimilarRaters, int minimalRaters, Filter filterCriteria){
ArrayList<Rating> movieSimRatings = new ArrayList();
ArrayList<Rating> raterSimList = getSimilarities(id);
ArrayList<String> movieIDList = new ArrayList();
HashMap<String,Double> similarMap = new HashMap();
int mapSize = getSimilarities(id).size();
int minIndex = Math.min(mapSize, numSimilarRaters);
for(Rating similar:getSimilarities(id).subList(0,minIndex)){
if(similar.getValue()>0){
similarMap.put(similar.getItem(), similar.getValue());
}
}
for(String movieID:MovieDatabase.filterBy(filterCriteria)){
int count =0;
double total =0;
for(Rater curRater:RaterDatabase.getRaters()){
double rating = -1;
if(similarMap.containsKey(curRater.getID()) && curRater.hasRating(movieID)){
rating = curRater.getRating(movieID) * similarMap.get(curRater.getID());
}
if(rating ==-1){}
else{
count++;
total = total + rating;
}
}
if(count< minimalRaters || total==0){}
else{
movieSimRatings.add(new Rating(movieID, total/count));
}
}
Collections.sort(movieSimRatings, Collections.reverseOrder());
return movieSimRatings;
}
}