-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirstRatings.java
261 lines (202 loc) · 7.59 KB
/
FirstRatings.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import edu.duke.*;
import java.util.*;
import org.apache.commons.csv.*;
public class FirstRatings {
public ArrayList<Movie> loadMovies(String fileName){
System.out.println(fileName);
FileResource fr = new FileResource(fileName);
CSVParser parser = fr.getCSVParser();
ArrayList<Movie> movies = new ArrayList<Movie>();
for (CSVRecord currRow: parser){
Movie currMovie = new Movie(currRow.get("id"), currRow.get("title"), currRow.get("year"), currRow.get("genre"),
currRow.get("director"),currRow.get("country"),currRow.get("poster"),Integer.parseInt(currRow.get("minutes")));
movies.add(currMovie);
}
return movies;
}
public void moviesComedyGenre(ArrayList<Movie> movies){
int numComedy =0;
for (Movie currMovie:movies){
if (currMovie.getGenres().contains("Comedy") || currMovie.getGenres().contains("comedy")){
numComedy++;
//System.out.println(currMovie.getMinutes());
}
}
System.out.println("Comedy Genre Movies "+numComedy);
}
public void moviesLength(ArrayList<Movie> movies){
int numLength =0;
for (Movie currMovie:movies){
if (currMovie.getMinutes()>150){
numLength++;
//System.out.println(currMovie.getMinutes());
}
}
System.out.println("Movies Greater Than 150 minutes "+numLength);
}
public HashMap<String,Integer> maximumNumMoviesDirector(ArrayList<Movie> movies){
HashMap<String,Integer> directorNumMovies = new HashMap<String,Integer>();
for (Movie movie:movies){
String directorName = movie.getDirector();
if(!directorNumMovies.containsKey(directorName)){
directorNumMovies.put(directorName, 1);
}
else{
directorNumMovies.put(directorName,directorNumMovies.get(directorName)+1);
}
}
return directorNumMovies;
}
public void testLoadMovies(){
String fileName = "C:/Users/aw634c/newJava/workspace/MovieRecommender/src/data/ratedmovies_short.csv";
//FileResource fr = new FileResource(fileName);
ArrayList<Movie> movies = loadMovies(fileName);
/*for (Movie currMovie:movies){
System.out.println(currMovie.getTitle());
}*/
System.out.println(movies.size());
moviesComedyGenre(movies);
moviesLength(movies);
//Build a directory of the Directors and the number of movies the director has directed
HashMap<String,Integer> directorNames = maximumNumMoviesDirector(movies);
System.out.println("Maximum Number of movies by Director");
Set entrySet = directorNames.entrySet();
Iterator it = entrySet.iterator();
int maxNumMovies =0;
String directorName="";
while(it.hasNext()){
Map.Entry me = (Map.Entry)it.next();
int movieNum = (int) me.getValue();
if (movieNum>=maxNumMovies){
maxNumMovies = (int) movieNum;
directorName = (String) me.getKey();
}
}
System.out.println(directorNames.get(directorName));
System.out.println("Max Number of Movies by a director "+directorName);
System.out.println(directorNames);
}
public void getNumRaters(ArrayList<Rater> raters,String id){
int numOfRatings =0;
for (Rater rater:raters){
//System.out.println(rater.getID());
if (rater.getID().equals(id)){
//System.out.println("I am inside "+rater.numRatings());
numOfRatings= rater.numRatings();
}
}
System.out.println("No of Ratings by one "+numOfRatings);
}
public ArrayList<Rater> loadRaters(String fileName){
FileResource fr = new FileResource(fileName);
CSVParser parser = fr.getCSVParser();
ArrayList<Rater> raters = new ArrayList<Rater>();
String tempId ="";
Rater rater = new EfficientRater("");
//PlainRater rater = new PlainRater("");
for (CSVRecord currRec: parser){
String myId = currRec.get("rater_id");
String movId = currRec.get("movie_id");
if(currRec.get("rater_id").equals(rater.getID()) ){
rater.addRating(movId, Double.parseDouble(currRec.get("rating")));
//raters.add(rater);
}
else{
rater = new EfficientRater(myId);
rater.addRating(currRec.get("movie_id"), Double.parseDouble(currRec.get("rating")));
raters.add(rater);
}
}
//System.out.println("This is of the raters "+raters.size());
/*for (Rater currRater:raters){
System.out.println("This is the number of rating by rater "+currRater.getID() +" "+currRater.numRatings());
}*/
return raters;
}
public void getRaterWithNumOfMovies(ArrayList<Rater> raters,HashMap<String,Integer> raterWithNumOfMovies){
for(Rater rater:raters){
if(!raterWithNumOfMovies.containsKey(rater.getID())){
raterWithNumOfMovies.put(rater.getID(),1);
}else{
int num=raterWithNumOfMovies.get(rater.getID());
num+=1;
raterWithNumOfMovies.put(rater.getID(),num);
}
}
}
public void getRaterWithMaxNumOfMovies(HashMap<String,Integer> raterWithNumOfMovies,Integer maxValue){
String maxKey="";
for(String s:raterWithNumOfMovies.keySet()){
if(raterWithNumOfMovies.get(s)==maxValue){
maxKey=s;
}
}
System.out.println("maxKey "+maxKey);
}
public int getSumOfRatingsPerMovie(ArrayList<Rater> raters,String movie_id){
int sum=0;
for(Rater rater:raters){
if(rater.hasRating(movie_id)){
sum+=rater.getRating(movie_id);
}
}
return sum;
}
public void maximunNumOfRatings(int raterId, ArrayList<Rater> raters){
int count=0;
for (Rater currRater:raters){
if(Integer.parseInt(currRater.getID())==(raterId)){
count++;
}
}
System.out.println("Number of Ratings by: "+raterId+" is "+count);
}
public void maximunNumOfRatingsMovie(int movieId, ArrayList<Rater> raters){
int count=0;
for (Rater currRater:raters){
if(currRater.hasRating(Integer.toString(movieId))){
//System.out.println("This is the Rater Id:"+currRater.getID());
count++;
}
}
System.out.println("Number of Ratings by: "+movieId+" is "+count);
}
public void testLoadRaters(){
String fileName = "C:/Users/aw634c/newJava/workspace/MovieRecommender/src/data/ratings_short.csv";
//FileResource fr = new FileResource(fileName);
ArrayList<Rater> raters = loadRaters(fileName);
ArrayList<String> numOfDiffMoviesTotal = new ArrayList<String>();
System.out.println("This is the size of raters class "+ raters.size());
int raterId=193;
maximunNumOfRatings(raterId,raters);
int movieId = 1798709;
maximunNumOfRatingsMovie(movieId,raters);
for (Rater currRater:raters){
HashMap<String, Rating> rating = currRater.getMyRatings();
for(String str:rating.keySet()){
if (!numOfDiffMoviesTotal.contains(str)){
numOfDiffMoviesTotal.add(str);
}
}
}
System.out.println("Num of Movies Rated by the Raters:"+numOfDiffMoviesTotal.size() );
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//FirstRatings mr = new FirstRatings();
//mr.testLoadMovies();
//FirstRatings fr = new FirstRatings();
//fr.testLoadRaters();
//MovieRunnerAverage mr1 = new MovieRunnerAverage();
//mr1.printAverageRatings();
//mr1.getAverageRatingOneMovie();
MovieRunnerWithFilters mr1 = new MovieRunnerWithFilters();
mr1.printAverageRatings();
//mr1.printAverageRatingsByYear();
//mr1.printAverageRatingsByGenre();
//mr1.printAverageRatingsByMinutes();
//mr1.printAverageRatingsByDirector();
//mr1.printAverageRatingsByYearAfterAndGenre();
//mr1.printAverageRatingsByDirectorsAndMinutes();
}
}