-
Notifications
You must be signed in to change notification settings - Fork 0
/
Movie_DataBase.cpp
525 lines (459 loc) · 19 KB
/
Movie_DataBase.cpp
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <chrono>
#include <unordered_set>
#include "Movie.h"
using namespace std;
//prints out movies and associated information in tabular (rowwise) format
void printMovies(const vector<Movie>& movies, int number_of_rows);
//merge for sorting by average ratings
void mergeRatings(vector<Movie>& movies, int l, int m, int r, bool ascending);
//merge for sorting by number of votes
void mergeVotes(vector<Movie>& movies, int l, int m, int r, bool ascending);
//merge sort for either
void mergeSort(vector<Movie>& movies, int l, int r, bool ascending, int sorting_parameter);
//partition for sorting by average ratings
int partitionRatings(vector<Movie>& movies, int low, int high, bool ascending);
//partion for sorting by number of votes
int partitionVotes(vector<Movie>& movies, int low, int high, bool ascending);
//quicksort for both
void quickSort(vector<Movie>& movies, int low, int high, bool ascending, int sorting_parameter);
int main() {
//vector for storing all movie objects
vector<Movie> movies;
long totalRuntime = 0;
//data source
ifstream file("IMDB_Data.txt");
string line;
getline(file, line); // skip first line (header)
//read in all data, creating movie object for each row of data, filling in object variables
while (getline(file, line)) {
string tconst, titleType, primaryTitle, averageRatingStr, numVotesStr, startYearStr, runtimeMinutesStr, genre1, genre2, genre3;
float averageRating;
int numVotes, startYear, runtimeMinutes;
stringstream ss(line);
getline(ss, tconst, '\t');
getline(ss, titleType, '\t');
getline(ss, primaryTitle, '\t');
getline(ss, averageRatingStr, '\t');
if (averageRatingStr == "NA") averageRating = -1;
else averageRating = stof(averageRatingStr);
getline(ss, numVotesStr, '\t');
if (numVotesStr == "NA") numVotes = -1;
else numVotes = stoi(numVotesStr);
getline(ss, startYearStr, '\t');
if (startYearStr == "NA") startYear = -1;
else startYear = stoi(startYearStr);
getline(ss, runtimeMinutesStr, '\t');
if (runtimeMinutesStr == "NA") runtimeMinutes = -1;
else runtimeMinutes = stoi(runtimeMinutesStr);
getline(ss, genre1, '\t');
getline(ss, genre2, '\t');
getline(ss, genre3, '\t');
//adding movie to vector of movies
movies.push_back(Movie(tconst, titleType, primaryTitle, averageRating, numVotes, startYear, runtimeMinutes, genre1, genre2, genre3));
}
file.close();
//User Menu / Console Interface
int main_menu_selection = -1;
vector<Movie> moviesFiltered;
vector<Movie> moviesFilteredQuick;
bool filter_chosen = false;
int year_lower_bound = -1;
int year_upper_bound = -1;
unordered_set<string> genres;
unordered_set<string> title_types;
int runtime_lower_bound = -1;
int runtime_upper_bound = -1;
int votes_lower_bound = -1;
int votes_upper_bound = -1;
float rating_lower_bound = -1;
float rating_upper_bound = -1;
while (main_menu_selection != 0) {
cout << "\n\nSelect Movie Filter:\n1. Year\n2. Genre\n3. Title Type\n4. Runtime\n5. Number of Votes\n6. Rating\n0. No further filters\n" << endl;
cin >> main_menu_selection;
//stores whether any filters were ever applied
if (main_menu_selection != 0) {
filter_chosen = true;
}
//Year filter
if (main_menu_selection == 1) {
cout << "Enter Year Lower Bound (enter -1 for no lower bound)" << endl;
cin >> year_lower_bound;
cout << "Enter Year Upper Bound (enter -1 for no upper bound)" << endl;
cin >> year_upper_bound;
}
//Genre filter
else if (main_menu_selection == 2) {
cout << "Enter a comma separated list of genres to include:\nOptions include Comedy, Drama, Romance, Western, Biography, Adventure, Music, Mystery, Family, Animation, Film-Noir, Crime, War, Sci-Fi, Horror, Thriller, Musical\n" << endl;
string genre_input = "";
cin >> genre_input;
std::stringstream ss(genre_input);
std::string genre;
//converting string input into set entries
while (std::getline(ss, genre, ',')) {
genres.insert(genre);
}
genre_input = "";
}
//title type filter
else if (main_menu_selection == 3) {
cout << "Enter a comma separated list of title types to include:\nOptions include movie, short, tvSeries\n" << endl;
string title_type_input = "";
cin >> title_type_input;
std::stringstream ss(title_type_input);
std::string title_type;
//inserting into set
while (std::getline(ss, title_type, ',')) {
title_types.insert(title_type);
}
title_type_input = "";
}
//runtime filter
else if (main_menu_selection == 4) {
cout << "Enter runtime lower bound in minutes: (enter -1 for no lower bound)\n" << endl;
cin >> runtime_lower_bound;
cout << "Enter Year Upper Bound (enter -1 for no upper bound)" << endl;
cin >> runtime_upper_bound;
}
//number of votes filter
else if (main_menu_selection == 5) {
cout << "Enter number of votes lower bound: (enter -1 for no lower bound)\n" << endl;
cin >> votes_lower_bound;
cout << "Enter number of votes upper bound: (enter -1 for no upper bound)\n" << endl;
cin >> votes_upper_bound;
}
//rating filter
else if (main_menu_selection == 6) {
cout << "Enter rating (0.0 - 10.0) lower bound: (enter -1 for no lower bound)\n" << endl;
cin >> rating_lower_bound;
cout << "Enter rating (0.0 - 10.0) upper bound: (enter -1 for no upper bound)\n" << endl;
cin >> rating_upper_bound;
}
}
if (filter_chosen) {
int length_of_movies = movies.size();
//applying filters to movies
for (int i = length_of_movies-1; i >= 0; i--) {
//Year constraints
if (year_upper_bound!=-1 && year_lower_bound != -1) {
if (movies[i].startYear > year_upper_bound || movies[i].startYear < year_lower_bound) {
movies.erase(movies.begin() + i);
continue;
}
}
else if(year_upper_bound != -1) {
if (movies[i].startYear > year_upper_bound) {
movies.erase(movies.begin() + i);
continue;
}
}
else if (year_lower_bound != -1) {
if (movies[i].startYear < year_lower_bound) {
movies.erase(movies.begin() + i);
continue;
}
}
//genre constraints
if (!genres.empty()) {
if (genres.count(movies[i].genre1)==0 && genres.count(movies[i].genre2)==0 && genres.count(movies[i].genre3)==0) {
movies.erase(movies.begin() + i);
continue;
}
}
//title_types constraints
if (!title_types.empty()) {
if (title_types.count(movies[i].titleType)==0) {
movies.erase(movies.begin() + i);
continue;
}
}
//runtime constraints
if (runtime_upper_bound!=-1 and runtime_lower_bound != -1) {
if (movies[i].runtimeMinutes > runtime_upper_bound || movies[i].runtimeMinutes < runtime_lower_bound) {
movies.erase(movies.begin() + i);
continue;
}
}
else if(runtime_upper_bound != -1) {
if (movies[i].runtimeMinutes > runtime_upper_bound) {
movies.erase(movies.begin() + i);
continue;
}
}
else if (runtime_lower_bound != -1) {
if (movies[i].runtimeMinutes < runtime_lower_bound) {
movies.erase(movies.begin() + i);
continue;
}
}
//votes constraints
if (votes_upper_bound!=-1 && votes_lower_bound != -1) {
if (movies[i].numVotes > votes_upper_bound || movies[i].numVotes < votes_lower_bound) {
movies.erase(movies.begin() + i);
continue;
}
}
else if(votes_upper_bound != -1) {
if (movies[i].numVotes > votes_upper_bound) {
movies.erase(movies.begin() + i);
continue;
}
}
else if (votes_lower_bound != -1) {
if (movies[i].numVotes < votes_lower_bound) {
movies.erase(movies.begin() + i);
continue;
}
}
//rating constraints
if (rating_upper_bound!=-1 && rating_lower_bound != -1) {
if (movies[i].averageRating > rating_upper_bound || movies[i].averageRating < rating_lower_bound) {
movies.erase(movies.begin() + i);
continue;
}
}
else if(rating_upper_bound != -1) {
if (movies[i].averageRating > rating_upper_bound) {
movies.erase(movies.begin() + i);
continue;
}
}
else if (rating_lower_bound != -1) {
if (movies[i].averageRating < rating_lower_bound) {
movies.erase(movies.begin() + i);
continue;
}
}
}
}
moviesFiltered = movies;
//set the vector used for quicksort the same as the one used for mergesort
moviesFilteredQuick = moviesFiltered;
// "Ascending/decending/limit selections"
int ordering_choice = 1;
int limit_choice = 1;
int number_of_rows = -1;
int sorting_parameter = 1;
cout << "Sort by (1) average rating or (2) number of votes?" << endl;
cin >> sorting_parameter;
cout << "Order (1) ascending or (2) descending?" << endl;
cin >> ordering_choice;
cout << "Limit Number Rows of Output?\n1. Show all\n2. Specify number" << endl;
cin >> limit_choice;
if (limit_choice == 2) {
cout << "Enter maximum rows of data displayed" << endl;
cin >> number_of_rows;
}
//merge sort timer start
auto merge_start_time = chrono::high_resolution_clock::now();
//the boolean denotes whether sorting is ascending or descending order, which is reflected in the merge function
if (ordering_choice ==1) {
mergeSort(moviesFiltered, 0, moviesFiltered.size() - 1, true, sorting_parameter);
}
else {
mergeSort(moviesFiltered, 0, moviesFiltered.size() - 1, false, sorting_parameter);
}
auto merge_end_time = chrono::high_resolution_clock::now();
//quick sort timer start
auto quick_start_time = chrono::high_resolution_clock::now();
//the boolean denotes whether sorting is ascending or descending order, which is reflected in the partition function
if (ordering_choice ==1) {
quickSort(moviesFilteredQuick, 0, moviesFilteredQuick.size() - 1, true, sorting_parameter);
}
else {
quickSort(moviesFilteredQuick, 0, moviesFilteredQuick.size() - 1, false, sorting_parameter);
}
//quicksort end timer
auto quick_end_time = chrono::high_resolution_clock::now();
// calculate duration for each
auto merge_duration = chrono::duration_cast<chrono::microseconds>(merge_end_time - merge_start_time);
auto quick_duration = chrono::duration_cast<chrono::microseconds>(quick_end_time - quick_start_time);
// print movies using mergesort sorted vector
printMovies(moviesFiltered, number_of_rows);
cout << endl;
//print movies using quicksort sorted vector (varies slightly for equal values due to different implementations)
//printMovies(moviesFilteredQuick, number_of_rows);
cout << endl;
//output time it took for each sorting algorithm to process the data desired by user
cout << "Time taken by merge sort: " << merge_duration.count() << " microseconds." << endl;
cout << "Time taken by quick sort: " << quick_duration.count() << " microseconds." << endl;
//displays which sorting algorithm was faster and by how much in terms of percent change
if (merge_duration.count() > quick_duration.count()) {
cout << "Quick Sort was faster than Merge Sort by " << (abs(quick_duration.count() - merge_duration.count()))/(float)(quick_duration.count())*100 << "%" << endl;
}
if (merge_duration.count() < quick_duration.count()) {
cout << "Merge Sort was faster than Quick Sort by " << (abs(merge_duration.count() - quick_duration.count()))/(float)(merge_duration.count())*100 << "%" << endl;
}
cout << endl;
return 0;
}
void printMovies(const vector<Movie>& movies, int number_of_rows) {
std::cout << setw(12) << left << "Movie #"
<< setw(15) << left << "tconst"
<< setw(12) << left << "titleType"
<< setw(70) << left << "primaryTitle"
<< setw(15) << left << "averageRating"
<< setw(10) << left << "numVotes"
<< setw(10) << left << "startYear"
<< setw(15) << left << "runtimeMinutes"
<< setw(15) << left << "genre1"
<< setw(15) << left << "genre2"
<< setw(2) << left << "genre3"
<< endl;
if (number_of_rows == -1 || number_of_rows >= movies.size()) {
for (int i = 0; i < movies.size(); i++) {
std::cout << setw(12) << left << i + 1
<< setw(15) << left << movies[i].tconst
<< setw(12) << left << movies[i].titleType
<< setw(70) << left << movies[i].primaryTitle
<< setw(15) << left << movies[i].averageRating
<< setw(10) << left << movies[i].numVotes
<< setw(10) << left << movies[i].startYear
<< setw(15) << left << movies[i].runtimeMinutes
<< setw(15) << left << movies[i].genre1
<< setw(15) << left << movies[i].genre2
<< setw(2) << left << movies[i].genre3
<< endl;
}
}
else {
for (int i = 0; i < number_of_rows; i++) {
std::cout << setw(12) << left << i + 1
<< setw(15) << left << movies[i].tconst
<< setw(12) << left << movies[i].titleType
<< setw(70) << left << movies[i].primaryTitle
<< setw(15) << left << movies[i].averageRating
<< setw(10) << left << movies[i].numVotes
<< setw(10) << left << movies[i].startYear
<< setw(15) << left << movies[i].runtimeMinutes
<< setw(15) << left << movies[i].genre1
<< setw(15) << left << movies[i].genre2
<< setw(2) << left << movies[i].genre3
<< endl;
}
}
}
void mergeRatings(vector<Movie>& movies, int l, int m, int r, bool ascending) {
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
vector<Movie> L(n1);
vector<Movie> R(n2);
for (i = 0; i < n1; i++)
L[i] = movies[l + i];
for (j = 0; j < n2; j++)
R[j] = movies[m + 1 + j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if ((ascending && L[i].averageRating <= R[j].averageRating) || (!ascending && L[i].averageRating >= R[j].averageRating)) {
movies[k] = L[i];
i++;
}
else {
movies[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
movies[k] = L[i];
i++;
k++;
}
while (j < n2) {
movies[k] = R[j];
j++;
k++;
}
}
void mergeVotes(vector<Movie>& movies, int l, int m, int r, bool ascending) {
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
vector<Movie> L(n1);
vector<Movie> R(n2);
for (i = 0; i < n1; i++)
L[i] = movies[l + i];
for (j = 0; j < n2; j++)
R[j] = movies[m + 1 + j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if ((ascending && L[i].numVotes <= R[j].numVotes) || (!ascending && L[i].numVotes >= R[j].numVotes)) {
movies[k] = L[i];
i++;
}
else {
movies[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
movies[k] = L[i];
i++;
k++;
}
while (j < n2) {
movies[k] = R[j];
j++;
k++;
}
}
void mergeSort(vector<Movie>& movies, int l, int r, bool ascending, int sorting_parameter) {
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(movies, l, m, ascending, sorting_parameter);
mergeSort(movies, m + 1, r, ascending, sorting_parameter);
if (sorting_parameter == 1) {
mergeRatings(movies, l, m, r, ascending);
}
else {
mergeVotes(movies, l, m, r, ascending);
}
}
}
int partitionRatings(vector<Movie>& movies, int low, int high, bool ascending) {
float pivot = movies[high].averageRating;
int i = low - 1;
for (int j = low; j < high; j++) {
if ((ascending && movies[j].averageRating <= pivot) || (!ascending && movies[j].averageRating >= pivot)) {
i++;
swap(movies[i], movies[j]);
}
}
swap(movies[i + 1], movies[high]);
return i + 1;
}
int partitionVotes(vector<Movie>& movies, int low, int high, bool ascending) {
int pivot = movies[high].numVotes;
int i = low - 1;
for (int j = low; j < high; j++) {
if ((ascending && movies[j].numVotes <= pivot) || (!ascending && movies[j].numVotes >= pivot)) {
i++;
swap(movies[i], movies[j]);
}
}
swap(movies[i + 1], movies[high]);
return i + 1;
}
void quickSort(vector<Movie>& movies, int low, int high, bool ascending, int sorting_parameter) {
if (low < high) {
int pivot = 0;
if (sorting_parameter == 1) {
pivot = partitionRatings(movies, low, high, ascending);
}
else {
pivot = partitionVotes(movies, low, high, ascending);
}
quickSort(movies, low, pivot - 1, ascending, sorting_parameter);
quickSort(movies, pivot + 1, high, ascending, sorting_parameter);
}
}