forked from nlpcl-lab/ace2005-preprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
229 lines (184 loc) · 9.44 KB
/
main.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
import os
import copy
import re
from parser import Parser
import json
from stanfordcorenlp import StanfordCoreNLP
import argparse
from tqdm import tqdm
import traceback
def get_data_paths(ace2005_path):
test_files, dev_files, train_files = [], [], []
with open('./data_list.csv', mode='r') as csv_file:
rows = csv_file.readlines()
for row in rows[1:]:
items = row.replace('\n', '').split(',')
data_type = items[0]
name = items[1]
path = os.path.join(ace2005_path, name)
if data_type == 'test':
test_files.append(path)
elif data_type == 'dev':
dev_files.append(path)
elif data_type == 'train':
train_files.append(path)
return test_files, dev_files, train_files
def find_token_index(tokens, start_pos, end_pos, phrase):
start_idx, end_idx = -1, -1
for idx, token in enumerate(tokens):
if token['characterOffsetBegin'] <= start_pos:
start_idx = idx
assert start_idx != -1, "start_idx: {}, start_pos: {}, phrase: {}, tokens: {}".format(start_idx, start_pos, phrase, tokens)
chars = ''
def remove_punc(s):
s = re.sub(r'[^\w]', '', s)
return s
for i in range(0, len(tokens) - start_idx):
chars += remove_punc(tokens[start_idx + i]['originalText'])
if remove_punc(phrase) in chars:
end_idx = start_idx + i + 1
break
assert end_idx != -1, "end_idx: {}, end_pos: {}, phrase: {}, tokens: {}, chars:{}".format(end_idx, end_pos, phrase, tokens, chars)
return start_idx, end_idx
def verify_result(data):
def remove_punctuation(s):
for c in ['-LRB-', '-RRB-', '-LSB-', '-RSB-', '-LCB-', '-RCB-', '\xa0']:
s = s.replace(c, '')
s = re.sub(r'[^\w]', '', s)
return s
def check_diff(words, phrase):
return remove_punctuation(phrase) not in remove_punctuation(words)
for item in data:
words = item['words']
for entity_mention in item['golden-entity-mentions']:
if check_diff(''.join(words[entity_mention['start']:entity_mention['end']]), entity_mention['text'].replace(' ', '')):
print('============================')
print('[Warning] entity has invalid start/end')
print('Expected: ', entity_mention['text'])
print('Actual:', words[entity_mention['start']:entity_mention['end']])
print('start: {}, end: {}, words: {}'.format(entity_mention['start'], entity_mention['end'], words))
for event_mention in item['golden-event-mentions']:
trigger = event_mention['trigger']
if check_diff(''.join(words[trigger['start']:trigger['end']]), trigger['text'].replace(' ', '')):
print('============================')
print('[Warning] trigger has invalid start/end')
print('Expected: ', trigger['text'])
print('Actual:', words[trigger['start']:trigger['end']])
print('start: {}, end: {}, words: {}'.format(trigger['start'], trigger['end'], words))
for argument in event_mention['arguments']:
if check_diff(''.join(words[argument['start']:argument['end']]), argument['text'].replace(' ', '')):
print('============================')
print('[Warning] argument has invalid start/end')
print('Expected: ', argument['text'])
print('Actual:', words[argument['start']:argument['end']])
print('start: {}, end: {}, words: {}'.format(argument['start'], argument['end'], words))
print('Complete verification')
def preprocessing(data_type, files):
result = []
event_count, entity_count, sent_count, argument_count = 0, 0, 0, 0
print('=' * 20)
print('[preprocessing] type: ', data_type)
for file in tqdm(files):
parser = Parser(path=file)
entity_count += len(parser.entity_mentions)
event_count += len(parser.event_mentions)
sent_count += len(parser.sents_with_pos)
for item in parser.get_data():
data = dict()
data['sentence'] = item['sentence']
data['golden-entity-mentions'] = []
data['golden-event-mentions'] = []
try:
nlp_res_raw = nlp.annotate(item['sentence'], properties={'annotators': 'tokenize,ssplit,pos,lemma,parse'})
nlp_res = json.loads(nlp_res_raw)
except Exception as e:
print('[Warning] StanfordCore Exception: ', nlp_res_raw, 'This sentence will be ignored.')
print('If you want to include all sentences, please refer to this issue: https://github.com/nlpcl-lab/ace2005-preprocessing/issues/1')
continue
tokens = nlp_res['sentences'][0]['tokens']
if len(nlp_res['sentences']) >= 2:
# TODO: issue where the sentence segmentation of NTLK and StandfordCoreNLP do not match
# This error occurred so little that it was temporarily ignored (< 20 sentences).
continue
data['stanford-colcc'] = []
for dep in nlp_res['sentences'][0]['enhancedPlusPlusDependencies']:
data['stanford-colcc'].append('{}/dep={}/gov={}'.format(dep['dep'], dep['dependent'] - 1, dep['governor'] - 1))
data['words'] = list(map(lambda x: x['word'], tokens))
data['pos-tags'] = list(map(lambda x: x['pos'], tokens))
data['lemma'] = list(map(lambda x: x['lemma'], tokens))
data['parse'] = nlp_res['sentences'][0]['parse']
sent_start_pos = item['position'][0]
for entity_mention in item['golden-entity-mentions']:
position = entity_mention['position']
start_idx, end_idx = find_token_index(
tokens=tokens,
start_pos=position[0] - sent_start_pos,
end_pos=position[1] - sent_start_pos + 1,
phrase=entity_mention['text'],
)
entity_mention['start'] = start_idx
entity_mention['end'] = end_idx
del entity_mention['position']
# head
head_position = entity_mention["head"]["position"]
head_start_idx, head_end_idx = find_token_index(
tokens=tokens,
start_pos=head_position[0] - sent_start_pos,
end_pos=head_position[1] - sent_start_pos + 1,
phrase=entity_mention["head"]["text"]
)
entity_mention["head"]["start"] = head_start_idx
entity_mention["head"]["end"] = head_end_idx
del entity_mention["head"]["position"]
data['golden-entity-mentions'].append(entity_mention)
for event_mention in item['golden-event-mentions']:
# same event mention can be shared
event_mention = copy.deepcopy(event_mention)
position = event_mention['trigger']['position']
start_idx, end_idx = find_token_index(
tokens=tokens,
start_pos=position[0] - sent_start_pos,
end_pos=position[1] - sent_start_pos + 1,
phrase=event_mention['trigger']['text'],
)
event_mention['trigger']['start'] = start_idx
event_mention['trigger']['end'] = end_idx
del event_mention['trigger']['position']
del event_mention['position']
arguments = []
argument_count += len(event_mention['arguments'])
for argument in event_mention['arguments']:
position = argument['position']
start_idx, end_idx = find_token_index(
tokens=tokens,
start_pos=position[0] - sent_start_pos,
end_pos=position[1] - sent_start_pos + 1,
phrase=argument['text'],
)
argument['start'] = start_idx
argument['end'] = end_idx
del argument['position']
arguments.append(argument)
event_mention['arguments'] = arguments
data['golden-event-mentions'].append(event_mention)
result.append(data)
print('======[Statistics]======')
print('sent :', sent_count)
print('event :', event_count)
print('entity :', entity_count)
print('argument:', argument_count)
verify_result(result)
with open('output/{}.json'.format(data_type), 'w') as f:
json.dump(result, f, indent=2)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--data', help="Path of ACE2005 English data", default='./data/ace_2005_td_v7/data/English')
parser.add_argument('--nlp', help="Standford Core Nlp path", default='./stanford-corenlp-full-2018-10-05')
args = parser.parse_args()
test_files, dev_files, train_files = get_data_paths(args.data)
with StanfordCoreNLP(args.nlp, memory='8g', timeout=60000) as nlp:
# res = nlp.annotate('Donald John Trump is current president of the United States.', properties={'annotators': 'tokenize,ssplit,pos,lemma,parse'})
# print(res)
preprocessing('dev', dev_files)
preprocessing('test', test_files)
preprocessing('train', train_files)