-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvua_preprocessing.py
280 lines (260 loc) · 8.27 KB
/
vua_preprocessing.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
import os
from os.path import join
import csv
import configparser
import re
import lxml.etree as ET
import pandas as pd
TRAINING_PARTION = [
'a1e-fragment01',
'a1f-fragment06',
'a1f-fragment07',
'a1f-fragment08',
'a1f-fragment09',
'a1f-fragment10',
'a1f-fragment11',
'a1f-fragment12',
'a1g-fragment26',
'a1g-fragment27',
'a1h-fragment05',
'a1h-fragment06',
'a1j-fragment34',
'a1k-fragment02',
'a1l-fragment01',
'a1m-fragment01',
'a1n-fragment09',
'a1n-fragment18',
'a1p-fragment01',
'a1p-fragment03',
'a1x-fragment03',
'a1x-fragment04',
'a1x-fragment05',
'a2d-fragment05',
'a38-fragment01',
'a39-fragment01',
'a3c-fragment05',
'a3e-fragment03',
'a3k-fragment11',
'a3p-fragment09',
'a4d-fragment02',
'a6u-fragment02',
'a7s-fragment03',
'a7y-fragment03',
'a80-fragment15',
'a8m-fragment02',
'a8n-fragment19',
'a8r-fragment02',
'a8u-fragment14',
'a98-fragment03',
'a9j-fragment01',
'ab9-fragment03',
'ac2-fragment06',
'acj-fragment01',
'ahb-fragment51',
'ahc-fragment60',
'ahf-fragment24',
'ahf-fragment63',
'ahl-fragment02',
'ajf-fragment07',
'al0-fragment06',
'al2-fragment23',
'al5-fragment03',
'alp-fragment01',
'amm-fragment02',
'as6-fragment01',
'as6-fragment02',
'b1g-fragment02',
'bpa-fragment14',
'c8t-fragment01',
'cb5-fragment02',
'ccw-fragment03',
'cdb-fragment02',
'cdb-fragment04',
'clp-fragment01',
'crs-fragment01',
'ea7-fragment03',
'ew1-fragment01',
'fef-fragment03',
'fet-fragment01',
'fpb-fragment01',
'g0l-fragment01',
'kb7-fragment10',
'kbc-fragment13',
'kbd-fragment07',
'kbh-fragment01',
'kbh-fragment02',
'kbh-fragment03',
'kbh-fragment09',
'kbh-fragment41',
'kbj-fragment17',
'kbp-fragment09',
'kbw-fragment04',
'kbw-fragment11',
'kbw-fragment17',
'kbw-fragment42',
'kcc-fragment02',
'kcf-fragment14',
'kcu-fragment02',
'kcv-fragment42']
TESTING_PARTION = [
'a1j-fragment33',
'a1u-fragment04',
'a31-fragment03',
'a36-fragment07',
'a3e-fragment02',
'a3m-fragment02',
'a5e-fragment06',
'a7t-fragment01',
'a7w-fragment01',
'aa3-fragment08',
'ahc-fragment61',
'ahd-fragment06',
'ahe-fragment03',
'al2-fragment16',
'b17-fragment02',
'bmw-fragment09',
'ccw-fragment04',
'clw-fragment01',
'cty-fragment03',
'ecv-fragment05',
'faj-fragment17',
'kb7-fragment31',
'kb7-fragment45',
'kb7-fragment48',
'kbd-fragment21',
'kbh-fragment04',
'kbw-fragment09']
def read_config(configFilename):
parser = configparser.ConfigParser()
parser.read(configFilename)
xml_file = parser['params']['xml_file']
functions = set(parser['params']['functions'].split(','))
types = set(parser['params']['types'].split(','))
subtypes = set(parser['params']['subtypes'].split(','))
function_override = bool(
parser['params']['function_override'].lower() == 'true')
return xml_file, functions, types, subtypes, function_override
def is_metaphor(seg, functions, types, subtypes, function_override):
if seg is not None:
if seg.get('function') in functions:
if not function_override:
return 1
elif function_override and (seg.get('type') in types or seg.get('subtype') in subtypes):
return 1
else:
return 0
else:
return 0
else:
return 0
def handle_anomaly(txt_id, sentence_id):
if txt_id == 'as6-fragment01' and sentence_id == '26':
return 'M_to'
if txt_id == 'as6-fragment01' and sentence_id == '89':
return 'M_sector'
if txt_id == 'kb7-fragment48' and sentence_id == '13368':
return 'like'
def extract_xml_tag_text(txt_id, sentence_id, namespace, t, functions, types, subtypes, function_override):
final_token = None
segs = t.findall('./' + namespace + 'seg')
if len(segs) > 0:
for seg in segs:
if seg.text is None:
return handle_anomaly(txt_id, sentence_id)
flag = is_metaphor(seg, functions, types, subtypes, function_override)
temp_token = seg.text.strip()
temp_token = re.sub('[\[\]]', '', temp_token) # replace non-word
if flag == 1:
temp_token = 'M_' + temp_token
temp_token = re.sub(' +', ' M_', temp_token)
prefix = t.text
if prefix:
temp_token = prefix.strip() + ' ' + temp_token
suffix = seg.tail
if suffix:
temp_token = temp_token + ' ' + suffix.strip()
temp_token = re.sub(' +', ' ', temp_token)
if final_token:
final_token += temp_token.strip()
else:
final_token = temp_token.strip()
else:
try:
final_token = t.text.strip()
# replace non-word
final_token = re.sub('[\[\]]', '', final_token)
final_token = re.sub(' +', ' ', final_token)
except:
pass
if final_token and re.search('-', final_token) and re.search('M_', final_token):
final_token = re.sub(' ', '', final_token)
final_token = re.sub('M_', '', final_token)
final_token = 'M_' + final_token
if final_token and len(final_token.split()) > 1 and len(t.get('lemma').split()) == 1:
final_token = re.sub(' ', '', final_token)
# cleaning corrupted tokens due to annotator errors
if final_token and re.search('^>[A-Za-z]+', final_token):
final_token = re.sub('>', '', final_token)
if final_token and re.search('^<[A-Za-z]+', final_token):
final_token = re.sub('<', '', final_token)
if final_token and re.search('^=[A-Za-z]+', final_token):
final_token = re.sub('=', '', final_token)
if final_token and re.search('^/[A-Za-z]+', final_token):
final_token = re.sub('/', '', final_token)
return final_token
def process_sentence(txt_id, sentence, tei_namespace, functions, types, subtypes, function_override):
sentence_id = sentence.get('n')
tokens_lst = []
tokens = sentence.findall('*')
for t in tokens:
# special handling of cases with embedded words/puncts within a <hi></hi> pair of tags
if t.tag == tei_namespace + 'hi':
subTokens = t.findall('*')
for st in subTokens:
token_text = extract_xml_tag_text(txt_id, sentence_id, tei_namespace, st, functions, types, subtypes, function_override)
if token_text is None or token_text == '':
continue
tokens_lst.append(token_text.strip())
continue # done for this tag pair, continue
token_text = extract_xml_tag_text(txt_id, sentence_id, tei_namespace, t, functions, types, subtypes, function_override)
# skips empty, non-meaningful tokens
if token_text is None or token_text == '':
continue
tokens_lst.append(token_text.strip())
return sentence_id, ' '.join(tokens_lst)
def extract_xml(xml_file, functions, types, subtypes, function_override):
tei_namespace = '{http://www.tei-c.org/ns/1.0}'
xml_namespace = '{http://www.w3.org/XML/1998/namespace}'
tree = ET.parse(xml_file)
root = tree.getroot()
texts = root.findall('./' + tei_namespace + 'text/' + tei_namespace + 'group/' + tei_namespace + 'text')
id = []
sentence = []
word = []
label = []
for txt in texts:
txt_id = txt.attrib[xml_namespace + 'id']
# if txt_id not in TRAINING_PARTION:
if txt_id not in TESTING_PARTION:
continue
sents = txt.findall('.//' + tei_namespace + 's')
for s in sents:
sentence_id, sentence_txt = process_sentence(txt_id, s, tei_namespace, functions, types, subtypes, function_override)
tokens = sentence_txt.strip().split()
offset_id = 1
for t in tokens:
id.extend(['_'.join((txt_id, str(sentence_id), str(offset_id)))])
sentence.extend([re.sub('M_', '', sentence_txt)])
word.extend([re.sub('M_', '', t)])
label.extend([1 if "M_" in t else 0])
offset_id += 1
return id, sentence, word, label
def main():
xml_file, functions, types, subtypes, function_override = read_config('setup.cfg')
xml_file = "./data/VUA/raw/VUAMC.xml"
id, sentence, word, label = extract_xml(xml_file, functions, types, subtypes, function_override)
data = pd.DataFrame({"id": id, "sentence": sentence, "word": word, "label": label})
data.to_csv('./data/VUA/VUA_train.csv', index=False)
data.to_csv('./data/VUA/VUA_test_all.csv', index=False)
if __name__ == '__main__':
main()