-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathoa_jsonl.c
More file actions
414 lines (351 loc) · 10.8 KB
/
oa_jsonl.c
File metadata and controls
414 lines (351 loc) · 10.8 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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// oa_jsonl.c
// Copyright 2024 Kenny Peng
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#define ASSERT_INCREMENT(ptr, c) do {assert(*(ptr) == c); ++(ptr);} while(0)
#define DEFAULT_SIZE 100
// TODO: give it error handling?
typedef struct OaJsonl {
char **words;
int words_size, n_words;
char *abstract;
int abstract_size;
} OaJsonl;
static int initial_number_char(char c) {
return (c >= '0' && c <= '9') || c == '-';
}
static int number_char(char c) {
return initial_number_char(c) || c == '+' || c == 'e' || c == 'E' || c == '.';
}
static char* advance_space(char *ptr) {
// '\n' is not acceptable whitespace in JSONL format
for (; *ptr == ' ' || *ptr == '\t' || *ptr == '\r'; ++ptr);
return ptr;
}
static char* advance_string(char *ptr, int terminate) {
ASSERT_INCREMENT(ptr, '"');
// because only '"' and "\"" are relevant, break from normal JSON parsing
// with a backwards-advancing pointer scheme to achieve the minimal loop
while (1) {
if (*ptr == '"') {
int cnt = 0;
for (char *tmp = ptr-1; *tmp == '\\'; --tmp) ++cnt;
if ((cnt & 0x1) == 0) break;
}
++ptr;
}
if (terminate) {
assert(*ptr == '"');
*(ptr++) = '\0';
} else {
ASSERT_INCREMENT(ptr, '"');
}
return ptr;
}
static char* advance_composite_open(char *ptr, char open_c) {
ptr = advance_space(ptr);
ASSERT_INCREMENT(ptr, open_c);
// edge case: list or object can be empty, so burn through whitespace
// to potentially find close_c, even if it's the first value's whitespace
ptr = advance_space(ptr);
return ptr;
}
static char* advance_composite_try_close(char *ptr, char close_c) {
if (*ptr != close_c) {
return NULL;
}
++ptr; // close_c
ptr = advance_space(ptr);
return ptr;
}
static char* advance_composite_next(char *ptr) {
if (*ptr == ',') {
++ptr; // ','
}
return ptr;
}
static char* advance_value_skip(char *ptr) {
ptr = advance_space(ptr);
if (initial_number_char(*ptr)) { // number
for (; number_char(*ptr); ++ptr);
} else if (*ptr == 'f') { // false
ptr += 5;
} else if (*ptr == 't' || *ptr == 'n') { // true or null
ptr += 4;
} else if (*ptr == '"') { // string
ptr = advance_string(ptr, 0);
} else if (*ptr == '{' || *ptr == '[') { // array or object
int brackets = (*ptr == '[');
int braces = (*ptr == '{');
++ptr;
while (brackets || braces) {
if (*ptr == '"') {
ptr = advance_string(ptr, 0);
} else {
switch (*ptr) {
case '[': ++brackets; break;
case ']': --brackets; break;
case '{': ++braces; break;
case '}': --braces; break;
}
++ptr;
}
}
} else { // not a JSON type (impossible for valid JSON)
assert(0);
}
ptr = advance_space(ptr);
return ptr;
}
static char* parse_string(char *ptr, char **dst) {
ptr = advance_space(ptr);
*dst = ptr + 1; // string starts after '"'
ptr = advance_string(ptr, 1);
ptr = advance_space(ptr);
return ptr;
}
static char* parse_nullable_string(char *ptr, char **dst) {
// similar to parse_string, but '"' is not a hard requirement
ptr = advance_space(ptr);
if (*ptr == '"') {
*dst = ptr + 1; // string starts after '"'
ptr = advance_string(ptr, 1);
} else if (*ptr == 'n') {
*dst = NULL;
ptr += 4;
} else {
assert(0);
}
ptr = advance_space(ptr);
return ptr;
}
static char* parse_name(char *ptr, char **dst) {
// similar to parse_string, but colon is a hard requirement
ptr = parse_string(ptr, dst); // same code here
ASSERT_INCREMENT(ptr, ':');
return ptr;
}
// TODO: add checking to hthis
static char* parse_integer(char *ptr, int *val) {
int neg = 0;
ptr = advance_space(ptr);
if (*ptr == '-') {
neg = 1;
++ptr; // '-'
}
*val = *(ptr++) - '0';
while (*ptr >= '0' && *ptr <= '9') {
*val *= 10;
*val += *(ptr++) - '0';
}
if (neg) {
*val = -(*val);
}
ptr = advance_space(ptr);
return ptr;
}
static char* parse_list_open(char *ptr) {
return advance_composite_open(ptr, '[');
}
static char* parse_list_try_close(char *ptr) {
return advance_composite_try_close(ptr, ']');
}
static char* parse_list_next(char *ptr) {
return advance_composite_next(ptr);
}
static char* parse_object_open(char *ptr) {
return advance_composite_open(ptr, '{');
}
static char* parse_object_try_close(char *ptr) {
return advance_composite_try_close(ptr, '}');
}
static char* parse_object_next(char *ptr) {
return advance_composite_next(ptr);
}
OaJsonl* oajsonl_alloc(int words_size, int abstract_size) {
if (words_size < 0) {
words_size = DEFAULT_SIZE;
}
if (abstract_size < 0) {
abstract_size = DEFAULT_SIZE;
}
OaJsonl* oa = (OaJsonl*)malloc(sizeof(OaJsonl));
oa->words = (char**)malloc(words_size*sizeof(char*));
oa->words_size = words_size;
oa->abstract = (char*)malloc(abstract_size*sizeof(char));
oa->abstract_size = abstract_size;
return oa;
}
static void oajsonl_init_abstract(OaJsonl* oa) {
oa->n_words = 0;
oa->abstract[0] = '\0';
}
static void oajsonl_add_word(OaJsonl* oa, int idx, char* word) {
int prev;
if (idx >= oa->words_size) {
prev = oa->words_size;
oa->words_size = idx*2;
oa->words = (char**)realloc(oa->words, oa->words_size*sizeof(char*));
}
if (idx >= oa->n_words) {
prev = oa->n_words;
oa->n_words = idx+1;
for (int i = prev; i < oa->n_words; i++) {
oa->words[i] = NULL;
}
}
oa->words[idx] = word;
}
// TODO: this could be cleaner?
static char* oajsonl_realloc_abstract(OaJsonl* oa, char* ptr) {
int n = (ptr != NULL) ? ptr - oa->abstract : -1;
oa->abstract_size *= 2;
oa->abstract = (char*)realloc(oa->abstract, oa->abstract_size);
return ptr ? oa->abstract + n : NULL;
}
void oajsonl_build_abstract(OaJsonl* oa) {
char* tmp = oa->abstract;
for (int i = 0; i < oa->n_words; i++) {
if (oa->words[i] == NULL) {
continue;
}
for (char *tmp_2 = oa->words[i]; *tmp_2 != '\0'; ++tmp_2) {
*(tmp++) = *tmp_2;
if (tmp >= (oa->abstract + oa->abstract_size)) {
tmp = oajsonl_realloc_abstract(oa, tmp);
}
}
if (i != oa->n_words-1) {
*(tmp++) = ' ';
if (tmp >= (oa->abstract + oa->abstract_size)) {
tmp = oajsonl_realloc_abstract(oa, tmp);
}
}
}
*tmp = '\0';
}
char* oajsonl_parse_abstract_inverted_index(OaJsonl *oa, char *ptr, char** abstract) {
*abstract = NULL;
// edge case: abstract_inverted_index is nullable, so burn through
// whitespace to potentially find null, even if it's the object's whitespace
ptr = advance_space(ptr);
if (*ptr == 'n') {
ptr += 4;
ptr = advance_space(ptr);
return ptr;
}
oajsonl_init_abstract(oa);
char *tmp, *tmp_2;
for (
ptr = parse_object_open(ptr);
(tmp = parse_object_try_close(ptr)) == NULL;
ptr = parse_object_next(ptr)
) {
char *word;
ptr = parse_name(ptr, &word);
for (
ptr = parse_list_open(ptr);
(tmp_2 = parse_list_try_close(ptr)) == NULL;
ptr = parse_list_next(ptr)
) {
int idx;
ptr = parse_integer(ptr, &idx);
oajsonl_add_word(oa, idx, word);
}
ptr = tmp_2;
}
ptr = tmp;
oajsonl_build_abstract(oa);
*abstract = oa->abstract;
return ptr;
}
void oajsonl_destroy(OaJsonl* oa) {
free(oa->words);
free(oa->abstract);
free(oa);
}
int read_line(FILE *f, char** line, int line_size) {
int c = -1, i = 0;
while (c != '\n') {
c = fgetc(f);
if (c == EOF) {
c = '\n';
}
(*line)[i++] = c;
if (i >= line_size) {
line_size *= 2;
*line = (char*)realloc(*line, line_size);
}
}
(*line)[i] = '\0';
return line_size;
}
int main(){
int line_size = DEFAULT_SIZE;
char *line = (char*)malloc(line_size);
OaJsonl *oa = oajsonl_alloc(-1, -1);
FILE *f = stdin;
while (!feof(f)) {
line_size = read_line(f, &line, line_size);
// if the file ended with '\n', then the last "line" will be empty
if (line[0] == '\n') {
break;
}
int drop = 0;
char *ptr, *tmp, *tmp_2;
char *id, *title, *lang, *abstract;
for (
ptr = parse_object_open(line);
(tmp = parse_object_try_close(ptr)) == NULL;
ptr = parse_object_next(ptr)
) {
ptr = parse_name(ptr, &tmp_2);
if (strcmp(tmp_2, "id") == 0) {
ptr = parse_string(ptr, &id);
} else if (strcmp(tmp_2, "title") == 0) {
ptr = parse_nullable_string(ptr, &title);
} else if (strcmp(tmp_2, "language") == 0) {
ptr = parse_nullable_string(ptr, &lang);
if (!lang || strcmp(lang, "en") != 0) {
drop = 1;
break;
}
} else if (strcmp(tmp_2, "abstract_inverted_index") == 0) {
ptr = oajsonl_parse_abstract_inverted_index(oa, ptr, &abstract);
if (!abstract || abstract[0] == '\0') {
drop = 1;
break;
}
} else {
ptr = advance_value_skip(ptr);
}
}
if (drop) {
continue;
}
ptr = tmp;
// TODO: handle UTF-16 surrogate pairs instead of outputting JSON
if (title) {
printf(
"{\"id\":\"%s\",\"document\":\"%s %s\"}\n", id, title, abstract
);
} else {
printf(
"{\"id\":\"%s\",\"document\":\"%s\"}\n", id, abstract
);
}
}
oajsonl_destroy(oa);
}