main.cpp
6.98 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
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
#include <cstdio>
#include <cinttypes> //PRId64
#include <exception>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <boost/regex.hpp>
#include <rapidjson/document.h>
#include <rapidjson/istreamwrapper.h>
#include <rapidjson/reader.h>
using namespace std;
enum MODE {
NONE = 0,
PAPER = 1,
COAUTHOR = 2,
};
const int mode = MODE::PAPER | MODE::COAUTHOR;
const char* DBLP_FILENAME = "dblp.json";
const char* DBLP_COAUTHOR_FILENAME = "tmp_dblp_coauthorship.json";
const string COLUMN_DELIMITER = "||";
const string AUTHOR_DELIMITER = "&&";
boost::regex paper_reg{"(conf|journals).*"};
struct PaperRecord {
string paper_key;
vector<string> authors;
unsigned int year;
void write(ofstream& fout) {
fout << paper_key << COLUMN_DELIMITER;
for (auto it=authors.begin(); it!=authors.end(); ++it) {
if (it != authors.begin()) {
fout << AUTHOR_DELIMITER;
}
fout << *it;
}
fout << COLUMN_DELIMITER << year << endl;
}
void clear() {
paper_key.clear();
authors.clear();
year = 1;
}
void add_author(string str) {
authors.push_back(str);
}
};
struct DblpPaperHandler {
bool whole_array = false;
bool is_record = false;
bool is_authors = false;
bool is_paper = false;
uint64_t record_count = 0;
PaperRecord paper;
ofstream& ofs;
DblpPaperHandler(ofstream& fout)
: ofs(fout) {
}
//
bool Null() {
return true;
}
bool Bool(bool b) {
//cout << "Bool(" << boolalpha << b << ")" << endl;
return true;
}
bool Int(int i) {
//cout << "Int(" << i << ")" << endl;
return true;
}
bool Uint(unsigned u) {
//cout << "Uint(" << u << ")" << endl;
if (is_paper) {
paper.year = u;
}
return true;
}
bool Int64(int64_t i) {
//cout << "Int64(" << i << ")" << endl;
return true;
}
bool Uint64(uint64_t u) {
//cout << "Uint64(" << u << ")" << endl;
return true;
}
bool Double(double d) {
//cout << "Double(" << d << ")" << endl;
return true;
}
bool RawNumber(const char* str, rapidjson::SizeType length, bool copy) {
//cout << "Number(" << str << ", " << length << ", " << boolalpha << copy << ")" << endl;
return true;
}
bool String(const char* str, rapidjson::SizeType length, bool copy) {
if (is_record) {
if (is_authors) {
if (!is_paper)
return true;
paper.add_author(string(str));
} else {
if (boost::regex_match(str, paper_reg)) {
is_paper = true;
paper.paper_key = string(str);
}
}
}
return true;
}
bool StartObject() {
//cout << "StartObject()" << endl;
return true;
}
bool Key(const char* str, rapidjson::SizeType length, bool copy) {
//cout << "Key(" << str << ", " << length << ", " << boolalpha << copy << ")" << endl;
return true;
}
bool EndObject(rapidjson::SizeType memberCount) {
//cout << "EndObject(" << memberCount << ")" << endl;
return true;
}
bool StartArray() {
if (!whole_array) {
whole_array = true;
} else if (!is_record) {
is_record = true;
} else if (!is_authors) {
is_authors = true;
}
return true;
}
bool EndArray(rapidjson::SizeType elementCount) {
if (is_record) {
if (is_authors) {
is_authors = false;
} else {
if (is_paper) {
paper.write(ofs);
paper.clear();
}
is_record = false;
is_paper = false;
++record_count;
if (record_count % 100000 == 0) {
printf("* [%" PRIu64 "] \n", record_count);
}
}
} else {
whole_array = false;
printf("* total paper record: [%" PRIu64 "]\n", record_count);
}
return true;
}
};
struct CoauthorRecord {
string author1, author2;
unsigned int year;
void write(ofstream& fout) {
fout << author1 << COLUMN_DELIMITER
<< author2 << COLUMN_DELIMITER
<< year << endl;
}
};
struct DblpCoauthorHandler {
bool whole_array = false;
bool is_record = false;
int read_author_cnt = 0;
uint64_t record_count = 0;
CoauthorRecord coauthor;
ofstream& ofs;
DblpCoauthorHandler(ofstream& fout)
: ofs(fout) {
}
//
bool Null() {
return true;
}
bool Bool(bool b) {
//cout << "Bool(" << boolalpha << b << ")" << endl;
return true;
}
bool Int(int i) {
//cout << "Int(" << i << ")" << endl;
return true;
}
bool Uint(unsigned u) {
coauthor.year = u;
return true;
}
bool Int64(int64_t i) {
//cout << "Int64(" << i << ")" << endl;
return true;
}
bool Uint64(uint64_t u) {
//cout << "Uint64(" << u << ")" << endl;
return true;
}
bool Double(double d) {
//cout << "Double(" << d << ")" << endl;
return true;
}
bool RawNumber(const char* str, rapidjson::SizeType length, bool copy) {
//cout << "Number(" << str << ", " << length << ", " << boolalpha << copy << ")" << endl;
return true;
}
bool String(const char* str, rapidjson::SizeType length, bool copy) {
if (is_record) {
if (read_author_cnt == 0) {
coauthor.author1 = string(str);
} else {
coauthor.author2 = string(str);
}
++read_author_cnt;
}
return true;
}
bool StartObject() {
//cout << "StartObject()" << endl;
return true;
}
bool Key(const char* str, rapidjson::SizeType length, bool copy) {
//cout << "Key(" << str << ", " << length << ", " << boolalpha << copy << ")" << endl;
return true;
}
bool EndObject(rapidjson::SizeType memberCount) {
//cout << "EndObject(" << memberCount << ")" << endl;
return true;
}
bool StartArray() {
if (!whole_array) {
whole_array = true;
} else if (!is_record) {
is_record = true;
read_author_cnt = 0;
}
return true;
}
bool EndArray(rapidjson::SizeType elementCount) {
if (is_record) {
coauthor.write(ofs);
is_record = false;
++record_count;
if (record_count % 100000 == 0) {
printf("* [%" PRIu64 "] \n", record_count);
}
} else {
whole_array = false;
printf("* total paper record: [%" PRIu64 "]\n", record_count);
}
return true;
}
};
int main(int argc, char* argv[]) {
rapidjson::Reader reader;
try {
if (mode & MODE::PAPER) {
ifstream dblp_paper_in;
ofstream dblp_paper_out;
dblp_paper_in.open(DBLP_FILENAME);
dblp_paper_out.open((string(DBLP_FILENAME)+string(".out")).c_str());
if (!dblp_paper_in || !dblp_paper_out) {
throw exception("dblp paper file");
}
DblpPaperHandler paper_handler(dblp_paper_out);
rapidjson::IStreamWrapper dblp_paper_isw(dblp_paper_in);
reader.Parse(dblp_paper_isw, paper_handler);
if (dblp_paper_in) dblp_paper_in.close();
if (dblp_paper_out) dblp_paper_out.close();
}
if (mode & MODE::COAUTHOR) {
ifstream dblp_coauthor_in;
ofstream dblp_coauthor_out;
dblp_coauthor_in.open(DBLP_COAUTHOR_FILENAME);
dblp_coauthor_out.open((string(DBLP_COAUTHOR_FILENAME)+string(".out")).c_str());
if (!dblp_coauthor_in || !dblp_coauthor_out) {
throw exception("dblp coauthor file");
}
DblpCoauthorHandler coauthor_handler(dblp_coauthor_out);
rapidjson::IStreamWrapper dblp_coauthor_isw(dblp_coauthor_in);
reader.Parse(dblp_coauthor_isw, coauthor_handler);
if (dblp_coauthor_in) dblp_coauthor_in.close();
if (dblp_coauthor_out) dblp_coauthor_out.close();
}
}
catch (const exception& e) {
cerr << "Error: " << e.what() << endl;
return -1;
}
return 0;
}