-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
353 lines (308 loc) · 12.2 KB
/
main.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
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
#include <thread>
#include <future>
#include "util/data.h"
ad_characteristic_map gen_ad_characteristic_map(std::string filename, bool is_entity);
std::map<std::string, ad_characteristic_map> gen_ad_characteristic_map_set(std::string doc_construct, IdMap &entity_map);
void gen_user_topic_map(
int tid,
user_characteristic_map *user_topic_map,
std::string filename,
int start_row,
int end_row,
document_topic_map *doc_topic_map,
user_characteristic_set *user_topic_ref,
IdMap *uuid_map,
IdMap *entity_map);
std::vector<user_characteristic_map> gen_user_topic_map_set(
IdMap &uuid_map, std::string doc_filename, display_map *display_map, bool is_entity, IdMap &entity_map);
void write_user_ad_interaction_on_topic(
std::string doc_type,
std::string doc_file,
display_map *display_map,
ad_map *ad_map,
IdMap &entity_map,
IdMap &uuid_map
);
ad_characteristic_map gen_ad_characteristic_map(
std::string filename,
bool is_entity,
IdMap &entity_map
)
{
//read promoted_content
ad_characteristic_map ad_characteristic_map;
std::string id1;
std::string id2;
std::string others;
Timer tmr;
std::cout << "Start processing " << filename << std::endl;
CsvGzReader file(filename);
//generate ad topic map set
int row_count = 0; //processed rows
if (is_entity == false) {
while (file.getline(&id1, ',')) {
file.getline(&id2);
auto id1_value = ad_characteristic_map.find(stoi(id1));
if (id1_value != ad_characteristic_map.end()) {
id1_value->second.push_back(stoi(id2));
} else {
std::vector<int> v;
v.push_back(stoi(id2));
ad_characteristic_map.insert({stoi(id1), v});
}
++row_count;
}
} else {
while (file.getline(&id1, ',')) {
file.getline(&id2);
int entity_id = entity_map.get_id(id2);
auto id1_value = ad_characteristic_map.find(stoi(id1));
if (id1_value != ad_characteristic_map.end()) {
id1_value->second.push_back(entity_id);
} else {
std::vector<int> v;
v.push_back(entity_id);
ad_characteristic_map.insert({stoi(id1), v});
}
++row_count;
}
}
//Cleanup
std::cout << "\nrow_count = " << row_count << std::endl;
tmr.finish();
return ad_characteristic_map;
};
std::map<std::string, ad_characteristic_map> gen_ad_characteristic_map_set(
std::string doc_construct,
IdMap &entity_map
) {
std::string top_k = "5";
bool is_entity = false;
std::map<std::string, ad_characteristic_map> ad_characteristic_map_set;
std::string ad_constructs[] = {"campaign", "advertiser"};
//std::string ad_constructs[] = {"advertiser"};
if (doc_construct == "entity") {
is_entity = true;
}
for (std::string &ad_c: ad_constructs) {
std::cout << "Generating map for " << ad_c + doc_construct << std::endl;
ad_characteristic_map_set[ad_c + doc_construct] = gen_ad_characteristic_map(
"cache/" + ad_c + "_top" + top_k + "_" + doc_construct + ".csv.gz", is_entity, entity_map);
}
return ad_characteristic_map_set;
};
void gen_user_topic_map(
int tid,
user_characteristic_map *user_topic_map,
std::string filename,
int start_row,
int end_row,
document_topic_map *doc_topic_map,
user_characteristic_set *user_topic_ref,
IdMap *uuid_map,
IdMap *entity_map)
{
std::string uuid;
std::string document_id;
std::string others;
// I. calculate user-topic interaction based on page_views
Timer tmr;
std::cout << tid << "Start processing " << filename << std::endl;
CsvGzReader file(filename);
// skip rows until start row
int i = 0; //all rows
while(i < start_row - 1) {
file.getline(&others);
++i;
}
// start processing
int row_count = 0; //processed rows
while(file.getline(&uuid, ',') && i < end_row) {
file.getline(&document_id, ',');
file.getline(&others);
auto user = uuid_map->data()->find(uuid); // convert string uuid to int uid to save memory
// if the document has topics associated with it
auto document = (*doc_topic_map).find(stoi(document_id));
if (user != uuid_map->data()->end() && document != (*doc_topic_map).end()) {
for (auto &t: document->second) {
//if user topic exists in the reference
auto user_topic = (*user_topic_ref).find(std::make_pair(user->second, t.first));
if (user_topic != (*user_topic_ref).end()) {
auto user_topic2 = (*user_topic_map).find(std::make_pair(user->second, t.first));
if (user_topic2 != (*user_topic_map).end()) {
// if user topic exists in the map
user_topic2->second += t.second;
} else {
// if not
(*user_topic_map).insert({std::make_pair(user->second, t.first), t.second});
}
}
}
}
if (i % 10000000 == 0) {
std::cout << "[" <<start_row << "]" << i/10000000 << "0M...";
std::cout.flush();
}
++row_count;
++i;
}
std::cout << "\nrow_count = " << row_count <<" (" << start_row << " - " << end_row << ")" << std::endl;
tmr.finish();
}
std::vector<user_characteristic_map> gen_user_topic_map_set(
IdMap &uuid_map,
std::string doc_filename,
display_map *display_map,
bool is_entity,
IdMap &entity_map
)
{
// 1. generate document topic map
// <document_id, <topic_id, confidence_level>>
document_topic_map doc_topic_map = gen_doc_topic_map(doc_filename, is_entity, entity_map);
// 2. generate user topic reference map
// <display_id, <uuid, document_id>>
user_characteristic_set user_topic_ref = gen_user_topic_ref(
display_map, &doc_topic_map);
// 3. user topic map set
std::vector<user_characteristic_map> user_topic_map_set;
std::string filename = "../input/page_views.csv.gz";
//std::string filename = "../input/page_views_sample.csv.gz";
unsigned int num_thread = 5;
int num_row = 2034275448/num_thread + 1; //406,855,090
//init user_topic_map
for (int i = 0; i < num_thread; ++i) {
user_characteristic_map user_topic_map;
user_topic_map_set.push_back(user_topic_map);
}
//start thread
std::vector<std::thread> thread_list;
for (int i = 0; i < num_thread; ++i) {
thread_list.push_back(std::thread(gen_user_topic_map,
i,
&user_topic_map_set[i],
filename,
(i * num_row + 1),
((1 + i) * num_row),
&doc_topic_map,
&user_topic_ref,
&uuid_map,
&entity_map));
}
//finish thread
for (auto &t: thread_list) {
t.join();
}
return user_topic_map_set;
}
int calc_user_ad_interaction_topic(
std::string doc_type,
std::string ad_type,
std::string click_file,
int (*get_key) (ad),
std::map<std::string, ad_characteristic_map> *ad_topic_map_set,
std::vector<user_characteristic_map> *user_topic_map_set,
ad_map *ad_map,
display_map *display_map
)
{
std::string outfile_name = "cache/clicks_" + click_file + "_user_"+ ad_type +"_interaction_on_" + doc_type + ".csv.gz";
// read clicks_train
std::string filename = "../input/clicks_" + click_file + ".csv.gz";
std::string display_id;
std::string ad_id;
std::string others;
ad_characteristic_map vec = (*ad_topic_map_set)[ad_type+doc_type];
Timer tmr;
std::cout << "Start generating " << outfile_name << std::endl;
CsvGzReader file(filename);
// write interaction weights
std::ofstream outfile(outfile_name, std::ios_base::out | std::ios_base::binary);
boost::iostreams::filtering_streambuf<boost::iostreams::output> out;
out.push(boost::iostreams::gzip_compressor());
out.push(outfile);
std::ostream outstream(&out);
outstream << "weight\n";
// for row
// read clicks_train row
// save interaction to separate file
int i = 0;
while(file.getline(&display_id, ',')) {
file.getline(&ad_id);
//calculate weight
float weight = 0.0;
// if uuid and document id related to the display_id exists
auto display = (*display_map).find(stoi(display_id));
if (display != (*display_map).end()) {
// if ad_id related exists
auto ad = (*ad_map).find(stoi(ad_id));//
if (ad != (*ad_map).end()) {
// if topic id related to the document id exists
auto ad_topic= vec.find((*get_key)(ad->second));
if (ad_topic != vec.end()) {
for (auto &t: ad_topic->second) {
// if topic id related to the user id exists
for (auto &ut_map: (*user_topic_map_set)) {
auto user_topic = ut_map.find(std::make_pair(display->second.first, t));
if (user_topic != ut_map.end()) {
weight += user_topic->second;
//std::cout << "+ " << user_topic->second << " => " << adv_weight << std::endl;
}
}
}
}
}
}
outstream << weight <<"\n";
if (i % 1000000 == 0) {
std::cout << i / 1000000 << "M...";
std::cout.flush();
}
++i;
}
std::cout << "\ni = " << i << std::endl;
tmr.finish();
return 0;
}
void write_user_ad_interaction_on_topic(
std::string doc_type,
std::string doc_file,
display_map *display_map,
ad_map *ad_map,
IdMap &entity_map,
IdMap &uuid_map
){
bool is_entity = false;
if (doc_type == "entity") {
is_entity = true;
}
// I. Read file
// <advertiser_id, <topic_id, confidence_level>>
std::map<std::string, ad_characteristic_map> ad_char_set = gen_ad_characteristic_map_set(
doc_type, entity_map);
// <<uuid, topic_id>, sum_confidence_level>
std::vector<user_characteristic_map> user_topic_map_set = gen_user_topic_map_set(
uuid_map, doc_file, display_map, is_entity, entity_map);
// II. calculate user-document interaction in terms of topic
calc_user_ad_interaction_topic(doc_type, "advertiser", "train", get_adv_id, &ad_char_set, &user_topic_map_set, ad_map, display_map);
calc_user_ad_interaction_topic(doc_type, "campaign", "train", get_camp_id, &ad_char_set, &user_topic_map_set, ad_map, display_map);
calc_user_ad_interaction_topic(doc_type, "advertiser", "test", get_adv_id, &ad_char_set, &user_topic_map_set, ad_map, display_map);
calc_user_ad_interaction_topic(doc_type, "campaign", "test", get_camp_id, &ad_char_set, &user_topic_map_set, ad_map, display_map);
}
int main() {
IdMap uuid_map;
IdMap entity_map;
std::map<std::string, std::string> doc_files = {
{"topic", "../input/documents_topics.csv.gz"},
{"entity", "../input/documents_entities.csv.gz"},
{"category", "../input/documents_categories.csv.gz"}};
display_map display_map = gen_display_map(uuid_map);
ad_map ad_map = gen_ad_map();
////user ad interaction on topic
write_user_ad_interaction_on_topic("topic", doc_files["topic"], &display_map, &ad_map, entity_map, uuid_map);
////user ad interaction on entity
write_user_ad_interaction_on_topic("entity", doc_files["entity"], &display_map, &ad_map, entity_map, uuid_map);
////user ad interaction on category
write_user_ad_interaction_on_topic("category", doc_files["category"], &display_map, &ad_map, entity_map, uuid_map);
return 0;
}