-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirstRatings.java
271 lines (203 loc) · 9.97 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
262
263
264
265
266
267
268
269
270
271
/**
* Write a description of FirstRatings here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.*;
import edu.duke.*;
import org.apache.commons.csv.*;
import java.lang.*;
public class FirstRatings {
/*
private ArrayList<Movie> list; // A list holding Movie objects
private ArrayList<Rater> raterList; // A list holding rater objects
*/
private HashMap<String,Integer> numMoviesByDirector; // A map to keep count of number of movies (value) directed by a director(key)
private HashMap<String,Integer> numRatingsByRater; // A map to keep count of number of ratings (value) given by a rater(key)
public FirstRatings(){
//list = new ArrayList<Movie>();
//raterList = new ArrayList<Rater>();
numMoviesByDirector = new HashMap<String,Integer>();
numRatingsByRater = new HashMap<String,Integer>();
}
public ArrayList<Movie> loadMovies(String filename){
/*
* From each record in the provided filename, extract information about each movie and create an object of class Movie. Add this object to the arrayList of such movie objects
*/
ArrayList<Movie> list = new ArrayList<Movie> ();
FileResource fr = new FileResource(filename);
for (CSVRecord rec: fr.getCSVParser()){
Movie curMovie = new Movie(rec.get("id"),rec.get("title"),rec.get("year"),rec.get("genre"), rec.get("director"),
rec.get("country"), rec.get("poster"), Integer.parseInt(rec.get("minutes")));
//Add curMovie to list
list.add(curMovie);
}
return list;
}
private ArrayList<String> numMoviesGenre(String genre, ArrayList<Movie> list){
/*
* Computes the number of movies that are of the mentioned genre
*/
ArrayList<String> movieList = new ArrayList<String>();
for (int i = 0; i < list.size(); i++){
Movie curMovie = list.get(i);
String curGenre = curMovie.getGenres();
if (curGenre.contains(genre) || curGenre.contains(genre.toLowerCase()))
movieList.add(curMovie.getTitle());
}
return movieList;
}
private ArrayList<String> numMoviesWithMins(int numMin, ArrayList<Movie> list){
/*
* Computes the number of movies that are greater than given length
*/
ArrayList<String> movieList = new ArrayList<String>();
for (int i = 0; i < list.size(); i++){
Movie curMovie = list.get(i);
if (curMovie.getMinutes() > 150)
movieList.add(curMovie.getTitle());
}
return movieList;
}
private void numDirectors(ArrayList<Movie> list){
// Builds the numMoviesByDirector map from the list of Movie objects
for (int i = 0; i < list.size(); i++){
Movie curMovie = list.get(i);
String[] dir = curMovie.getDirector().split(","); // Array of directors as a movie can have multiple directors.
for (int j = 0; j < dir.length; j++){
if (numMoviesByDirector.containsKey(dir[j])){
int val = numMoviesByDirector.get(dir[j]);
numMoviesByDirector.put(dir[j],val+=1);
}
else
numMoviesByDirector.put(dir[j],1);
}
}
}
private void moviesWithDirectors(){
int numMovies = 0;
String director = "";
for (String dir : numMoviesByDirector.keySet()){
if (numMoviesByDirector.get(dir) > numMovies){
numMovies = numMoviesByDirector.get(dir);
director = dir;
}
}
System.out.println(director + " has a maximum of " + numMovies);
}
public void testLoadMovies(){
String filename = "ratedmoviesfull.csv";
ArrayList<Movie> list = loadMovies(filename);
if (list == null){
System.out.println("List is empty.");
return;
}
/*
// Print number of movies
for (int i = 0; i < list.size(); i++)
System.out.println(list.get(i).toString());
*/
System.out.println("Number of movies are: " + list.size());
// count number of movies of given genre
String genre = "Comedy";
System.out.println("There are " + numMoviesGenre(genre,list).size() + " movies of " + genre + " genre");
// Count number of movies with t mins
int t = 150;
System.out.println("There are " + numMoviesWithMins(t,list).size() + " movies of with length greater than " + t + " minutes.");
// Directors with maxMovies
numDirectors(list); // Build a map of director and his/her movies
moviesWithDirectors();
}
public ArrayList<Rater> loadRaters(String filename){
ArrayList<Rater> raterList = new ArrayList<Rater>();
FileResource fr = new FileResource("data/" + filename);
for (CSVRecord rec: fr.getCSVParser()){
String id = rec.get("rater_id");
Rater curRater = new EfficientRater(id); // Create a new rater
double curRating = Double.parseDouble(rec.get("rating"));
curRater.addRating(rec.get("movie_id"),curRating); // Add rating to the movie
raterList.add(curRater); // Add the current rater to the list
}
return raterList;
}
private int findNumOfRatings (String id, ArrayList<Rater> raterList){
// Returns the number of rating provided by the rater given by id
int count = 0;
for (int i = 0; i < raterList.size(); i++){
if (id.equals(raterList.get(i).getID())) // IDs match
count++;
}
return count;
}
private void buildNumRatingsByRaters(ArrayList<Rater> raterList){
// Builds the numRatingsByRater map.
for (int i = 0; i < raterList.size(); i++){
String curId = raterList.get(i).getID();
if (numRatingsByRater.containsKey(curId)){ // Increment the number of ratings the rater with id curId has provided
int numRatings = numRatingsByRater.get(curId);
numRatingsByRater.put(curId,numRatings+=1);
}
else
numRatingsByRater.put(curId,1); // Note the rating provided by the new rater
}
}
private void findMaxRatingsAndRaters(){
// Prints the maximum number of ratings a rater has given and prints the rater id and the number of ratings
String maxRaterId = "";
int maxNumRatings = 0;
for (String curId : numRatingsByRater.keySet()){
if (numRatingsByRater.get(curId) > maxNumRatings){
maxNumRatings = numRatingsByRater.get(curId);
maxRaterId = curId;
}
}
System.out.println("Rater " + maxRaterId + " has a maximum of " + maxNumRatings);
}
private int findNumOfRatingsOfMovie(String MovieId, ArrayList<Rater> raterList){
// Returns the number of ratings received by the movie denoted by MovieId
int numRatings = 0;
for (int i = 0; i < raterList.size(); i++){
if (raterList.get(i).hasRating(MovieId)) // .hasRating(String item) checks if the item was rated by the rater.
numRatings++;
}
return numRatings;
}
private ArrayList<String> ratedMovies(ArrayList<Rater> raterList){
// Returns a list of unique movies rated by the raters in a file
ArrayList<String> moviesRated = new ArrayList<String>();
for (int i = 0; i < raterList.size(); i++){
Rater curRater = raterList.get(i);
ArrayList<String> curMoviesList = curRater.getItemsRated();
for (int j = 0; j < curMoviesList.size(); j++){
if (!moviesRated.contains(curMoviesList.get(j)))
moviesRated.add(curMoviesList.get(j));
}
}
return moviesRated;
}
public void testLoadRaters(){
String filename = "ratings.csv";
ArrayList<Rater> raterList = loadRaters(filename); // Create a list of raters, called raterList
// Test number of ratings from a rater.
String raterId = "193";
System.out.println("Rater id:" + raterId + " has" + findNumOfRatings(raterId,raterList) + " ratings");
buildNumRatingsByRaters(raterList); // Build the rater to the number of ratings map
findMaxRatingsAndRaters(); // Print the rater who has maximum number of ratings along with the number of ratings
// Test what movie had how many ratings
String movieId = "1798709";
System.out.println("Movie with movie_id " + movieId + "was rated " + findNumOfRatingsOfMovie(movieId,raterList) + " times");
// Test number of unique movies rated by the raters
System.out.println("Total of " + ratedMovies(raterList).size() + " unique movies were rated");
/*
// Print raterID, number of ratings, and movie and its rating
for (int i = 0; i < raterList.size(); i++){
System.out.println(raterList.get(i).getID() + " " + raterList.get(i).numRatings());
ArrayList<String> ratedItems = raterList.get(i).getItemsRated(); // List of movies that are rated.
// Iterate through the rated items list to find rating for each item
for (int j = 0; j < ratedItems.size(); j++)
System.out.println(ratedItems.get(j) + " " + raterList.get(i).getRating(ratedItems.get(j)));
}
*/
}
}