-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstRatings.java
More file actions
201 lines (177 loc) · 6.18 KB
/
FirstRatings.java
File metadata and controls
201 lines (177 loc) · 6.18 KB
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
import edu.duke.*;
import java.util.*;
import org.apache.commons.csv.*;
/**
* create a new class named FirstRatings to process the movie and ratings data and to answer questions about them.
*
*
* @author (your name)
* @version (a version number or a date)
*/
public class FirstRatings {
//Write a method named loadMovies that has one parameter, a String named filename. This method should process every record from the CSV file whose name is filename, a file of movie information, and return an ArrayList of type Movie with all of the movie data from the file.
public ArrayList<Movie> loadMovies(String fileName)
{
ArrayList<Movie> movieList = new ArrayList<Movie>();
FileResource fr = new FileResource("data/"+fileName);
CSVParser parse = fr.getCSVParser();
for(CSVRecord rc : parse)
{
Movie m = new Movie (rc.get("id"), rc.get("title"), rc.get("year"), rc.get("genre"), rc.get("director"),
rc.get("country"), rc.get("poster"), Integer.parseInt(rc.get("minutes")));
movieList.add(m);
}
return movieList;
}
public void testLoadMovies()
{
ArrayList<Movie> movies = loadMovies("ratedmovies_short.csv");
//ArrayList<Movie> movies = loadMovies("ratedmoviesfull.csv");
System.out.println("No. of Movies are: "+movies.size());
/*
for(Movie m : movies)
{
System.out.println(m);
}*/
ArrayList<Movie> cMovies = new ArrayList<Movie>();
for(Movie m : movies)
{
if(m.getGenres().contains("Comedy"))
{
cMovies.add(m);
}
}
System.out.println("No. of Comedy Movies are: "+cMovies.size());
ArrayList<Movie> bigMovies = new ArrayList<Movie>();
for(Movie m : movies)
{
if(m.getMinutes() > 150)
{
bigMovies.add(m);
}
}
System.out.println("No. of Movies > 150 minutes are: "+bigMovies.size());
HashMap<String, Integer> directorMovies = new HashMap<String, Integer>();
for(Movie m : movies)
{
String[] directors = m.getDirector().split(",");
for(String d : directors)
{
if(!directorMovies.containsKey(d))
{
directorMovies.put(d,1);
}
else
{
int num = directorMovies.get(d);
directorMovies.put(d,num+1);
}
}
}
int maxMovies = 0;
for(String d : directorMovies.keySet())
{
if(directorMovies.get(d) > maxMovies)
maxMovies = directorMovies.get(d);
}
System.out.println("Max movies by any director is: "+maxMovies);
System.out.println("Name of those Directors are: ");
for(String d : directorMovies.keySet())
{
if(directorMovies.get(d) == maxMovies)
{
System.out.println(d);
}
}
}
public ArrayList<Rater> loadRaters(String fileName)
{
ArrayList<Rater> raterList = new ArrayList<Rater>();
HashMap<String, ArrayList<Rating>> myMap = new HashMap<String, ArrayList<Rating>>();
FileResource fr = new FileResource("data/"+fileName);
CSVParser parse = fr.getCSVParser();
for(CSVRecord rc : parse)
{
String raterId = rc.get("rater_id");
String movieId = rc.get("movie_id");
double rating = Double.parseDouble(rc.get("rating"));
if(!myMap.containsKey(raterId))
{
Rating r = new Rating(movieId, rating);
ArrayList<Rating> rateList = new ArrayList<Rating>();
rateList.add(r);
myMap.put(raterId, rateList);
}
else
{
myMap.get(raterId).add(new Rating(movieId, rating));
}
}
//System.out.println(myMap);
for(String rID: myMap.keySet())
{
Rater r = new EfficientRater(rID);
for(Rating rat : myMap.get(rID))
{
r.addRating(rat.getItem(), rat.getValue());
}
raterList.add(r);
}
return raterList;
}
public void testLoadRaters()
{
ArrayList<Rater> raters = loadRaters("ratings_short.csv");
System.out.println("Total no. of raters are: "+raters.size());
/*
for(Rater r : raters)
{
System.out.println("Rater id: "+r.getID()+" No. of ratings: "+r.numRatings());
ArrayList<String> movieIds = r.getItemsRated();
for(String m : movieIds)
{
System.out.println(m+"\t"+r.getRating(m));
}
}*/
String raterId = "2";
for(Rater r : raters)
{
if(r.getID().equals(raterId))
{
System.out.println("number of ratings for rater:"+raterId+" is "+r.getItemsRated().size());
break;
}
}
int maxRating = 0;
for(Rater r : raters)
{
if(r.getItemsRated().size() > maxRating)
{
maxRating = r.getItemsRated().size();
}
}
System.out.println("maximum number of ratings by any rater: "+maxRating);
String movieId = "1798709";
int count = 0;
for(Rater r : raters)
{
for(String movie: r.getItemsRated())
{
if(movie.equals(movieId))
{
count++;
}
}
}
System.out.println("number of ratings "+movieId+" movie has "+count);
HashSet<String> movies = new HashSet<String>();
for(Rater r : raters)
{
for(String movie: r.getItemsRated())
{
movies.add(movie);
}
}
System.out.println("Total no. of movies rated "+movies.size());
}
}