-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMovieAnalyzer.java
289 lines (250 loc) · 9.02 KB
/
MovieAnalyzer.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
class MovieAnalyzer {
class Movie {
String seriesTitle;
int releasedYear;
String certificate;
int runtime;
Set<String> genres;
float imdbRating;
String overview;
String metaScore;
String director;
List<String> stars;
int noOfVotes;
int gross;
public Movie(String seriesTitle, int releasedYear, String certificate, int runtime,
Set<String> genres, float imdbRating, String overview, String metaScore, String director,
List<String> stars, int noOfVotes, int gross) {
this.seriesTitle = seriesTitle;
this.releasedYear = releasedYear;
this.certificate = certificate;
this.runtime = runtime;
this.genres = genres;
this.imdbRating = imdbRating;
this.overview = overview;
this.metaScore = metaScore;
this.director = director;
this.stars = stars;
this.noOfVotes = noOfVotes;
this.gross = gross;
}
public String getSeriesTitle() {
return seriesTitle;
}
public int getReleasedYear() {
return releasedYear;
}
public String getCertificate() {
return certificate;
}
public int getRuntime() {
return runtime;
}
public Set<String> getGenres() {
return genres;
}
public float getImdbRating() {
return imdbRating;
}
public List<StarRating> getStarRating() {
List<StarRating> starRatings = new ArrayList<>();
for (String star : stars
) {
starRatings.add(new StarRating(star, imdbRating));
}
return starRatings;
}
public String getOverview() {
return overview;
}
public String getMetaScore() {
return metaScore;
}
public String getDirector() {
return director;
}
public List<String> getStars() {
return stars;
}
public List<List<String>> getCoStars() {
List<List<String>> coStars = new ArrayList<>();
for (int i = 0; i < stars.size(); i++) {
for (int j = i + 1; j < stars.size(); j++) {
List<String> part = new ArrayList<>();
part.add(stars.get(i));
part.add(stars.get(j));
coStars.add(part);
}
}
return coStars;
}
public int getNoOfVotes() {
return noOfVotes;
}
public int getGross() {
return gross;
}
public List<StarGross> getStarGross() {
List<StarGross> starGrosses = new ArrayList<>();
if (gross > 0) {
for (String star : stars
) {
starGrosses.add(new StarGross(star, gross));
}
}
return starGrosses;
}
}
class StarRating {
String star;
float rating;
public StarRating(String star, float rating) {
this.star = star;
this.rating = rating;
}
public String getStar() {
return star;
}
public float getRating() {
return rating;
}
}
class StarGross {
String star;
int gross;
public StarGross(String star, int gross) {
this.star = star;
this.gross = gross;
}
public String getStar() {
return star;
}
public int getGross() {
return gross;
}
}
List<Movie> movies;
public MovieAnalyzer(String datasetPath) {
movies = new ArrayList<>();
try {
Scanner scanner = new Scanner(new FileReader(datasetPath, StandardCharsets.UTF_8));
scanner.nextLine();
String line;
Pattern pattern = Pattern.compile("(,)?((\"[^\"]*(\"{2})*[^\"]*\")*[^,]*)");
while (scanner.hasNextLine()) {
line = scanner.nextLine();
Matcher matcher = pattern.matcher(line);
List<String> data = new ArrayList<>();
while (matcher.find()) {
String cell = matcher.group(2);
if (cell.length() > 0 && cell.charAt(0) == '"') {
data.add(cell.substring(1, cell.length() - 1));
} else {
data.add(cell);
}
}
String seriesTitle = data.get(1);
int releasedYear = Integer.parseInt(data.get(2));
String certificate = data.get(3);
int runtime = Integer.parseInt(data.get(4).split(" ")[0]);
Set<String> genres = new HashSet<>(
Arrays.asList(data.get(5).split(", ")));
float imdbRating = Float.parseFloat(data.get(6));
String overview = data.get(7);
String metaScore = data.get(8);
String director = data.get(9);
List<String> stars = Arrays.asList(data.get(10), data.get(11), data.get(12), data.get(13));
stars.sort(Comparator.naturalOrder());
int noOfVotes = Integer.parseInt(data.get(14));
int gross = 0;
if (data.get(15).length() != 0) {
gross = Integer.parseInt(data.get(15).replace(",", ""));
}
Movie movie = new Movie(seriesTitle, releasedYear, certificate, runtime, genres, imdbRating,
overview, metaScore, director, stars, noOfVotes, gross);
movies.add(movie);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public Map<Integer, Integer> getMovieCountByYear() {
return movies.stream()
.sorted(Comparator.comparing(Movie::getReleasedYear).reversed()).collect(
Collectors.groupingBy(Movie::getReleasedYear, LinkedHashMap::new,
Collectors.summingInt(p -> 1)));
}
public Map<String, Integer> getMovieCountByGenre() {
return movies.stream().map(Movie::getGenres).flatMap(Collection::stream)
.collect(Collectors.groupingBy(p -> p, Collectors.summingInt(p -> 1))).entrySet().stream()
.sorted(Comparator.comparing(Map.Entry<String, Integer>::getValue).reversed()
.thenComparing(Entry::getKey)).collect(
Collectors.toMap(Map.Entry<String, Integer>::getKey,
Map.Entry<String, Integer>::getValue,
(oldValue, newValue) -> oldValue, LinkedHashMap::new));
}
public Map<List<String>, Integer> getCoStarCount() {
return movies.stream().map(Movie::getCoStars).flatMap(Collection::stream)
.collect(Collectors.groupingBy(e -> e, Collectors.summingInt(p -> 1))).entrySet().stream()
.sorted(Comparator.comparing(Map.Entry<List<String>, Integer>::getValue).reversed()
.thenComparing(e -> e.getKey().get(0))).collect(
Collectors.toMap(Map.Entry<List<String>, Integer>::getKey,
Map.Entry<List<String>, Integer>::getValue,
(oldValue, newValue) -> oldValue, LinkedHashMap::new));
}
public List<String> getTopMovies(int topk, String by) {
if (by.equals("runtime")) {
return movies.stream().sorted(
Comparator.comparing(Movie::getRuntime).reversed()
.thenComparing(Movie::getSeriesTitle))
.limit(topk).map(Movie::getSeriesTitle).collect(Collectors.toList());
} else {
return movies.stream().sorted(
Comparator.comparing((Movie e) -> e.getOverview().length()).reversed()
.thenComparing(Movie::getSeriesTitle)).limit(topk).map(Movie::getSeriesTitle)
.collect(Collectors.toList());
}
}
public List<String> getTopStars(int topk, String by) {
if (by.equals("rating")) {
return movies.stream().map(Movie::getStarRating).flatMap(Collection::stream)
.collect(Collectors.groupingBy(StarRating::getStar, Collectors.averagingDouble(
StarRating::getRating)))
.entrySet().stream()
.sorted(Comparator.comparing(Map.Entry<String, Double>::getValue).reversed()
.thenComparing(Entry::getKey)).map(Entry::getKey).limit(topk)
.collect(Collectors.toList());
} else {
return movies.stream().map(Movie::getStarGross).flatMap(Collection::stream)
.collect(Collectors.groupingBy(StarGross::getStar, Collectors.averagingDouble(
StarGross::getGross)))
.entrySet().stream()
.sorted(Comparator.comparing(Map.Entry<String, Double>::getValue).reversed()
.thenComparing(Entry::getKey)).map(Entry::getKey).limit(topk)
.collect(Collectors.toList());
}
}
public List<String> searchMovies(String genre, float minrating, int maxruntime) {
return movies.stream().filter(p -> p.getGenres().contains(genre))
.filter(p -> p.getImdbRating() >= minrating)
.filter(p -> p.getRuntime() <= maxruntime).map(Movie::getSeriesTitle).sorted()
.collect(Collectors.toList());
}
}