-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.cpp
148 lines (123 loc) · 3.51 KB
/
convert.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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <list>
#include <sstream>
#include "../csvstream/csvstream.h"
#include <list>
#include <set>
class Machine
{
private:
std::vector<std::map<std::string, std::string>> flowchartNodeList[15];
std::string all_options[8];
std::set<std::string> dictionary;
std::set<std::string> prompt_list;
public:
Machine()
{
all_options[0] = "opt_1";
all_options[1] = "opt_2";
all_options[2] = "opt_3";
all_options[3] = "opt_4";
all_options[4] = "opt_5";
all_options[5] = "opt_6";
all_options[6] = "opt_7";
all_options[7] = "opt_8";
};
std::string formatEntry(std::string input_entry)
{
std::string::iterator it = input_entry.begin();
for (; it != input_entry.end(); ++it)
{
if (!isalpha(*it))
{
*it = '_';
}
else
{
*it = tolower(*it);
}
}
return input_entry;
}
void readFromSheets(std::string &animeSheetName)
{
csvstream animeCSV(animeSheetName);
// Rows have key = column name, value = cell datum
std::map<std::string, std::string> row;
while (animeCSV >> row)
{
flowchartNodeList[stoi(row["importance"])].push_back(row);
if (dictionary.find(row["subject_str"]) != dictionary.end())
{
std::cout << "Subject Repeated: " << row["subject_str"] << std::endl;
}
else
{
dictionary.insert(row["subject_str"]);
}
}
}
void outputToFile()
{
std::ofstream fout("../js/raw_data.js");
std::ostringstream ending;
ending << "var map = new Map();" << std::endl;
for (int imp = 14; imp >= 0; --imp)
{
std::vector<std::map<std::string, std::string>> specificSet = flowchartNodeList[imp];
for (auto &entry : specificSet)
{
std::ostringstream singleNodeStream;
singleNodeStream << "var " << formatEntry(entry["subject_str"]) << " = [" << std::endl;
singleNodeStream << " \"" << entry["subject_str"] << "\"," << std::endl;
if (entry["opt_1"] == "")
{
entry["q_prompt"] = "Go scroll down to learn more about " + entry["subject_str"];
}
else if (entry["q_prompt"] == "")
{
entry["q_prompt"] = "We chose " + entry["opt_1"] + " for you!";
}
singleNodeStream << " \"" << entry["q_prompt"] << "\"," << std::endl;
for (auto ¤t_opt : all_options)
{
if (entry[current_opt].empty())
break;
singleNodeStream << " " << formatEntry(entry[current_opt]) << "," << std::endl;
}
singleNodeStream << "]" << std::endl;
ending << "map.set(\"" << entry["q_prompt"] << "\", " << formatEntry(entry["subject_str"]) << ");" << std::endl;
ending << "map.set(\"" << entry["subject_str"] << "\", " << formatEntry(entry["subject_str"]) << ");" << std::endl;
if (prompt_list.find(entry["q_prompt"]) != prompt_list.end())
{
std::cout << "Prompt Repeated: " << entry["q_prompt"] << std::endl;
}
else
{
prompt_list.insert(entry["q_prompt"]);
}
fout << singleNodeStream.str();
}
}
ending << "export { map };" << std::endl;
fout << ending.str();
}
};
int main(int argc, char *argv[])
{
std::string animeSheetName = "anime_flowchart_data.csv";
if (argc == 2)
{
animeSheetName = argv[1];
}
Machine machine;
// This should store it into data files
machine.readFromSheets(animeSheetName);
// This should spit it out
machine.outputToFile();
std::cout << "Everything was succesfully updated!" << std::endl;
}