-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathFirstRatings.java
193 lines (156 loc) · 7.45 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
/**
* loadMovies and loadRaters methods add data from the related csv files into corresponding
* ArrayLists.
*
* @ Konstantin Krumin
* @ Version: 1.0 (February 14, 2020)
*/
import edu.duke.*;
import java.util.*;
import org.apache.commons.csv.*;
public class FirstRatings {
public ArrayList<Movie> loadMovies (String filename) {
ArrayList<Movie> movieData = new ArrayList<Movie> ();
FileResource fr = new FileResource("data/" + filename + ".csv");
CSVParser parser = fr.getCSVParser();
for (CSVRecord record: parser) {
String currentID = record.get(0);
String currentTitle = record.get(1);
String currentYear = record.get(2);
String currentCountry = record.get(3);
String currentGenre = record.get(4);
String currentDirector = record.get(5);
int currentMinutes = Integer.parseInt(record.get(6));
String currentPoster = record.get(7);
Movie currentMovie = new Movie(currentID, currentTitle, currentYear, currentGenre, currentDirector,
currentCountry, currentPoster, currentMinutes);
movieData.add(currentMovie);
}
return movieData;
}
// test cases for the file
public void testLoadMovies () {
ArrayList<Movie> movies = loadMovies("ratedmoviesfull");
System.out.println("Number of movies: " + movies.size());
String countInGenre = "Comedy"; // variable
int countComedies = 0;
int minutes = 150; // variable
int countMinutes = 0;
for (Movie movie : movies) {
if (movie.getGenres().contains(countInGenre)) {
countComedies +=1;
}
if (movie.getMinutes() > minutes) {
countMinutes +=1;
}
}
System.out.println("There are " + countComedies + " comedies in the list");
System.out.println("There are " + countMinutes + " movies with more than " + minutes +
" minutes in the list");
// Create a HashMap with count of how many movies each particular director filmed
HashMap<String,Integer> countMoviesByDirector = new HashMap<String,Integer> ();
for (Movie movie : movies) {
String[] directors = movie.getDirector().split(",");
for (String director : directors ) {
director = director.trim();
if (! countMoviesByDirector.containsKey(director)) {
countMoviesByDirector.put(director, 1);
} else {
countMoviesByDirector.put(director, countMoviesByDirector.get(director) + 1);
}
}
}
// Count max number of movies directed by a particular director
int maxNumOfMovies = 0;
for (String director : countMoviesByDirector.keySet()) {
if (countMoviesByDirector.get(director) > maxNumOfMovies) {
maxNumOfMovies = countMoviesByDirector.get(director);
}
}
// Create an ArrayList with directors from the list that directed max number of movies
ArrayList<String> directorsList = new ArrayList<String> ();
for (String director : countMoviesByDirector.keySet()) {
if (countMoviesByDirector.get(director) == maxNumOfMovies) {
directorsList.add(director);
}
}
System.out.println("Max number of movies directed by one director: " + maxNumOfMovies);
System.out.println("Directors who directed that many movies are " + directorsList);
}
public ArrayList<Rater> loadRaters (String filename) {
ArrayList<Rater> ratersData = new ArrayList<Rater> ();
ArrayList<String> listOfIDs = new ArrayList<String> ();
FileResource fr = new FileResource("data/" + filename + ".csv");
CSVParser parser = fr.getCSVParser();
for (CSVRecord record : parser) {
String currentRaterID = record.get(0);
String currentMovieID = record.get(1);
double currentMovieRating = Double.parseDouble(record.get(2));
if (! listOfIDs.contains(currentRaterID)) {
Rater currentRater = new Rater(currentRaterID);
ratersData.add(currentRater);
currentRater.addRating(currentMovieID, currentMovieRating);
} else {
for (int k=0; k < ratersData.size(); k++) {
if (ratersData.get(k).getID().equals(currentRaterID)) {
ratersData.get(k).addRating(currentMovieID, currentMovieRating);
}
}
}
listOfIDs.add(currentRaterID);
}
return ratersData;
}
// test cases for the file
public void testLoadRaters () {
ArrayList<Rater> raters = loadRaters("ratings");
System.out.println("Total number of raters: " + raters.size());
HashMap<String, HashMap<String, Double>> hashmap = new HashMap<String, HashMap<String, Double>> ();
for (Rater rater : raters) {
HashMap<String, Double> ratings = new HashMap<String, Double> ();
ArrayList<String> itemsRated = rater.getItemsRated();
for (int i=0; i < itemsRated.size(); i++) {
String movieID = itemsRated.get(i);
double movieRating = rater.getRating(movieID);
ratings.put(movieID, movieRating);
}
hashmap.put(rater.getID(), ratings);
}
String raterID = "193"; //rater_id
int ratingsSize = hashmap.get(raterID).size();
System.out.println("Number of ratings for the rater " + raterID + " : " + ratingsSize);
int maxNumOfRatings = 0;
for (String key : hashmap.keySet()) {
int currAmountOfRatings = hashmap.get(key).size();
if (currAmountOfRatings > maxNumOfRatings) {
maxNumOfRatings = currAmountOfRatings;
}
}
System.out.println("Maximum number of ratings by any rater : " + maxNumOfRatings);
ArrayList<String> raterWithMaxNumOfRatings = new ArrayList<String> ();
for (String key : hashmap.keySet()) {
int currAmountOfRatings = hashmap.get(key).size();
if (maxNumOfRatings == currAmountOfRatings) {
raterWithMaxNumOfRatings.add(key);
}
}
System.out.println("Rater(s) with the most number of movies rated : " + raterWithMaxNumOfRatings);
String movieID = "1798709";
int numOfRatings = 0;
for (String key : hashmap.keySet()) {
if(hashmap.get(key).containsKey(movieID)) {
numOfRatings +=1;
}
}
System.out.println("Number of ratings movie " + movieID + " has : " + numOfRatings);
ArrayList<String> uniqueMovies = new ArrayList<String> ();
for (String key : hashmap.keySet()) {
for (String currMovieID : hashmap.get(key).keySet()) {
if (! uniqueMovies.contains(currMovieID)) {
uniqueMovies.add(currMovieID);
}
}
}
System.out.println("Total number of movies that were rated : " + uniqueMovies.size());
}
}