-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprep_pages_dict_xml.py
58 lines (48 loc) · 1.64 KB
/
prep_pages_dict_xml.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
import xml.etree.ElementTree as ET
import json
import os
dict_xml = 'path/to/Bridge_A-AG_clean_aligned.xml'
tree_dict = ET.parse(dict_xml)
root_dict = tree_dict.getroot()
tokens_dict = list(root_dict.iter('TOKEN'))
tokens_annot = []
annotated_dir = 'path/to/dir'
for i in range(310):
entry_xml = os.path.join(annotated_dir, 'entry_{}.xml'.format(i))
try:
tree_entry = ET.parse(entry_xml)
parent_map = {c:p for p in tree_entry.iter() for c in p}
root_entry = tree_entry.getroot()
tokens_cur = list(root_entry.iter('TOKEN'))
for idx, token in enumerate(tokens_cur):
parent = parent_map[token]
token.attrib['parent'] = parent.tag
tokens_annot += tokens_cur
except:
print("error in file " + entry_xml)
continue
pages_data = []
feats = []
labels = []
page_n = 1
for idx, token in enumerate(tokens_dict):
page_t = int(token.attrib['page'])
if page_t != page_n:
pages_data.append((feats,labels))
feats = []
labels = []
page_n = page_t
if not token.text == tokens_annot[idx].text:
print("misaligned data at idx {}! Breaking...".format(idx))
break
feat = ['SIZE='+token.attrib['font-size'],'BOLD='+token.attrib['bold'],'ITALIC='+token.attrib['italic'],'FONT='+token.attrib['font-name'],'TOKEN='+token.text]
# TODO add newlines and such
parent = tokens_annot[idx].attrib['parent']
if parent == "dictScrap":
label = "SCRAP"
else:
label = "ENTRY"
feats.append(feat)
labels.append(label)
json_file = 'path/to/pages_data.json'
json.dump(pages_data,open(json_file,'w'),indent=4)