-
Notifications
You must be signed in to change notification settings - Fork 1
/
read_document.py
295 lines (212 loc) · 9.19 KB
/
read_document.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
import sys
import os
import os.path
from lxml import etree
import collections
import pickle
def create_folder(filepath):
directory = os.path.dirname(filepath)
if not os.path.exists(directory):
os.makedirs(directory)
def check_entry_dict(event_tokens, d):
if event_tokens in d:
return " ".join(d[event_tokens])
else:
return event_tokens
def get_sentence(num, all_token):
temp = []
for token in all_token:
if token[1] == num:
temp.append(token[-1])
return temp
def transfter_to_token(s, all_token):
tmp = []
for c in s.split('_'):
token = all_token[int(c) - 1]
tmp.append(token[-1])
return ' '.join(tmp)
def get_sentence_num(s, all_token):
c = s.split('_')[0]
return all_token[int(c)-1][1]
def generate_feature(s, t, value, all_token):
s_text = transfter_to_token(s, all_token)
t_text = transfter_to_token(t, all_token)
s_sentence = get_sentence(get_sentence_num(s, all_token), all_token)
t_sentence = get_sentence(get_sentence_num(t, all_token), all_token)
return s_text, t_text
def all_tokens(filename):
ecbplus = etree.parse(filename, etree.XMLParser(remove_blank_text=True))
root_ecbplus = ecbplus.getroot()
root_ecbplus.getchildren()
all_token = []
for elem in root_ecbplus.findall('token'):
temp = (elem.get('t_id'), elem.get('sentence'),
elem.get('number'), elem.text)
all_token.append(temp)
return all_token
def extract_event_CAT(etreeRoot):
"""
:param etreeRoot: ECB+/ESC XML root
:return: dictionary with annotaed events in ECB+
"""
event_dict = collections.defaultdict(list)
for elem in etreeRoot.findall('Markables/'):
if elem.tag.startswith("ACTION") or elem.tag.startswith("NEG_ACTION"):
for token_id in elem.findall('token_anchor'): # the event should have at least one token
event_mention_id = elem.get('m_id', 'nothing')
token_mention_id = token_id.get('t_id', 'nothing')
event_dict[event_mention_id].append(token_mention_id)
return event_dict
def extract_time_CAT(etreeRoot):
time_dict = collections.defaultdict(list)
for elem in etreeRoot.findall('Markables/'):
if elem.tag.startswith("TIME_DATE"):
for token_id in elem.findall('token_anchor'): # the event should have at least one token
event_mention_id = elem.get('m_id', 'nothing')
token_mention_id = token_id.get('t_id', 'nothing')
time_dict[event_mention_id].append(token_mention_id)
return time_dict
def extract_corefRelations(etreeRoot, d):
"""
:param etreeRoot: ECB+ XML root
:return: dictionary with annotaed events in ECB+ (event_dict)
:return:
"""
relations_dict_appo = collections.defaultdict(list)
relations_dict = {}
for elem in etreeRoot.findall('Relations/'):
target_element = elem.find('target').get('m_id', 'null') # the target is a non-event
for source in elem.findall('source'):
source_elem = source.get('m_id', 'null')
if source_elem in d:
val = "_".join(d[source_elem])
relations_dict_appo[target_element].append(val) # coreferential sets of events
for k, v in relations_dict_appo.items():
for i in v:
relations_dict[i] = v
return relations_dict
def extract_plotLink(etreeRoot, d):
"""
:param etreeRoot: ESC XML root
:param d: dictionary with annotaed events in ESC (event_dict)
:return:
"""
plot_dict = collections.defaultdict(list)
for elem in etreeRoot.findall('Relations/'):
if elem.tag == "PLOT_LINK":
source_pl = elem.find('source').get('m_id', 'null')
target_pl = elem.find('target').get('m_id', 'null')
relvalu = elem.get('relType', 'null')
if source_pl in d:
val1 = "_".join(d[source_pl])
if target_pl in d:
val2 = "_".join(d[target_pl])
plot_dict[(val1, val2)] = relvalu
return plot_dict
def extract_timeLink(etreeRoot, d):
tlink_dict = collections.defaultdict(list)
for elem in etreeRoot.findall('Relations/'):
if elem.tag == "TLINK":
try:
source_pl = elem.find('source').get('m_id', 'null')
target_pl = elem.find('target').get('m_id', 'null')
except:
continue
relvalu = elem.get('relType', 'null')
if source_pl in d:
val1 = "_".join(d[source_pl])
if target_pl in d:
val2 = "_".join(d[target_pl])
tlink_dict[(val1, val2)] = relvalu
return tlink_dict
def read_evaluation_file(fn):
res = []
if not os.path.exists(fn):
return res
for line in open(fn):
fileds = line.strip().split('\t')
res.append(fileds)
return res
def read_file(ecbplus_original, ecbstart_new, evaluate_file, evaluate_coref_file):
"""
:param ecbplus_original: ECB+ CAT data
:param ecbstart_new: ESC CAT data
:param outfile1: event mention extended
:param outfile2: event extended coref chain
:return:
"""
ecbplus = etree.parse(ecbplus_original, etree.XMLParser(remove_blank_text=True))
root_ecbplus = ecbplus.getroot()
root_ecbplus.getchildren()
ecb_event_mentions = extract_event_CAT(root_ecbplus)
ecb_coref_relations = extract_corefRelations(root_ecbplus, ecb_event_mentions)
"""
ecbstar data
"""
ecbstar = etree.parse(ecbstart_new, etree.XMLParser(remove_blank_text=True))
ecbstar_root = ecbstar.getroot()
ecbstar_root.getchildren()
ecb_star_events = extract_event_CAT(ecbstar_root)
ecbstar_events_plotLink = extract_plotLink(ecbstar_root, ecb_star_events)
ecb_star_time = extract_time_CAT(ecbstar_root)
ecb_star_time.update(ecb_star_events)
ecbstar_timelink = extract_timeLink(ecbstar_root, ecb_star_time)
evaluation_data = read_evaluation_file(evaluate_file)
evaluationcrof_data = read_evaluation_file(evaluate_coref_file)
# TLINK ??
# print(ecb_star_events) # all the events
# print(ecb_star_time) # all the time expressions
# print(ecbstar_events_plotLink) # direct event plot link
# print(ecbstar_timelink)
return ecb_star_events, ecb_coref_relations, ecb_star_time, ecbstar_events_plotLink, ecbstar_timelink, evaluation_data, evaluationcrof_data
def make_corpus(ecbtopic, ecbstartopic, evaluationtopic, evaluationcoreftopic, datadict):
"""
:param ecbtopic: ECB+ topic folder in CAT format
:param ecbstartopic: ESC topic folder in CAT format
:param outdir: output folder for evaluation data format
:return:
"""
if os.path.isdir(ecbtopic) and os.path.isdir(ecbstartopic) and os.path.isdir(evaluationtopic):
if ecbtopic[-1] != '/':
ecbtopic += '/'
if ecbstartopic[-1] != '/':
ecbstartopic += '/'
if evaluationtopic[-1] != '/':
evaluationtopic += '/'
if evaluationcoreftopic[-1] != '/':
evaluationcoreftopic += '/'
ecb_subfolder = os.path.dirname(ecbtopic).split("/")[-1]
for f in os.listdir(ecbtopic):
if f.endswith('plus.xml'):
ecb_file = f
star_file = ecbstartopic + f + ".xml"
evaluate_file = evaluationtopic + f
evaluate_coref_file = evaluationcoreftopic + f
ecb_star_events, ecb_coref_relations, ecb_star_time, ecbstar_events_plotLink, ecbstar_timelink, evaluation_data, evaluationcrof_data = read_file(ecbtopic + ecb_file, star_file, evaluate_file, evaluate_coref_file)
for key in ecb_star_events:
ecb_star_events[key] = '_'.join(ecb_star_events[key])
for key in ecb_star_time:
ecb_star_time[key] = '_'.join(ecb_star_time[key])
all_token = all_tokens(star_file)
datadict[star_file] = [all_token, ecb_star_events, ecb_coref_relations, ecb_star_time, ecbstar_events_plotLink, ecbstar_timelink, evaluation_data, evaluationcrof_data]
# for elem in ecbstar_events_plotLink:
# (s, t), value = elem, ecbstar_events_plotLink[elem]
# s_text, t_text = generate_feature(s, t, value, all_token)
# print(s_text, t_text, value)
def main(argv=None):
version = 'v0.9'
ECBplusTopic = 'ECB+_LREC2014/ECB+/'
ECBstarTopic = 'annotated_data/' + version +'/'
EvaluationTopic = 'evaluation_format/full_corpus/' + version +'/event_mentions_extended/'
EvaluationCrofTopic = 'evaluation_format/full_corpus/' + version +'/coref_chain/'
data_dict = {}
for topic in os.listdir('annotated_data/v1.0/'):
if os.path.isdir('annotated_data/v1.0/' + topic):
dir1, dir2, dir3, dir4 = ECBplusTopic + topic, ECBstarTopic + topic, EvaluationTopic + topic, EvaluationCrofTopic + topic
make_corpus(dir1, dir2, dir3, dir4, data_dict)
for key in data_dict:
print(key)
with open('document_raw.pickle', 'wb') as f:
pickle.dump(data_dict, f, pickle.HIGHEST_PROTOCOL)
if __name__ == '__main__':
main()