-
Notifications
You must be signed in to change notification settings - Fork 76
/
grobid_client.py
509 lines (458 loc) · 17.1 KB
/
grobid_client.py
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
"""
Grobid Python Client
This version uses the standard ThreadPoolExecutor for parallelizing the
concurrent calls to the GROBID services. Given the limits of
ThreadPoolExecutor (input stored in memory, blocking Executor.map until the
whole input is acquired), it works with batches of PDF of a size indicated
in the config.json file (default is 1000 entries). We are moving from first
batch to the second one only when the first is entirely processed - which
means it is slightly sub-optimal, but should scale better. Working without
batch would mean acquiring a list of millions of files in directories and
would require something scalable too (e.g. done in a separate thread),
which is not implemented for the moment.
"""
import os
import io
import json
import argparse
import time
import concurrent.futures
import ntpath
import requests
import pathlib
from .client import ApiClient
class ServerUnavailableException(Exception):
pass
class GrobidClient(ApiClient):
def __init__(self, grobid_server='localhost',
batch_size=1000,
coordinates=["persName", "figure", "ref", "biblStruct", "formula", "s", "note", "title"],
sleep_time=5,
timeout=60,
config_path=None,
check_server=True):
self.config = {
'grobid_server': grobid_server,
'batch_size': batch_size,
'coordinates': coordinates,
'sleep_time': sleep_time,
'timeout': timeout
}
if config_path:
self._load_config(config_path)
if check_server:
self._test_server_connection()
def _load_config(self, path="./config.json"):
"""
Load the json configuration
"""
config_json = open(path).read()
self.config = json.loads(config_json)
def _test_server_connection(self):
"""Test if the server is up and running."""
the_url = self.get_server_url("isalive")
try:
r = requests.get(the_url)
except:
print("GROBID server does not appear up and running, the connection to the server failed")
raise ServerUnavailableException
status = r.status_code
if status != 200:
print("GROBID server does not appear up and running " + str(status))
return False, status
else:
print("GROBID server is up and running")
return True, status
def _output_file_name(self, input_file, input_path, output):
# we use ntpath here to be sure it will work on Windows too
if output is not None:
input_file_name = str(os.path.relpath(os.path.abspath(input_file), input_path))
filename = os.path.join(
output, os.path.splitext(input_file_name)[0] + ".grobid.tei.xml"
)
else:
input_file_name = ntpath.basename(input_file)
filename = os.path.join(
ntpath.dirname(input_file),
os.path.splitext(input_file_name)[0] + ".grobid.tei.xml",
)
return filename
def ping(self) -> (bool, int):
"""
Check the Grobid service. Returns True if the service is up.
In addition, returns also the status code.
"""
return self._test_server_connection()
def process(
self,
service,
input_path,
output=None,
n=10,
generateIDs=False,
consolidate_header=True,
consolidate_citations=False,
include_raw_citations=False,
include_raw_affiliations=False,
tei_coordinates=False,
segment_sentences=False,
force=True,
verbose=False,
):
batch_size_pdf = self.config["batch_size"]
input_files = []
for (dirpath, dirnames, filenames) in os.walk(input_path):
for filename in filenames:
if filename.endswith(".pdf") or filename.endswith(".PDF") or \
(service == 'processCitationList' and (filename.endswith(".txt") or filename.endswith(".TXT"))) or \
(service == 'processCitationPatentST36' and (filename.endswith(".xml") or filename.endswith(".XML"))):
if verbose:
try:
print(filename)
except Exception:
# may happen on linux see https://stackoverflow.com/questions/27366479/python-3-os-walk-file-paths-unicodeencodeerror-utf-8-codec-cant-encode-s
pass
input_files.append(os.sep.join([dirpath, filename]))
if len(input_files) == batch_size_pdf:
self.process_batch(
service,
input_files,
input_path,
output,
n,
generateIDs,
consolidate_header,
consolidate_citations,
include_raw_citations,
include_raw_affiliations,
tei_coordinates,
segment_sentences,
force,
verbose,
)
input_files = []
# last batch
if len(input_files) > 0:
self.process_batch(
service,
input_files,
input_path,
output,
n,
generateIDs,
consolidate_header,
consolidate_citations,
include_raw_citations,
include_raw_affiliations,
tei_coordinates,
segment_sentences,
force,
verbose,
)
def process_batch(
self,
service,
input_files,
input_path,
output,
n,
generateIDs,
consolidate_header,
consolidate_citations,
include_raw_citations,
include_raw_affiliations,
tei_coordinates,
segment_sentences,
force,
verbose=False,
):
if verbose:
print(len(input_files), "files to process in current batch")
# we use ThreadPoolExecutor and not ProcessPoolExecutor because it is an I/O intensive process
with concurrent.futures.ThreadPoolExecutor(max_workers=n) as executor:
#with concurrent.futures.ProcessPoolExecutor(max_workers=n) as executor:
results = []
for input_file in input_files:
# check if TEI file is already produced
filename = self._output_file_name(input_file, input_path, output)
if not force and os.path.isfile(filename):
print(filename, "already exist, skipping... (use --force to reprocess pdf input files)")
continue
selected_process = self.process_pdf
if service == 'processCitationList':
selected_process = self.process_txt
r = executor.submit(
selected_process,
service,
input_file,
generateIDs,
consolidate_header,
consolidate_citations,
include_raw_citations,
include_raw_affiliations,
tei_coordinates,
segment_sentences)
results.append(r)
for r in concurrent.futures.as_completed(results):
input_file, status, text = r.result()
filename = self._output_file_name(input_file, input_path, output)
if status != 200 or text is None:
print("Processing of", input_file, "failed with error", str(status), ",", text)
# writing error file with suffixed error code
try:
pathlib.Path(os.path.dirname(filename)).mkdir(parents=True, exist_ok=True)
with open(filename.replace(".grobid.tei.xml", "_"+str(status)+".txt"), 'w', encoding='utf8') as tei_file:
if text is not None:
tei_file.write(text)
else:
tei_file.write("")
except OSError:
print("Writing resulting TEI XML file", filename, "failed")
else:
# writing TEI file
try:
pathlib.Path(os.path.dirname(filename)).mkdir(parents=True, exist_ok=True)
with open(filename,'w',encoding='utf8') as tei_file:
tei_file.write(text)
except OSError:
print("Writing resulting TEI XML file", filename, "failed")
def process_pdf(
self,
service,
pdf_file,
generateIDs,
consolidate_header,
consolidate_citations,
include_raw_citations,
include_raw_affiliations,
tei_coordinates,
segment_sentences,
start=-1,
end=-1
):
pdf_handle = open(pdf_file, "rb")
files = {
"input": (
pdf_file,
pdf_handle,
"application/pdf",
{"Expires": "0"},
)
}
the_url = self.get_server_url(service)
# set the GROBID parameters
the_data = {}
if generateIDs:
the_data["generateIDs"] = "1"
if consolidate_header:
the_data["consolidateHeader"] = "1"
if consolidate_citations:
the_data["consolidateCitations"] = "1"
if include_raw_citations:
the_data["includeRawCitations"] = "1"
if include_raw_affiliations:
the_data["includeRawAffiliations"] = "1"
if tei_coordinates:
the_data["teiCoordinates"] = self.config["coordinates"]
if segment_sentences:
the_data["segmentSentences"] = "1"
if start > 0:
the_data["start"] = str(start)
if end > 0:
the_data["end"] = str(end)
try:
res, status = self.post(
url=the_url, files=files, data=the_data, headers={"Accept": "text/plain"}, timeout=self.config['timeout']
)
if status == 503:
time.sleep(self.config["sleep_time"])
return self.process_pdf(
service,
pdf_file,
generateIDs,
consolidate_header,
consolidate_citations,
include_raw_citations,
include_raw_affiliations,
tei_coordinates,
segment_sentences,
start,
end
)
except requests.exceptions.ReadTimeout:
pdf_handle.close()
return (pdf_file, 408, None)
pdf_handle.close()
return (pdf_file, status, res.text)
def get_server_url(self, service):
return self.config['grobid_server'] + "/api/" + service
def process_txt(
self,
service,
txt_file,
generateIDs,
consolidate_header,
consolidate_citations,
include_raw_citations,
include_raw_affiliations,
tei_coordinates,
segment_sentences
):
# create request based on file content
references = None
with open(txt_file) as f:
references = [line.rstrip() for line in f]
the_url = self.get_server_url(service)
# set the GROBID parameters
the_data = {}
if consolidate_citations:
the_data["consolidateCitations"] = "1"
if include_raw_citations:
the_data["includeRawCitations"] = "1"
the_data["citations"] = references
res, status = self.post(
url=the_url, data=the_data, headers={"Accept": "application/xml"}
)
if status == 503:
time.sleep(self.config["sleep_time"])
return self.process_txt(
service,
txt_file,
generateIDs,
consolidate_header,
consolidate_citations,
include_raw_citations,
include_raw_affiliations,
tei_coordinates,
segment_sentences
)
return (txt_file, status, res.text)
def main():
valid_services = [
"processFulltextDocument",
"processHeaderDocument",
"processReferences",
"processCitationList",
"processCitationPatentST36",
"processCitationPatentPDF"
]
parser = argparse.ArgumentParser(description="Client for GROBID services")
parser.add_argument(
"service",
help="one of " + str(valid_services),
)
parser.add_argument(
"--input", default=None, help="path to the directory containing files to process: PDF or .txt (for processCitationList only, one reference per line), or .xml for patents in ST36"
)
parser.add_argument(
"--output",
default=None,
help="path to the directory where to put the results (optional)",
)
parser.add_argument(
"--config",
default="./config.json",
help="path to the config file, default is ./config.json",
)
parser.add_argument("--n", default=10, help="concurrency for service usage")
parser.add_argument(
"--generateIDs",
action="store_true",
help="generate random xml:id to textual XML elements of the result files",
)
parser.add_argument(
"--consolidate_header",
action="store_true",
help="call GROBID with consolidation of the metadata extracted from the header",
)
parser.add_argument(
"--consolidate_citations",
action="store_true",
help="call GROBID with consolidation of the extracted bibliographical references",
)
parser.add_argument(
"--include_raw_citations",
action="store_true",
help="call GROBID requesting the extraction of raw citations",
)
parser.add_argument(
"--include_raw_affiliations",
action="store_true",
help="call GROBID requestiong the extraciton of raw affiliations",
)
parser.add_argument(
"--force",
action="store_true",
help="force re-processing pdf input files when tei output files already exist",
)
parser.add_argument(
"--teiCoordinates",
action="store_true",
help="add the original PDF coordinates (bounding boxes) to the extracted elements",
)
parser.add_argument(
"--segmentSentences",
action="store_true",
help="segment sentences in the text content of the document with additional <s> elements",
)
parser.add_argument(
"--verbose",
action="store_true",
help="print information about processed files in the console",
)
args = parser.parse_args()
input_path = args.input
config_path = args.config
output_path = args.output
if args.n is not None:
try:
n = int(args.n)
except ValueError:
print("Invalid concurrency parameter n:", n, ", n = 10 will be used by default")
pass
# if output path does not exist, we create it
if output_path is not None and not os.path.isdir(output_path):
try:
print("output directory does not exist but will be created:", output_path)
os.makedirs(output_path)
except OSError:
print("Creation of the directory", output_path, "failed")
else:
print("Successfully created the directory", output_path)
service = args.service
generateIDs = args.generateIDs
consolidate_header = args.consolidate_header
consolidate_citations = args.consolidate_citations
include_raw_citations = args.include_raw_citations
include_raw_affiliations = args.include_raw_affiliations
force = args.force
tei_coordinates = args.teiCoordinates
segment_sentences = args.segmentSentences
verbose = args.verbose
if service is None or not service in valid_services:
print("Missing or invalid service, must be one of", valid_services)
exit(1)
try:
client = GrobidClient(config_path=config_path)
except ServerUnavailableException:
exit(1)
start_time = time.time()
client.process(
service,
input_path,
output=output_path,
n=n,
generateIDs=generateIDs,
consolidate_header=consolidate_header,
consolidate_citations=consolidate_citations,
include_raw_citations=include_raw_citations,
include_raw_affiliations=include_raw_affiliations,
tei_coordinates=tei_coordinates,
segment_sentences=segment_sentences,
force=force,
verbose=verbose,
)
runtime = round(time.time() - start_time, 3)
print("runtime: %s seconds " % (runtime))
if __name__ == "__main__":
main()