-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirstRatings.java
215 lines (175 loc) · 7.2 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
import edu.duke.*;
import java.util.*;
import org.apache.commons.csv.*;
public class FirstRatings {
public ArrayList<Movie> loadMovies(String filename) {
ArrayList<Movie> movies = new ArrayList<Movie>();
FileResource fr = new FileResource(filename);
CSVParser parser = fr.getCSVParser();
for (CSVRecord record : parser) {
String id = record.get("id");
String title = record.get("title");
String year = record.get("year");
String genres = record.get("genre");
String director = record.get("director");
String country = record.get("country");
String poster = record.get("poster");
int minutes = Integer.parseInt(record.get("minutes"));
Movie movieRecord = new Movie(
id,
title,
year,
genres,
director,
country,
poster,
minutes
);
movies.add(movieRecord);
}
return movies;
}
public ArrayList<EfficientRater> loadRaters(String filename) {
ArrayList<EfficientRater> result = new ArrayList<EfficientRater>();
HashMap<String,EfficientRater> raters = new HashMap<String,EfficientRater>();
FileResource fr = new FileResource(filename);
CSVParser parser = fr.getCSVParser();
for (CSVRecord record : parser) {
String id = record.get("rater_id");
String item = record.get("movie_id");
Double rating = Double.parseDouble(record.get("rating"));
EfficientRater raterRecord;
if (raters.containsKey(id)) {
raterRecord = raters.get(id);
}
else {
raterRecord = new EfficientRater(id);
}
raterRecord.addRating(item, rating);
raters.put(id, raterRecord);
}
for (EfficientRater raterRecord : raters.values()) {
result.add(raterRecord);
}
return result;
}
public HashMap<String,Integer> getDirectorsMovies(
ArrayList<Movie> movies
) {
HashMap<String,Integer> directorsMovies = new HashMap<String,Integer>();
for (Movie movieItem : movies) {
String directors = movieItem.getDirector().toLowerCase();
for (String director : directors.split("\\s*,\\s*")) {
if (directorsMovies.containsKey(director)) {
int numMovies = directorsMovies.get(director);
directorsMovies.put(director, numMovies+1);
}
else {
directorsMovies.put(director, 1);
}
}
}
return directorsMovies;
}
public HashMap<String,Integer> getDirectorsMaxMovies(
HashMap<String,Integer> directorsMovies
) {
HashMap<String,Integer> directorsMaxMovies = new HashMap<String,Integer>();
int maxMovies = 0;
for (String director : directorsMovies.keySet()) {
int numMovies = directorsMovies.get(director);
if (numMovies >= maxMovies) {
if (numMovies > maxMovies) {
maxMovies = numMovies;
directorsMaxMovies.clear();
}
directorsMaxMovies.put(director, numMovies);
}
}
return directorsMaxMovies;
}
public HashMap<String,ArrayList<Integer>> loadMovieRatings(
String filename
) {
HashMap<String,ArrayList<Integer>> movieRatings = new HashMap<String,ArrayList<Integer>>();
FileResource fr = new FileResource(filename);
CSVParser parser = fr.getCSVParser();
for (CSVRecord record : parser) {
String item = record.get("movie_id");
Integer raterID = Integer.parseInt(record.get("rater_id"));
ArrayList<Integer> arrRatings;
if (movieRatings.containsKey(item)) {
arrRatings = movieRatings.get(item);
}
else {
arrRatings = new ArrayList<Integer>();
}
arrRatings.add(raterID);
movieRatings.put(item, arrRatings);
}
return movieRatings;
}
public void testLoadMovies() {
ArrayList<Movie> movies = loadMovies("data/ratedmoviesfull.csv");
// System.out.println("Movies found:");
// for (Movie movieItem : movies) {
// System.out.println(movieItem);
// }
System.out.println("Found " + movies.size() + " movies");
int numComedy = 0;
int longerThan150 = 0;
for (Movie movieItem : movies) {
if (movieItem.getGenres().contains("Comedy")) {
numComedy++;
}
if (movieItem.getMinutes() > 150) {
longerThan150++;
}
}
System.out.println("\nNumber of Comedy movies: " + numComedy);
System.out.println("\nNumber of movies longer than 150: " + longerThan150);
HashMap<String,Integer> directorsMovies = getDirectorsMovies(movies);
HashMap<String,Integer> directorsMaxMovies = getDirectorsMaxMovies(directorsMovies);
System.out.println("\nDirectors with Most Number of Movies:");
System.out.println(directorsMaxMovies.toString());
}
public void testLoadRaters() {
String filename = "data/ratings.csv";
ArrayList<EfficientRater> raters = loadRaters(filename);
// System.out.println("Raters found:");
// for (Rater raterItem : raters) {
// System.out.println(raterItem);
// }
System.out.println("Found " + raters.size() + " raters");
String queryRater = "193";
int rater193 = 0;
for (EfficientRater raterItem : raters) {
if (raterItem.getID().equals(queryRater)) {
rater193 = raterItem.numRatings();
}
}
System.out.println("\nRater 193 has " + rater193 + " rating(s).");
ArrayList<EfficientRater> maxRaters = new ArrayList<EfficientRater>();
int maxRatings = 0;
for (EfficientRater raterItem : raters) {
int numRatings = raterItem.numRatings();
if (numRatings >= maxRatings) {
if (numRatings > maxRatings) {
maxRatings = numRatings;
maxRaters.clear();
}
maxRaters.add(raterItem);
}
}
System.out.println("\nMost Number of Ratings: " + maxRatings);
System.out.println("Raters with Most Number of Ratings:");
System.out.println(maxRaters.toString());
// HashMap<String,Integer> movieRatings = loadMovieRatings(filename);
HashMap<String,ArrayList<Integer>> movieRatings = loadMovieRatings(filename);
// System.out.println("\nMovie ratings:");
// System.out.println(movieRatings.toString());
String movieID = "1798709";
System.out.println("\nMovie ID-" + movieID + " has " + movieRatings.get(movieID).size() + " rating(s)");
System.out.println("\nThere are " + movieRatings.size() + " unique movies.");
}
}