forked from ShuyangCao/open-ended_question_ontology
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_question_template.py
285 lines (215 loc) · 10.7 KB
/
build_question_template.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
import json
import argparse
import os
import numpy as np
from collections import defaultdict
from concurrent.futures import ProcessPoolExecutor
import string
import tempfile
from sklearn.metrics.pairwise import cosine_similarity
import subprocess as sp
puncts = set(string.punctuation)
SPLIT_SIZE = 60000
with open('stopwords') as f:
stopwords = set([line.strip() for line in f])
concept_net_word_vectors = {}
with open('numberbatch-en-19.08.txt') as f:
for line in f:
tmp = line.strip().split()
word = tmp[0]
vector = [float(v) for v in tmp[1:]]
concept_net_word_vectors[word] = np.array(vector)
def abstract_ne(sents):
for sent in sents:
sent_words = sent['words']
entity_mentions = sent['entity_mentions']
for mention in entity_mentions:
ner_type = mention[0]
ner_start = mention[1]
ner_end = mention[2]
if (ner_end - ner_start) == 1 and sent_words[ner_start]['word'] in puncts and sent_words[ner_start]['pos'] in ['DT', 'CC', 'IN']:
continue
if ner_type == 'DATE' and (ner_end - ner_start) == 1 and (sent_words[ner_start]['lemma'] in
['once', 'present', 'fall', 'supper', 'about', 'Falls', 'Fall', 'Once']):
continue
for i in range(ner_start, ner_end):
sent_words[i]['abstract'] = True
return sents
def abstract_overlap(sents, answer_words):
for sent in sents:
sent_words = sent['words']
for i, word in enumerate(sent_words):
lower_word = word['lemma'].lower()
if lower_word not in stopwords and lower_word not in puncts and lower_word in answer_words:
word['abstract'] = True
return sents
def update_replaced_constituency(old_constituency, new_constituency):
if old_constituency is None:
return new_constituency
old_type, old_start, old_end = old_constituency
new_type, new_start, new_end = new_constituency
if new_start <= old_start and new_end >= old_end:
return new_constituency
return old_constituency
def abstract_constituent(sents):
for sent in sents:
sent_words = sent['words']
constituency_spans = sent['constituency_spans']
head2span = defaultdict(list)
block_spans = []
all_block_spans = []
for ctype, start, end, head in constituency_spans:
if ctype in ['NP', 'ADJP', 'ADVP']:
head2span[head].append((ctype, start, end))
elif ctype in ['S', 'SQ', 'SBAR']:
block_spans.append((start, end))
all_block_spans.append((start, end))
elif ctype == 'PP':
block_spans.append((start, end))
for i, word in enumerate(sent_words):
if word['abstract']:
if i in head2span:
for span in head2span[i]:
block_indices = []
if span[0] == 'NP':
span_start = span[1]
span_end = span[2]
for block_start, block_end in all_block_spans:
if block_start >= span_start and block_end <= span_end:
for j in range(block_start, block_end):
block_indices.append(j)
elif span[0] in ['ADJP', 'ADVP']:
span_start = span[1]
span_end = span[2]
for block_start, block_end in block_spans:
if block_start >= span_start and block_end <= span_end:
for j in range(block_start, block_end):
block_indices.append(j)
for j in range(span[1], span[2]):
if j in block_indices:
continue
sent_words[j]['replaced_constituency'] = update_replaced_constituency(
sent_words[j]['replaced_constituency'], span
)
sent_words[j]['abstract'] = True
return sents
def abstract_embedding_content_percent(sents, answer_words):
answer_word2vec_vectors = []
for answer_word, answer_lemma, answer_pos in zip(answer_words['words'], answer_words['lemmas'], answer_words['pos']):
if answer_lemma.lower() in stopwords or answer_lemma in puncts:
continue
if answer_word.lower() in concept_net_word_vectors:
answer_word2vec_vectors.append((answer_word, concept_net_word_vectors[answer_word.lower()]))
elif answer_lemma.lower() in concept_net_word_vectors:
answer_word2vec_vectors.append((answer_word, concept_net_word_vectors[answer_lemma.lower()]))
# merge into matrix
word2vec_words = [x[0] for x in answer_word2vec_vectors]
answer_word2vec_vecs = [x[1] for x in answer_word2vec_vectors]
answer_word2vec_matrix = np.stack(answer_word2vec_vecs)
if not word2vec_words:
return sents
cnt_content = 0
cnt_content_abstracted = 0
for sent in sents:
sent_words = sent['words']
for word in sent_words:
if word['lemma'].lower() in stopwords or word['lemma'].lower() in puncts:
continue
cnt_content += 1
if word['abstract']:
cnt_content_abstracted += 1
further_abstract = int(cnt_content * 0.8) - cnt_content_abstracted
if further_abstract <= 0:
return sents
all_words = []
for j, sent in enumerate(sents):
sent_words = sent['words']
for i, word in enumerate(sent_words):
if word['lemma'].lower() in stopwords or word['lemma'].lower() in puncts:
continue
q_word2vec_word_vec = None
if word['abstract']:
continue
if word['word'].lower() in concept_net_word_vectors:
q_word2vec_word_vec = np.expand_dims(concept_net_word_vectors[word['word'].lower()], 0)
elif word['lemma'].lower() in concept_net_word_vectors:
q_word2vec_word_vec = np.expand_dims(concept_net_word_vectors[word['lemma'].lower()], 0)
if q_word2vec_word_vec is not None and word2vec_words:
similarity_scores = cosine_similarity(q_word2vec_word_vec, answer_word2vec_matrix).squeeze()
max_id = np.argmax(similarity_scores).item()
all_words.append((j, i, similarity_scores[max_id].item()))
for sid, wid, _ in sorted(all_words, key=lambda x: x[2], reverse=True)[:further_abstract]:
sents[sid]['words'][wid]['abstract'] = True
return sents
def main():
parser = argparse.ArgumentParser()
parser.add_argument('question_converted_jsonl')
parser.add_argument('answer_jsonl')
parser.add_argument('tmp_dir')
parser.add_argument('out_jsonl')
args = parser.parse_args()
question_parses = []
with open(args.question_converted_jsonl) as f:
for line in f:
question_parses.append(json.loads(line))
ids = [question_parse['id'] for question_parse in question_parses]
question_parses = [question_parse['sents_converted'] for question_parse in question_parses]
print('Question loaded.')
for sents in question_parses:
for sent in sents:
for w in sent['words']:
w['replaced_constituency'] = None
answer_words = []
with open(args.answer_jsonl) as f:
for line in f:
dp = json.loads(line)
answer_words.append({'words': dp['doc_words'], 'lemmas': dp['doc_lemmas'], 'pos': dp['doc_pos']})
print('Answer loaded.')
with tempfile.TemporaryDirectory(dir=args.tmp_dir) as tmp_dir:
part_out_jsonls = []
for part_id, part_start in enumerate(range(0, len(ids), SPLIT_SIZE)):
print('===PART {}===='.format(part_id))
part_ids = ids[part_start: part_start + SPLIT_SIZE]
part_question_parses = question_parses[part_start: part_start + SPLIT_SIZE]
part_answer_words = answer_words[part_start: part_start + SPLIT_SIZE]
with ProcessPoolExecutor() as executor:
ne_abstracted = []
for question_parse in part_question_parses:
ne_abstracted.append(executor.submit(abstract_ne, question_parse))
ne_abstracted = [x.result() for x in ne_abstracted]
print('NE abstracted.')
overlap_abstracted = []
with ProcessPoolExecutor() as executor:
for ne_abs, answer_word in zip(ne_abstracted, part_answer_words):
overlap_abstracted.append(executor.submit(abstract_overlap, ne_abs, set([w.lower() for w in answer_word['lemmas']])))
overlap_abstracted = [x.result() for x in overlap_abstracted]
print('Overlap abstracted.')
constituent_abstracted = []
with ProcessPoolExecutor() as executor:
for overlap_abs in overlap_abstracted:
constituent_abstracted.append(executor.submit(abstract_constituent, overlap_abs))
constituent_abstracted = [x.result() for x in constituent_abstracted]
print('Constituent abstracted.')
overlap_abstracted = constituent_abstracted
embedding_abstracted = []
with ProcessPoolExecutor() as executor:
for overlap_abs, answer_word in zip(overlap_abstracted, part_answer_words):
embedding_abstracted.append(executor.submit(abstract_embedding_content_percent, overlap_abs, answer_word))
embedding_abstracted = [x.result() for x in embedding_abstracted]
print('Embedding abstracted.')
constituent_abstracted = []
with ProcessPoolExecutor() as executor:
for embedding_abs in embedding_abstracted:
constituent_abstracted.append(executor.submit(abstract_constituent, embedding_abs))
constituent_abstracted = [x.result() for x in constituent_abstracted]
print('Constituent abstracted.')
assert len(part_ids) == len(constituent_abstracted)
part_out_jsonl = os.path.join(tmp_dir, 'part{}'.format(part_id))
part_out_jsonls.append(part_out_jsonl)
with open(part_out_jsonl, 'w') as f:
for id, abs in zip(part_ids, constituent_abstracted):
f.write(json.dumps({'id': id, 'abs_sents': abs}) + '\n')
with open(args.out_jsonl, 'w') as f:
sp.run(['cat'] + part_out_jsonls, stdout=f)
if __name__ == '__main__':
main()