-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJeopardyData.cpp
More file actions
221 lines (208 loc) · 7.58 KB
/
JeopardyData.cpp
File metadata and controls
221 lines (208 loc) · 7.58 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
#include "JeopardyData.h"
void JeopardyData::ReadFile()
{
string filename = "master_season1-36.tsv";
ifstream file(filename);
if (file.fail())
{
cout << "Error: Failed to read file." << endl;
exit(EXIT_FAILURE);
}
string line;
float progress = 0.0f, progFactor = 0.000279662403f;
//progFactor from 100/357574 (num of lines)
//manual input simplifies time complexity; only iterating through the file once
//only saves 20 seconds but it's something (from ~160s to ~140s)
ShowConsoleCursor(false); //removes cursor so it doesn't spaz out while printing the progress
getline(file, line);
auto start = chrono::high_resolution_clock::now();
while (getline(file, line))
{
//Create JeopardyQ obj and a string of the its category
JeopardyQ jq = ParseLine(line);
string category = jq[3];
//Add JeopardyQ to maps
data[category].push_back(jq);
//Progress bar
cout << "Populating Ordered Map... " << (int)progress << "%\r";
cout.flush();
progress += progFactor;
}
auto stop = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::seconds>(stop - start);
cout << "Populating Ordered Map... " << (int)progress << '%' << endl;
cout << "Populated Ordered Map with Data in: " << duration.count() << " seconds\n";
file.clear();
file.seekg(0, ios::beg);
getline(file, line);
progress = 0.0f;
start = chrono::high_resolution_clock::now();
while (getline(file, line))
{
//Create JeopardyQ obj and a string of the its category
JeopardyQ jq = ParseLine(line);
string category = jq[3];
//Add JeopardyQ to maps
unorderedData[category].push_back(jq);
//Progress bar
cout << "Populating Unordered Map... " << (int)progress << "%\r";
cout.flush();
progress += progFactor;
}
stop = chrono::high_resolution_clock::now();
duration = chrono::duration_cast<chrono::seconds>(stop - start);
cout << "Populating Unordered Map... " << (int)progress << '%' << endl;
cout << "Populated Unordered Map with Data in: " << duration.count() << " seconds\n";
ShowConsoleCursor(true);
CleanData();
file.close();
}
JeopardyQ JeopardyData::ParseLine(string& line)
{
stringstream ss(line);
string temp;
vector<string> v;
while (getline(ss, temp, '\t'))
{
temp.erase(remove(temp.begin(), temp.end(), '\\'), temp.end());
v.push_back(temp);
}
return JeopardyQ(v);
}
void JeopardyData::ShowConsoleCursor(bool showCursor)
{
//From https://stackoverflow.com/questions/18028808/remove-blinking-underscore-on-console-cmd-prompt
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(out, &cursorInfo);
cursorInfo.bVisible = showCursor;
SetConsoleCursorInfo(out, &cursorInfo);
}
void JeopardyData::CleanData()
{
auto iter1 = data.begin();
auto iter2 = unorderedData.begin();
//data and unorderedData should ALWAYS be the same size, so there are no edge cases to account for
//iterate through both at the same time, erasing elements that don't have >= 5 JeopardyQs
//(this is necessary because Jeopardy boards have 5 questions per category - don't want unusable data)
//(with the master file, amount of elements left over is > 168,555)
//(11,121 categories are unusable; ~200,700 remaining in the maps)
while (iter1 != data.end() && iter2 != unorderedData.end())
{
if (iter1->second.size() < 5)
data.erase(iter1++);
else
iter1++;
if (iter2->second.size() < 5)
unorderedData.erase(iter2++);
else
iter2++;
}
}
vector<JeopardyQ> JeopardyData::Find(string category)
{
auto start = chrono::high_resolution_clock::now();
vector<JeopardyQ> questions = this->data[category];
auto stop = chrono::high_resolution_clock::now();
if (questions.size() == 0)
{
cout << "Category not found." << endl;
return questions;
}
auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
cout << "Ordered Map: Found category " << category << " in " << duration.count() << " microseconds." << endl;
start = chrono::high_resolution_clock::now();
questions = this->unorderedData[category];
stop = chrono::high_resolution_clock::now();
duration = chrono::duration_cast<chrono::microseconds>(stop - start);
cout << "Unordered Map: Found category " << category << " in " << duration.count() << " microseconds." << endl;
unordered_set<int> randomNums;
vector<JeopardyQ> result;
while (randomNums.size() != 5) //generates 5 unique random numbers within the question set
{
int num = rand() % questions.size();
if (randomNums.find(num) == randomNums.end())
randomNums.insert(num);
}
for (auto iter = randomNums.begin(); iter != randomNums.end(); iter++) //add the 5 values to the result vector
result.push_back(questions[*iter]);
return result;
}
vector<JeopardyQ> JeopardyData::FindOrdered(string category)
{
auto start = chrono::high_resolution_clock::now();
vector<JeopardyQ> questions = this->data[category];
auto stop = chrono::high_resolution_clock::now();
if (questions.size() == 0)
{
cout << "Category not found." << endl;
return questions;
}
auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
cout << "Ordered Map: Found category " << category << " in " << duration.count() << " microseconds." << endl;
unordered_set<int> randomNums;
vector<JeopardyQ> result;
while (randomNums.size() != 5) //generates 5 unique random numbers within the question set
{
int num = rand() % questions.size();
if (randomNums.find(num) == randomNums.end())
randomNums.insert(num);
}
for (auto iter = randomNums.begin(); iter != randomNums.end(); iter++)//add the 5 values to the result vector
result.push_back(questions[*iter]);
return result;
}
vector<JeopardyQ> JeopardyData::FindUnordered(string category)
{
auto start = chrono::high_resolution_clock::now();
vector<JeopardyQ> questions = this->unorderedData[category];
auto stop = chrono::high_resolution_clock::now();
if (questions.size() == 0)
{
cout << "Category not found." << endl;
return questions;
}
auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
cout << "Unordered Map: Found category " << category << " in " << duration.count() << " microseconds." << endl;
unordered_set<int> randomNums;
vector<JeopardyQ> result;
while (randomNums.size() != 5) //generates 5 unique random numbers within the question set
{
int num = rand() % questions.size();
if (randomNums.find(num) == randomNums.end())
randomNums.insert(num);
}
for (auto iter = randomNums.begin(); iter != randomNums.end(); iter++)//add the 5 values to the result vector
result.push_back(questions[*iter]);
return result;
}
vector<string> JeopardyData::RandCategories()
{
vector<string> categories;
vector<int> indices;
//From https://diego.assencio.com/?index=6890b8c50169ef45b74db135063c227c
random_device device;
mt19937 generator(device());
uniform_int_distribution<int> dist(0, data.size() - 1);
//Populate indices with 5 random nums between 0 and num of categories
//Using find function to avoid collisions (no repeat nums)
for (int i = 0; i < 5; i++)
{
int randNum = dist(device);
if (find(indices.begin(), indices.end(), randNum) != indices.end())
i--;
else
indices.push_back(randNum);
}
//Populate categories with 5 random categories at the generated indices
int j = 0;
for (auto iter = data.begin(); iter != data.end(); iter++)
{
if (find(indices.begin(), indices.end(), j) != indices.end())
categories.push_back(iter->first);
if (categories.size() == 5)
break;
j++;
}
return categories;
}