-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirstRatings.java
215 lines (167 loc) · 7.36 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
/**
* Write a description of class FirstRatings here.
*
* @author (your name)
* @version (a version number or a date)
*/
import edu.duke.*;
import java.util.*;
import org.apache.commons.csv.*;
public class FirstRatings
{
public ArrayList<Movie> loadMovies(String fileName){
ArrayList<Movie> movieList = new ArrayList<Movie>();
FileResource fr = new FileResource("./data/" + fileName);
CSVParser movieData = fr.getCSVParser();
for(CSVRecord movieEntry: movieData){
Movie movie = new Movie(
movieEntry.get("id"),
movieEntry.get("title"),
movieEntry.get("year"),
movieEntry.get("genre"),
movieEntry.get("director"),
movieEntry.get("country"),
movieEntry.get("poster"),
Integer.parseInt(movieEntry.get("minutes"))
);
movieList.add(movie);
}
return movieList;
}
public ArrayList<EfficientRater> loadRaters(String fileName){
ArrayList<EfficientRater> movieRaterList = new ArrayList<EfficientRater>();
FileResource fr = new FileResource("./data/" + fileName);
CSVParser raterData = fr.getCSVParser();
for (CSVRecord raterEntry : raterData) {
if(movieRaterList.size() == 0) {
EfficientRater rater = new EfficientRater(raterEntry.get("rater_id"));
rater.addRating(raterEntry.get("movie_id"),
Double.parseDouble(raterEntry.get("rating")));
movieRaterList.add(rater);
}
else {
String id = raterEntry.get("rater_id");
boolean found = false;
for (int i = 0; i < movieRaterList.size(); i++) {
EfficientRater rater = movieRaterList.get(i);
if (rater.getID().equals(id)){
rater.addRating(raterEntry.get("movie_id"),
Double.parseDouble(raterEntry.get("rating")));
found = true;
}
}
if (!found) {
EfficientRater rater = new EfficientRater(raterEntry.get("rater_id"));
rater.addRating(raterEntry.get("movie_id"),
Double.parseDouble(raterEntry.get("rating")));
movieRaterList.add(rater);
}
}
}
return movieRaterList;
}
public void testLoadMovies(){
int comedyCount = 0;
int longMovies = 0;
int moviePerDirector = 0;
// ArrayList<Movie> movieDataFull = loadMovies("ratedmoviesfull.csv");
ArrayList<Movie> movieDataFull = loadMovies("ratedmovies_short2.csv");
HashMap<String, Integer> directorsMovies = new HashMap<String, Integer>();
for (Movie movie : movieDataFull){
String[] directors = movie.getDirector().split(",");
if(directors.length == 1){
moviePerDirector ++;
}
if(movie.getGenres().indexOf("Comedy") != -1) {
comedyCount ++;
}
if(movie.getMinutes() > 150){
longMovies ++;
}
// Inserting movies into a Hashmap
for(int i = 0; i < directors.length; i++){
String director = directors[i].trim();
if (directorsMovies.containsKey(director)) {
directorsMovies.put(director, directorsMovies.get(director) + 1);
}
else {
directorsMovies.put(director, 1);
}
}
}
int maxValue = 0;
int oneMovie = 0;
for (String director : directorsMovies.keySet()){
int currValue = directorsMovies.get(director);
if(currValue == 1){
oneMovie ++;
}
if (currValue > maxValue){
maxValue = currValue;
}
// System.out.println("director: " + director +
// ", movies directed: " + directorsMovies.get(director) + "\n");
}
for (String director : directorsMovies.keySet()){
if(directorsMovies.get(director) == maxValue){
System.out.println("Director with higher number of movies: " + director);
}
}
System.out.println(movieDataFull.size());
System.out.println("Number of movies include the Comedy genre: " + comedyCount);
System.out.println("Number of movies longer than 150 min: " + longMovies);
System.out.println("Maxinum Number of films directed by one director: " + moviePerDirector);
}
public void testLoadRaters() {
// ArrayList<Rater> raterData = loadRaters("ratings_short.csv");
ArrayList<EfficientRater> raterData = loadRaters("ratings.csv");
//ArrayList<Rater> raterData = loadRaters("ratedmoviesfull.csv");
int maxRatings = 0;
//String id = "2";
String id = "193";
int ratingsPerMovie = 0;
String movieId = "1798709";
ArrayList<String> totalMovies = new ArrayList<String>();
/*
for (Rater r : raterData){
ArrayList<String> itemsList = r.getItemsRated();
System.out.println("Id: " + r.getID() + " number of ratings: " + r.numRatings());
for (String item : itemsList){
System.out.println("List of rated items: " + item + ": " + r.getRating(item));
}
System.out.println("\n");
}
*/
System.out.println("Total raters: " + raterData.size());
for (EfficientRater r : raterData){
if(r.getID().equals(id)) {
System.out.println("Number of ratings for user " + id + ": " + r.numRatings());
}
int currRatings = r.numRatings();
if(currRatings > maxRatings){
maxRatings = currRatings;
}
if(r.numRatings() == maxRatings) {
System.out.println("User with maximum ratings: " + r.getID());
}
ArrayList<String> itemsList = r.getItemsRated();
for (String item : itemsList){
if(item.equals(movieId)) {
ratingsPerMovie ++;
}
}
ArrayList<String> itemsList2 = r.getItemsRated();
for (String item : itemsList2){
if(!totalMovies.contains(item)) {
totalMovies.add(item);
}
}
}
System.out.println("Maximum number of ratings: " + maxRatings);
System.out.println("\n");
System.out.println("Total of ratings for movieID: " + movieId + " :" + ratingsPerMovie);
System.out.println("\n");
System.out.println("Total movies rated by all raters: " + totalMovies.size());
System.out.println("\n");
}
}