-
Notifications
You must be signed in to change notification settings - Fork 1
/
expansion_to_phrases.py
295 lines (258 loc) · 10.9 KB
/
expansion_to_phrases.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 re
import string
import spacy
import textacy
from tqdm import tqdm
from config import *
from utils import load_json
relation_map = load_json("relation_map.json")
class ExpansionConverter:
"""
Converting commonsense expansions to phrases.
"""
def __init__(self):
self.nlp = spacy.load('en_core_web_md')
self.srl_predictor = "https://storage.googleapis.com/allennlp-public-models/structured-prediction-srl-bert.2020.12.15.tar.gz"
# object and subject constants
self.OBJECT_DEPS = {"dobj", "dative", "attr", "oprd"}
self.SUBJECT_DEPS = {"nsubj", "nsubjpass", "csubj", "agent", "expl"}
# Exclude these subjects
self.exclude_list = ['what is', 'what are', 'where', 'where is', 'where are', 'what',
'how are', 'how many', 'how is', 'how', 'where is', 'where are', 'where',
'when was', 'when is',
'which is', 'which are', 'can you', 'which', 'would the',
'is the', 'is this', 'why did', 'why is', 'are the', 'do', 'why']
self.atomic_relations = ["oEffect",
"oReact",
"oWant",
"xAttr",
"xEffect",
"xIntent",
"xNeed",
"xReact",
"xReason",
"xWant"]
self.excluded_relations = [
"CausesDesire",
"DefinedAs",
"DesireOf",
"HasFirstSubevent",
"HasLastSubevent",
"HasPainCharacter",
"HasPainIntensity",
"HasSubEvent",
"HasSubevent",
"HinderedBy",
"InheritsFrom",
"InstanceOf",
"MotivatedByGoal",
"NotDesires",
"NotHasA",
"NotIsA",
"ReceivesAction",
"RelatedTo",
"SymbolOf",
"isFilledBy",
]
def convert(self, sentence, exp, use_srl, question="", exclude_subject=False):
"""
:param sentence: actual sentence provided as input to COMET
:param exp: expansions of the sentences
:param srl: if srl should be used for generating person x
:return:
"""
context, top_context = [],[]
seen = set()
# lower case relations
excluded = [x.lower() for x in self.excluded_relations]
# personx: the subject from the sentence inputted to COMET
personx = ""
if question:
personx_q = self.get_personx(question.replace("_", ""))
personx = personx_q
if not question or not personx_q:
personx = self.get_personx(sentence.replace("_", ""))
for relation, beams in exp.items():
if relation.lower() not in excluded:
for beam in beams:
source = personx
if beam != " none":
target = beam.lstrip().translate(str.maketrans('', '', string.punctuation))
if relation in self.atomic_relations and not self.is_person(source):
continue
if target and target!=source and ("none" not in target) and (target not in seen) and (not self.lexical_overlap(seen,
target)):
if exclude_subject:
sent = relation_map[relation.lower()].replace("{0}", "").replace("{1}", target)
else:
sent = relation_map[relation.lower()].replace("{0}", source).replace("{1}", target) + "."
sent = sent.capitalize()
context.append(sent)
seen.add(target)
return [context, top_context]
def is_person(self, word):
if word:
living_beings_vocab = ["person", "people", "man", "woman", "girl", "boy", "child",
"bird", "cat", "dog", "animal", "insect", "pet", "baby"]
refdoc = self.nlp(" ".join(living_beings_vocab))
tokens = [token for token in self.nlp(word) if token.pos_ == "NOUN" or token.pos_ == "PROPN"]
avg = 0
for token2 in tokens:
for token in refdoc:
sim = token.similarity(token2)
if sim >= 0.7:
return True
avg += sim
avg = avg / len(refdoc)
if avg > 0.5:
return True
return False
def get_personx(self, input_event, use_chunk=True):
"""
@param input_event:
@param use_chunk:
@return:
"""
doc = self.nlp(input_event)
svos = [svo for svo in textacy.extract.subject_verb_object_triples(doc)]
if len(svos) == 0:
if use_chunk:
logger.info(f'No subject was found for the following sentence: "{input_event}". Using noun chunks.')
noun_chunks = [chunk for chunk in doc.noun_chunks]
if len(noun_chunks) > 0:
personx = noun_chunks[0].text
# is_named_entity = noun_chunks[0].root.pos_ == "PROP"
return personx
else:
logger.info("Didn't find noun chunks either, skipping this sentence.")
return ""
else:
logger.warning(
f'No subject was found for the following sentence: "{input_event}". Skipping this sentence')
return ""
else:
subj_head = svos[0][0]
# print("SUBJ HEAD", subj_head)
# is_named_entity = subj_head[0].root.pos_ == "PROP"
personx = subj_head[0].text
# " ".join([t.text for t in list(subj_head.lefts) + [subj_head] + list(subj_head.rights)])
return personx
def get_personx_long(self, input_event, use_chunk=True):
"""
@param input_event:
@param use_chunk:
@return:
"""
doc = self.nlp(input_event)
personx = ""
# Try subjects
for token in doc:
# print(token.dep_, token.text)
if ("subj" in token.dep_):
subtree = list(token.subtree)
start = subtree[0].i
end = subtree[-1].i + 1
return " ".join([a.text for a in doc[start:end]])
# Try objects
for token in doc:
if ("dobj" in token.dep_):
subtree = list(token.subtree)
start = subtree[0].i
end = subtree[-1].i + 1
return " ".join([a.text for a in doc[start:end]])
# Try SVOS and noun phrases
svos = [svo for svo in textacy.extract.subject_verb_object_triples(doc)]
if len(svos) == 0:
if use_chunk:
logger.info(f'No subject was found for the following sentence: "{input_event}". Using noun chunks.')
noun_chunks = [chunk.text for chunk in doc.noun_chunks]
# noun_chunks = [np.text
# for nc in doc.noun_chunks
# for np in [
# nc,
# doc[
# nc.root.left_edge.i
# :nc.root.right_edge.i + 1]]]
# print(noun_chunks)
if len(noun_chunks) > 0:
return noun_chunks[0]
else:
logger.info("Didn't find noun chunks either, skipping this sentence.")
return ""
else:
logger.warning(
f'No subject was found for the following sentence: "{input_event}". Skipping this sentence')
return ""
else:
subj_head = svos[0][0]
personx = subj_head[0].text
return personx
def lexical_overlap(self, vocab, s1, threshold=0.7):
if not vocab or not s1:
return 0
w1 = s1.split()
for s2 in vocab:
w2 = s2.split()
overlap = len(set(w1) & set(w2)) / (len(w1)+1)
if overlap > threshold:
return True
return False
def get_personx_srl(self, sentence):
"""
@param sentence:
@return:
"""
from allennlp.predictors.predictor import Predictor
predictor = Predictor.from_path(self.srl_predictor)
results = predictor.predict(
sentence=sentence
)
print(results)
personx = {}
for v in results['verbs']:
# print(v['verb'])
text = v['description']
# print(text)
search_results = re.finditer(r'\[.*?\]', text)
for item in search_results:
out = item.group(0).replace("[", "")
out = out.replace("]", "")
out = out.split(": ")
# print(out)
if len(out) >= 2:
relation = out[0]
node = out[1]
if relation == 'ARG1' and v['verb'] not in personx:
personx[v['verb']] = node
if relation == 'ARG0':
personx[v['verb']] = node
# print(personx)
persons = list(personx.values())
substring_list = ['what is', 'what are', 'where', 'where is', 'where are', 'what',
'how are', 'how many', 'how is', 'how', 'where is', 'where are', 'where',
'when was', 'when is',
'which is', 'which are', 'can you', 'which', 'would the',
'is the', 'is this', 'why did', 'why is', 'are the', 'do', 'why']
returnval = persons[0] if persons else ""
for subs in substring_list:
if subs in returnval:
returnval = returnval.replace(subs, "")
return returnval if returnval else "person"
if __name__ == '__main__':
logger.info("Converting caption expansions to sentences")
question_converter = ExpansionConverter()
sample_questions = [
"is in the motorcyclist 's mouth",
"Number birthday is probably being celebrated ",
"best describes the pool of water",
"The white substance is on top of the cupcakes",
"type of device is sitting next to the laptop",
"A laptop computer sitting on top of a desk"]
print("Checking sample questions")
for i in tqdm(range(len(sample_questions))):
question = sample_questions[i]
print("IN: ", question)
print("======================")
qp = question_converter.get_personx_srl(question)
print("SUBJ: ", qp)
print("\n")