-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
664 lines (471 loc) · 18.1 KB
/
Copy pathSource.cpp
File metadata and controls
664 lines (471 loc) · 18.1 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
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <fstream>
#include <chrono>
#include <queue>
#include <algorithm>
using namespace std;
// Content class
class Content {
private:
string format;
string title;
int startYear;
int endYear;
int runTimeMins;
vector<string> genres;
float rating;
public:
Content();
Content(string f, string t, int sY, int eY, int rTM);
void setGenres(string genreStream);
string getFormat();
string getTitle();
int getStartYear();
int getEndYear();
int getRunTimeMins();
vector<string> getGenres();
string RunTimeMins_to_Hours_and_Mins();
void outputDetails();
float getRating();
void setRating(float f);
};
// default constructor
Content::Content() {
format = "";
title = "";
startYear = 0;
endYear = 0;
runTimeMins = 0;
//genres = {};
}
// constructor that makes Content objects from the stream
Content::Content(string f, string t, int sY, int eY, int rTM) {
format = f;
title = t;
startYear = sY;
endYear = eY;
runTimeMins = rTM;
//genres = {};
rating = 0.0f;
}
// sets the genres of the piece of content
// accepts a stream of genres in csv form from the tsv file
// divides them and then adds them to the genre vector
// genres are strings
void Content::setGenres(string genreStream) {
int index = genreStream.find_first_of(',');
while (index != -1) {
genres.push_back(genreStream.substr(0, index));
genreStream = genreStream.substr(index + 1);
index = genreStream.find_first_of(',');
}
genres.push_back(genreStream);
}
// getter methods
string Content::getFormat() {
return format;
}
string Content::getTitle() {
return title;
}
int Content::getStartYear() {
return startYear;
}
int Content::getEndYear() {
return endYear;
}
int Content::getRunTimeMins() {
return runTimeMins;
}
vector<string> Content::getGenres() {
return genres;
}
// converts the minute runTime of a piece of content to a more conventional
// hour + minute form
// includes edge cases
// returns a string so it can be outpuuted in the Content output function
string Content::RunTimeMins_to_Hours_and_Mins() {
string result = "";
if (runTimeMins < 60) {
result += to_string(runTimeMins) + " minute";
if (runTimeMins > 1 || runTimeMins == 0) {
result += "s";
}
return result;
}
int mins = runTimeMins % 60;
int hours = runTimeMins / 60;
if (mins == 0) {
result += to_string(hours) + " hour";
if (hours > 1 || hours == 0) {
result += "s";
}
return result;
}
result += to_string(hours) + " hour";
if (hours > 1 || hours == 0) {
result += "s";
}
result += " and " + to_string(mins) + " minute";
if (mins > 1 || mins ==0) {
result += "s";
}
return result;
}
// outputs the details of apiece of Content
// will probably be called to output the piece of Content
// that matches the user's qualifications
void Content::outputDetails() {
cout << "\"" << title << "\" Information:" << endl;
cout << "Format: " << format << endl;
cout << "Release Year: " << startYear << endl;
if (format == "tvSeries" && endYear != 0) {
cout << "End Year:" << endYear << endl;
}
cout << "Run Time: " << RunTimeMins_to_Hours_and_Mins() << endl;
cout << "Genres: ";
for (int i = 0; i < genres.size(); i++) {
cout << genres[i];
if (i != genres.size() - 1) {
cout << ",";
}
}
if(rating == 0.0f){
cout << "\nRating: None" << endl;
}
else{
cout << "\nRating: " << rating << "\n"<< endl;
}
}
float Content::getRating() {
return rating;
}
void Content::setRating(float f) {
rating = f;
}
// reads in data from a tsv file and constructs/returns a map
// containing each piece of Content, with a unique identifier as the key
unordered_map<string, Content> ReadTitleBasics(string filename) {
cout << "reading basics file" << endl;
string line = "";
unordered_map<string, Content> allContents;
fstream myfile(filename);
// open file
if (myfile.is_open()) {
getline(myfile, line); // read past first line
// temporary vector to store each tab separated value as an element
vector<string> vals;
// for each line
while (getline(myfile, line)) {
// read in each tab-sparated value into vals
int index = line.find_first_of("\t");
while (index != -1) {
// if the value is null, store it as 0
if (line.substr(0, index) == "\\N") {
vals.push_back("0");
}
else {
vals.push_back(line.substr(0, index));
}
line = line.substr(index + 1);
index = line.find_first_of("\t");
}
vals.push_back(line);
/* Note ! Form of the vector is this:
vals[0] == unique key
vals[1] == format
vals[2] == primaryTitle
vals[3] == originalTitle
vals[4] == isAdult
vals[5] == startYear
vals[6] == endYear
vals[7] == runtimeMinutes
vals[8] == csv stream of genres
*/
// only make Content object if the content is not adult
if (vals[4] == "0") {
Content obj = Content(vals[1], vals[2], stoi(vals[5]), stoi(vals[6]), stoi(vals[7]));
obj.setGenres(vals[8]);
allContents[vals[0]] = obj;
}
vals.clear();
}
}
else {
cout << "Title Basics File is Not Opening" << endl;
}
return allContents;
}
// opens the title_ratings file and assigns the respective ratings to the Content objects in the map
// with the correct identifier
void setRatings(string filename, unordered_map<string, Content>& allContents) {
cout << "reading ratings file" << endl;
string line = "";
int index = 0;
string key = "";
float rating = 0.0f;
fstream myfile(filename);
if (myfile.is_open()) {
getline(myfile, line);
while (getline(myfile, line)) {
index = line.find_first_of("\t");
key = line.substr(0, index);
line = line.substr(index + 1);
index = line.find_first_of("\t");
rating = stof(line.substr(0, index));
if (allContents.count(key) != 0) {
allContents[key].setRating(rating);
}
// else {
// cout << "Cannot find " << key << " in the Contents Map" << endl;
// }
}
}
else {
cout << "Ratings File is not open" << endl;
}
}
// Filtering
// first input is unordered map containing <key, Content list of information on the movie at this key>
// second input is a Content list of user inputs
// returns a list of keys that match the search requests
vector<pair<string, float> > mapFilter(unordered_map<string, Content>& allContent, Content& userInputs) {
int temp = 0;
Content currList;
vector<pair<string, float> > correctKeys;
vector<string> currGenre, currInput = userInputs.getGenres();
for (auto& i : allContent) { // i loops through every key of allContent
currList = i.second; // looking at Content at this key
currGenre = currList.getGenres(); // geting the vector of Genres for this key
// checking if the movie matches user inputs or is null
if ((currList.getFormat() == userInputs.getFormat() || userInputs.getFormat() == "None") && (currList.getTitle() == userInputs.getTitle() || userInputs.getTitle() == "None") && (currList.getStartYear() == userInputs.getStartYear() || userInputs.getStartYear() == 0) && (currList.getEndYear() == userInputs.getEndYear() || userInputs.getEndYear() == 0) && (currList.getRunTimeMins() == userInputs.getRunTimeMins() || userInputs.getRunTimeMins() == 0)) {
if (currInput[0] == "None") { // null
correctKeys.push_back(make_pair(i.first, i.second.getRating())); // inserting key into the output list
}
else {
for (int j = 0; j < currGenre.size(); j++) { // looping through genres of this movie
temp = 0;
for (int k = 0; k < currInput.size(); k++) { // looping through genres of the search request
temp = k;
if (currGenre[j] == currInput[k]) { // breaking if one matches
correctKeys.push_back(make_pair(i.first, i.second.getRating())); // inserting key into the output list
break;
}
}
if (currGenre[j] == currInput[temp]) { // breaking if one matches
break;
}
}
}
}
}
if (correctKeys.empty()) { // case no search results found
cout << "\nNo results found\nSearch Again" << endl;
}
return correctKeys; // return list of keys that match the search results
}
// Sorting
// Merge sort
vector<pair<string, float> > mergeSort(vector <pair<string, float> >& content, int start, int end) {
// Make and populate new vector endSort from subsection of given vector
vector <pair<string, float> > endSort;
for (int i = start; i < end; i++) {
endSort.push_back(content[i]);
}
// Find current split point of original vector
int midpoint = (end - start) / 2 + start;
// If provided list is too small to be halved, return endSort
if (midpoint == 0 || endSort.size() == 1) {
return endSort;
}
// Recursively halve list
vector <pair<string, float> > firstHalf = mergeSort(content, start, midpoint);
vector <pair<string, float> > secondHalf = mergeSort(content, midpoint, end);
//Compare halves' contents and merge
int index = 0;
int firstIndex = 0;
int secondIndex = 0;
while (firstIndex < (int)firstHalf.size() && secondIndex < (int)secondHalf.size()) {
if (firstHalf[firstIndex].second > secondHalf[secondIndex].second) {
endSort[index++] = firstHalf[firstIndex++];
}
else {
endSort[index++] = secondHalf[secondIndex++];
}
}
while (firstIndex < (int)firstHalf.size()) {
endSort[index++] = firstHalf[firstIndex++];
}
while (secondIndex < (int)secondHalf.size()) {
endSort[index++] = secondHalf[secondIndex++];
}
return endSort;
}
// K Largest
// Min Rating sets the logic for a priority queue of content titles and ratings
struct minRating {
constexpr bool operator()(
pair<string, float> const& a,
pair<string, float> const& b)
const noexcept {
return a.second > b.second;
}
};
vector<pair<string, float> > kSort(vector <pair<string, float> >& content, int k) {
// Make a min priority queue to hold all given ints
priority_queue<pair<string, float>, vector<pair<string, float> >, minRating> sorter;
vector<pair<string, float> > endSort;
// Iterate through the given vector, filling the priority queue up to k elements
for (int i = 0; i < k; i++) {
sorter.push(make_pair(content[i].first, content[i].second));
}
// For the remaining elements, if an int is larger than the top of the queue,
// pop the smallest element from the priority queue and add that int
for (int i = k; i < content.size(); i++) {
if (sorter.top().second < content[i].second) {
sorter.pop();
sorter.push(make_pair(content[i].first, content[i].second));
}
}
// Add priority queue elements to a vector and return it
for (int i = 0; i < k; i++) {
endSort.push_back(make_pair(sorter.top().first, sorter.top().second));
sorter.pop();
}
reverse(endSort.begin(), endSort.end());
return endSort;
}
void userInputs(string title_basics, string title_ratings){
string sortType, format, title, strStartYear, strEndYear, strRuntime, strGenre;
int startYear = 0, endYear = 0, runtime = 0, isValid = 0;
vector<string> genres;
vector<pair<string, float> > correctKeys;
// opening file
unordered_map<string, Content> allContent = ReadTitleBasics(title_basics);
setRatings(title_ratings, allContent);
// asking user for inputs
cout << "The MediaMiddleMan\n--------------------------------------------------------------------\nWould You like to use Merge Sort or K-Largest Sort? (Type as Shown): ";
getline(cin, sortType);
if (sortType != "Merge" && sortType != "K-Largest") { // which sort the user will use
while (true) {
if (sortType == "Merge" || sortType == "K-Largest") {
break;
}
cout << "Incorrect Sort Please Type as Shown: ";
getline(cin, sortType);
}
}
cout << "\nSelect your preferences or type None if you do not care about the type of filter" << endl;
while (correctKeys.empty()) { // getting search inputs
cout << "Format: ";
getline(cin, format);
cout << "Title: ";
getline(cin, title);
cout << "Release Year: ";
getline(cin, strStartYear);
while (true) {
if (strStartYear == "None") { // switching to int
startYear == 0;
break;
}
else {
try {
startYear = stoi(strStartYear);
break;
}
catch (invalid_argument& e) { // case not an int
cout << "Invalid: Please Input a Number or None\nRelease Year: ";
cin >> strStartYear;
}
}
}
cout << "End Year of Series: ";
cin >> strEndYear;
while (true) {
if (strEndYear == "None") { // switching to int
endYear == 0;
break;
}
else {
try {
endYear = stoi(strEndYear);
break;
}
catch (invalid_argument& e) { // case not an int
cout << "Invalid: Please Input a Number or None\nEnd Year of Series: ";
cin >> strEndYear;
}
}
}
cout << "Run Time in Minutes or Number of Seasons: ";
cin >> strRuntime;
while (true) {
if (strRuntime == "None") { // switching to int
runtime == 0;
break;
}
else {
try {
runtime = stoi(strRuntime);
break;
}
catch (invalid_argument& e) { // case not an int
cout << "Invalid: Please Input a Number or None\nRun Time in Minutes or Number of Seasons: ";
cin >> strRuntime;
}
}
}
cout << "Genre (Comedy, Drama, War, Action, Adventure, Documentary, etc...[enter with commas inbetween]): ";
cin >> strGenre;
// setting user inputs in a Content List
Content userInputs;
userInputs = Content(format, title, startYear, endYear, runtime);
userInputs.setGenres(strGenre);
// filtering keys based on search inputs
correctKeys = mapFilter(allContent, userInputs);
}
// sorting
vector<pair<string, float> > sortedKeys;
if (sortType == "Merge") { // user wants merge sort
// (Chrono clock objects and manipulation sourced from GeeksforGeeks
// https://www.geeksforgeeks.org/measure-execution-time-function-cpp/)
// Get start and end timepoints and then subtract them to find duration of functions
auto start = std::chrono::high_resolution_clock::now();
sortedKeys = mergeSort(correctKeys, 0, (int)correctKeys.size());
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
cout << "\nDuration of Merge sort algorithm: " << duration.count() << " microseconds" << endl;
}
else { // user wants k-largest sort
auto start = std::chrono::high_resolution_clock::now();
sortedKeys = kSort(correctKeys, 5);
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
cout << "\nDuration of K-Largest sort algorithm: " << duration.count() << " microseconds" << endl;
}
// Outputing Results
cout << "\nTop Search Results" << endl;
if (sortedKeys.size() >= 5) { // Top Five Results
for (int i = 0; i < 5; i++) {
allContent[sortedKeys[i].first].outputDetails();
}
}
else { // case we do not have 5 results that match the search
for (int i = 0; i < sortedKeys.size(); i++) {
allContent[sortedKeys[i].first].outputDetails();
cout << "\n";
}
}
}
int main() { // user inputs and method calling
string title_basics = "";
string title_ratings = "";
//Tarik Filenames
title_basics = "title_basics.tsv";
title_ratings = "title_ratings.tsv";
//Cecilia filenames
userInputs(title_basics,title_ratings);
}