-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_extract.py
More file actions
54 lines (46 loc) · 1.39 KB
/
run_extract.py
File metadata and controls
54 lines (46 loc) · 1.39 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
import sys
import json
from pathlib import Path
from sdser import extract_from_pdf
def dump(path: str):
cas, ec, cas_name_map, ec_name_map, pairs = extract_from_pdf(path)
out = {
'CAS': cas,
'EC': ec,
'cas_name_map': cas_name_map,
'ec_name_map': ec_name_map,
'pairs': pairs,
}
# Ensure pairs are JSON-serializable lists
out['pairs'] = [[c, e] for (c, e) in out['pairs']]
print(json.dumps(out, indent=2, ensure_ascii=False))
def main():
if len(sys.argv) < 2:
print("Usage: python run_extract.py <path-to-pdf> [out.json]")
sys.exit(1)
pdf = sys.argv[1]
out_path = None
if len(sys.argv) >= 3:
out_path = Path(sys.argv[2])
s = None
try:
from sdser import extract_from_pdf
cas, ec, cas_name_map, ec_name_map, pairs = extract_from_pdf(pdf)
out = {
'CAS': cas,
'EC': ec,
'cas_name_map': cas_name_map,
'ec_name_map': ec_name_map,
'pairs': [[c, e] for (c, e) in pairs],
}
j = json.dumps(out, indent=2, ensure_ascii=False)
if out_path:
out_path.write_text(j, encoding='utf-8')
print(f"Wrote expected JSON to {out_path}")
else:
print(j)
except Exception as exc:
print("Error extracting:", exc)
raise
if __name__ == '__main__':
main()