-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDataLoader.cpp
More file actions
90 lines (73 loc) · 2.12 KB
/
Copy pathDataLoader.cpp
File metadata and controls
90 lines (73 loc) · 2.12 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
#include "DataLoader.h"
#include <fstream>
#include <sstream>
#include <iostream>
vector<string> parseLine(const string& line) {
vector<string> fields;
string current;
bool insideQuotes = false;
for (size_t i = 0; i < line.size(); i++) {
char ch = line[i];
// Handles commas inside of quoted fields. Example: "Action,Adventure"
if (ch == '"') {
if (insideQuotes && i + 1 < line.size() && line[i + 1] == '"') {
current += '"';
i++;
} else {
insideQuotes = !insideQuotes;
}
} else if (ch == ',' && !insideQuotes) {
fields.push_back(current);
current.clear();
} else {
current += ch;
}
}
fields.push_back(current);
return fields;
}
vector<string> splitGenres(const string& genreString) {
vector<string> genres;
stringstream ss(genreString);
string genre;
while (getline(ss, genre, ',')) {
genres.push_back(genre);
}
return genres;
}
vector<Movie> loadMoviesFromCSV(const string& filename) {
vector<Movie> movies;
ifstream file(filename);
if (!file.is_open()) {
cout << "Could not open file: " << filename << endl;
return movies;
}
string line;
getline(file, line);
while (getline(file, line)) {
vector<string> fields = parseLine(line);
if (fields.size() < 9) {
continue;
}
string titleType = fields[2];
string genres = fields[6];
if (titleType != "movie" || genres == "\\N") {
continue;
}
try {
Movie movie;
movie.tconst = fields[1];
movie.title = fields[3];
movie.year = stoi(fields[5]);
movie.genres = splitGenres(genres);
movie.rating = stod(fields[7]);
movie.numVotes = stoi(fields[8]);
movies.push_back(movie);
}
catch (...) {
// If one row has missing or weird data, skip it instead of ending the program.
continue;
}
}
return movies;
}