This repository has been archived by the owner on Dec 20, 2024. It is now read-only.
forked from kermitt2/grobid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreference_segmenter_data_update.py
72 lines (49 loc) · 1.81 KB
/
reference_segmenter_data_update.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 14 11:00:20 2022
@author: bryder
"""
import xml.etree.ElementTree as ET
import sys
from shutil import copyfile
import json
def main(argv):
json_file = argv[0]
reference_data = []
i = 0
with open(json_file,'r') as f:
for line in f:
reference_data.append(json.loads(line, strict=False))
i+=1
print(i)
input_dir = argv[1]
output_dir = argv[2]
for item in reference_data:
print(item['item_id'])
item_id = item['item_id']
file_path = input_dir + item_id + '.training.references.referenceSegmenter.tei.xml'
citations = item['raw_citations']
try:
tree = ET.parse(file_path)
except:
print(f"Error Parsing File {item_id}")
continue
root = tree.getroot()
text = root.find('text')
bib_list = text.find('listBibl')
for item in bib_list.findall('bibl'):
bib_list.remove(item)
i = 1
for citation in citations:
label = str(i) + '.'
new_bibl = ET.SubElement(bib_list,'bibl')
new_label = ET.SubElement(new_bibl,'label')
new_label.text = label
ref_text = ET.SubElement(new_bibl,'lb')
ref_text.text = citation
i += 1
tree.write(output_dir + 'tei/' + item_id + ".training.references.referenceSegmenter.tei.xml")
copyfile(input_dir + item_id + '.training.references.referenceSegmenter',output_dir + 'raw/' + item_id + '.training.references.referenceSegmenter')
if __name__ == "__main__":
main(sys.argv[1:])