-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·283 lines (248 loc) · 10.6 KB
/
main.cpp
File metadata and controls
executable file
·283 lines (248 loc) · 10.6 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
#include <iostream>
#include <fstream>
#include <algorithm>
#include <sstream>
#include <vector>
#include <list>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <ctime>
#include <thread>
#include <regex>
#include <uWS/uWS.h>
#include "movie.hpp"
#include "levenshtein.hpp"
using namespace std;
using json = nlohmann::json;
inline void log_query_and_speed(string query, clock_t begin) {
clock_t end = clock();
double elapsed_ms = double(end - begin) / CLOCKS_PER_SEC * 1000;
cout << "Processed query: " << query << " in " << elapsed_ms << " ms" << endl;
}
int main() {
/* Variables and files for stats */
unordered_map<string, vector<movie*>> ngrams;
unordered_map<string, unordered_map<string, double>> genre_freq, nationality_freq;
stack<movie, list<movie>> movies;
ifstream jsonfile("db/fulldump.json");
string line;
int i = 0, number_of_movies = 88000;
/* Load DB */
if (!jsonfile.is_open()) {
cerr << "Could not open db/fulldump.json" << endl;
return EXIT_FAILURE;
}
while (getline(jsonfile, line)) {
/* Progress */
if (i++%2000 == 0) {
cout << "\rLoading movies... " << i * 100 / number_of_movies << "% " << flush;
}
/* Parse and create movie */
movies.push(json::parse(line));
movie& m = movies.top();
/* Compute and index n-grams of the titles */
string title = m.title;
stem(title);
unordered_set<string> localngrams;
for (auto j = title.begin(); j != title.end(); ++j) {
for (auto k = j; k != title.end(); ++k) {
string ngram = string(j, k+1);
if (localngrams.find(ngram) != localngrams.end()) // Avoid mapping two similar n-grams to the same movie
continue;
localngrams.insert(ngram);
ngrams[ngram].push_back(&m); // Index movie to each n-gram of the title
}
}
/* Extract words */
istringstream title_ss(title);
vector<string> words{istream_iterator<string>{title_ss}, istream_iterator<string>{}};
words.erase (remove_if (words.begin (), words.end (), isACommonWord), words.end ());
for (auto w: words) {
for (auto g: m.genre)
genre_freq[w][g] += (m.rank + 600000)/((double) 60000)/((double) words.size());
for (auto g: m.nationalities)
nationality_freq[w][g] += (m.rank + 600000)/((double) 60000)/((double) words.size());
}
}
/* Pre-rank movies according to each n-gram */
i = 0;
cout << endl;
size_t sz = ngrams.size();
for (auto& e: ngrams) {
// Progress
if (i++%600000 == 0) {
cout << "\rRanking movies... " << i * 100 / (sz-600000) << "% " << flush;
}
// Rank movies
sort(e.second.begin(), e.second.end(), moviecomp_max());
}
cout << endl << "Normalizing frequencies...";
/* Normalize frequencies */
for (auto& gf: genre_freq) {
double total = 0;
for (auto &f: gf.second) total += f.second;
for (auto &f: gf.second) f.second /= total;
}
for (auto& nf: nationality_freq) {
double total = 0;
for (auto &f: nf.second) total += f.second;
for (auto &f: nf.second) f.second /= total;
}
cout << " ok" << endl;
/* Close DB */
jsonfile.close();
/* Print aggregated stats */
cout << endl << movies.size() << " movies";
cout << endl << sz << " ngrams";
cout << endl << "Server started !" << endl;
/* START SERVER */
uWS::Hub h;
/* Web Socket Controller */
h.onMessage([&ngrams, &genre_freq, &nationality_freq](uWS::WebSocket<uWS::SERVER> *ws, char *message, size_t length, uWS::OpCode opCode) {
if (length > 80) {ws->send("", 0, opCode); return;}
clock_t begin = clock();
message[length] = '\0';
char feature = message[0];
string response;
string input(message+1);
stem(input);
// Full Movie Detail
if (feature == 'f') {
if (ngrams[input].size()) {
json j = *(ngrams[input][0]);
response = j.dump();
}
}
// Discover Feature
else if (feature == 'd') {
vector<movie *> &exact_movies = ngrams[input];
// Extact Match
if (ngrams[input].size() >= 6) {
for (int i = 0; i < 6; ++i) {
movie *e = exact_movies[i];
response += e->title + "§" + e->local_cover + "§" + e->director + "§" + e->date + "\n";
}
}
// Fuzzy Search
else {
fuzzy_search_context<movie *, moviecomp_min> ctx(input, &ngrams);
vector<movie *> fuzzy_movies = ctx.best_matches(6, 1); // 6 results, Max levenstein distance = 1
size_t fuzzy_count = fuzzy_movies.size() > 6 ? 6 : fuzzy_movies.size();
for (int i = 0; i < fuzzy_count; ++i) {
movie *e = fuzzy_movies[i];
response += e->title + "§" + e->local_cover + "§" + e->director + "§" + e->date + "\n";
}
}
}
// Analyze feature
else if (feature == 'a') {
istringstream title_ss(input);
vector<string> words{istream_iterator<string>{title_ss}, istream_iterator<string>{}};
unordered_map<string, double> genre_proba, nationality_proba;
vector<pair<double, string>> genre_res, nationality_res;
double total;
for (string& word: words) {
if (isACommonWord(word))
continue;
// genre analysis
auto genre_freq_it = genre_freq.find(word);
if (genre_freq_it == genre_freq.end())
continue;
for (auto& a_genre_freq: (*genre_freq_it).second) {
genre_proba[a_genre_freq.first] += a_genre_freq.second;
}
// Nationality analysis
auto nationality_freq_it = nationality_freq.find(word);
if (nationality_freq_it == nationality_freq.end())
continue;
for (auto& a_nationality_freq: (*nationality_freq_it).second) {
nationality_proba[a_nationality_freq.first] += a_nationality_freq.second;
}
}
// Sort genre results
total = 0;
for (auto& gp: genre_proba) total += gp.second;
for (auto& gp: genre_proba) genre_res.push_back(make_pair(gp.second/total, gp.first));
sort(genre_res.rbegin(), genre_res.rend());
genre_res.resize(min((size_t)4, genre_res.size())); // Keep 4 best genres
// Sort nationality results
total = 0;
for (auto& np: nationality_proba) total += np.second;
for (auto& np: nationality_proba) nationality_res.push_back(make_pair(np.second/total, np.first));
sort(nationality_res.rbegin(), nationality_res.rend());
nationality_res.resize(min((size_t)2, nationality_res.size())); // Keep 2 best nationalities
// JSON output
json j;
j["query"] = string(message+1);
j["genre"] = {};
j["nationality"] = {};
for (auto& gr: genre_res) if (gr.first > 0.01) j["genre"].push_back({{gr.second, (int) (gr.first*100)}});
for (auto& nr: nationality_res) if (nr.first > 0.01) j["nationality"].push_back({{nr.second,(int) (nr.first*100)}});
response = j.dump();
}
ws->send(response.data(), response.length(), opCode);
log_query_and_speed(("WS "+string(message)).data(), begin);
});
/* HTTP Server Controller */
h.onHttpRequest([](uWS::HttpResponse *res, uWS::HttpRequest req, char *data, size_t length, size_t remainingBytes) {
// Parse request and discard malformed requests
if (length > 80) {res->end(); return;}
clock_t begin = clock();
string url = req.getUrl().toString();
string error404 = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nContent-type: text/plain\r\n\r\n";
if (url == "/") url = "/index.html";
if (!regex_match(url, regex("/[a-zA-Z0-9]{1,15}\\.[a-z]{2,4}"))) {
res->write(error404.data(), error404.length());
res->end();
log_query_and_speed(("HTTP "+url).data(), begin);
return;
}
if (url.substr(url.size() - 3) == "jpg" || url.substr(url.size() - 3) == "png" || url.substr(url.size() - 3) == "ico") url = "/img"+url;
if (url.substr(url.size() - 3) == "txt") url = "./stats"+url;
else url = "./web"+url;
ifstream in(url);
// If requested file exists
if (in.is_open()) {
// Build HTTP headers
string cache = "max-age=172800, public, must-revalidate";
string content_type;
if (url.substr(url.size() - 3) == "jpg") {
content_type = "image/jpeg";
cache = "max-age=31536000, public";
} else if (url.substr(url.size() - 3) == "png" || url.substr(url.size() - 3) == "ico") {
content_type = "image/png";
cache = "max-age=31536000, public";
} else if (url.substr(url.size() - 3) == "css")
content_type = "text/css";
else if (url.substr(url.size() - 4) == "html")
content_type = "text/html; charset=utf-8";
else if (url.substr(url.size() - 2) == "js")
content_type = "application/javascript";
else
content_type = "text/plain; charset=utf-8";
// Read file
string str(static_cast<stringstream const&>(stringstream() << in.rdbuf()).str());
string ok200 = "HTTP/1.1 200 OK\r\n"
"Content-Length: "+to_string(str.length())+"\r\n"
"Content-Type: "+content_type+"\r\n"
"Cache-Control: "+cache+"\r\n\r\n";
// Send file contents
res->write((ok200 + str).data(), ok200.length() + str.length());
res->end();
log_query_and_speed(("HTTP "+url).data(), begin);
return;
}
// Or redirect to home page
else {
res->write(error404.data(), error404.length());
res->end();
log_query_and_speed(("HTTP "+url).data(), begin);
return;
}
});
if (!h.listen(2200))
cout << "Failed to listen" << endl << "Server stopped" << endl;
h.run();
return 0;
}