-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathref_parser.py
460 lines (400 loc) · 18.6 KB
/
ref_parser.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 24 16:21:26 2018
@author: payalkhullar
"""
import inspect
import copy
import spacy
from spacy import displacy
#import ipdb
def filetotext():
return open("temp.txt", "r").read()
class WHQuestionGenerator():
last = False
def __init__(self, nlp):
self.nlp = nlp
def lastdec(fun):
def newfun(self, x):
self.last = x
return fun(self, x)
return newfun
@lastdec
def show(self, u_line):
doc = self.nlp(u_line)
print("=========================Sentence -", u_line)
print("=========================Tokens and POS tags")
for token in doc:
print(token.text, token.lemma_, token.pos_, token.tag_, token.dep_, token.shape_, token.is_alpha,
token.is_stop)
print("=========================NER tags")
for ent in doc.ents:
print(ent.text, ent.start_char, ent.end_char, ent.label_)
print("=========================Noun Chunks tags")
for c in doc.noun_chunks:
print(c.text)
print("=========================SENTENCE DONE")
@lastdec
def display(self, u_line):
doc = self.nlp(u_line)
displacy.serve(doc, style='dep', port=8080)
def props(self, obj):
pr = {}
for name in dir(obj):
value = getattr(obj, name)
if not name.startswith('__') and not inspect.ismethod(value):
pr[name] = value
return pr
def serialize(self, obj):
return {
'tag_': obj.tag_,
'dep_': obj.dep_,
'pos_': obj.pos_,
}
def filt(self, d2):
return lambda x: set(d2.items()).issubset(set(self.serialize(x).items()))
def expand(self, d, i=0):
if i >= len(d.keys()):
return [d]
ind, val = list(d.items())[i]
result = []
if type(val) == list:
for option in val:
temp = copy.deepcopy(d)
temp[ind] = option
result += self.expand(temp, i + 1)
else:
result += self.expand(d, i + 1)
return result
def _filteratt(self, att, doc):
att = self.expand(att)
if len(att) == 1:
att = att[0]
if type(att) == list:
return sum([self._filteratt(i, doc) for i in att], [])
return list(filter(lambda tup: self.filt(att)(tup), doc))
def filteratt(self, att, doc):
return sorted(self._filteratt(att, doc),key =lambda x: x.i)
def conjHandling(self, doc):
sentential_conjunctions = []
conjunctions = self.filteratt({
'pos_': ["CCONJ", "PUNCT"],
}, doc) + self.filteratt({
'pos_': "ADP",
'dep_': "mark"
}, doc)
sorted_conjunctions = sorted(conjunctions, key=lambda x: x.i)
end = len(doc)
for conjunction in reversed(sorted_conjunctions):
# temp_doc = doc[conjunction.i:]
noun_chunks = [c for c in doc[conjunction.i:].noun_chunks]
if noun_chunks[0].root.dep_ == "nsubj":
sentential_conjunctions.append(conjunction)
end = conjunction.i
sentential_conjunctions = reversed(sentential_conjunctions)
indices = [x.i for x in sentential_conjunctions]
def splitsentence(sentence):
start = sentence[0].i
end = sentence[-1].i
for i in sentence:
if i.i in indices:
return [doc[start:i.i]] + splitsentence(doc[i.i + 1:end + 1])
return [sentence]
return splitsentence(doc)
def genq(self, sentence):
def without(start, end, doc):
startind = start
endind = end
for ind, val in enumerate(doc):
if val.i == start:
startind = ind
if val.i == end:
endind = ind
return [x.text for x in doc[:startind]] + [x.text for x in doc[endind + 1:]]
def VerbChunk(root):
aux_verb = self.filteratt({
'dep_' : ['aux','auxpass']
},list(root.children))
if len(aux_verb) > 0:
return aux_verb[0].i
else:
return root.i
# if root.head == aux or root.head == auxpass :
# return root.head.i
def NounCousin(root):
Head_Noun_Chunk = root
Root_children = self.filteratt({[
'pos_','NOUN','PROPN','PRON']
},list(root.children))
for child in Root_children:
if child.dep_ != 'nsubj':
Head_Noun_Chunk = child
return Head_Noun_Chunk
def NounParent(index):
original = index
Head_Noun_Chunk = index.head
while (Head_Noun_Chunk.pos_ not in ['NOUN','PROPN']):
if Head_Noun_Chunk.dep_ == "ROOT":
return NounCousin(Head_Noun_Chunk)
elif Head_Noun_Chunk == Head_Noun_Chunk.head:
return original
Head_Noun_Chunk = Head_Noun_Chunk.head
#print(Head_Noun_Chunk)
return Head_Noun_Chunk
def getNounChunk(noun):
found = False
for noun_chunk in doc.noun_chunks:
if noun_chunk.start <= noun.i and noun_chunk.end >= noun.i:
found = True
result = noun_chunk
if found:
return result
else:
return False
def PPChunker(doc, Head_Noun_Chunk):
end = Head_Noun_Chunk
while True:
if Head_Noun_Chunk.head.text == "of" and Head_Noun_Chunk.head.head.pos_ in ["NOUN"]:
Head_Noun_Chunk = Head_Noun_Chunk.head.head
else:
break
## TODO - Forward PP chunking( I have met the mother of my son who is)
return doc[getNounChunk(Head_Noun_Chunk).start:getNounChunk(end).end]
doc = self.nlp(sentence)
relativeclauseswh = self.filteratt({
'tag_': ['WDT', 'WP$', 'WPO', 'WPS', 'WQL', 'WRB', 'WP'],
}, doc)
loc_relative_clause = 0
for wpword in relativeclauseswh:
'''
Rule 1: Using the matrix clause
Rule 2: Using the embedded clause
Rule 3: Relative clause modifying the NP Constituent
'''
answer = PPChunker(doc, NounParent(wpword))
matrix = doc[loc_relative_clause:answer.start]
relclause = doc[wpword.i:]
hasanswer = {
'who': True,
'whom': True,
'whose': True,
'which': True,
'that': True,
'when': False,
'how': False,
'why': False,
'whatsoever': False,
'whomsoever': False
}
conversions = {
'who': ['Who', 'Who', 'Who'],
'whom': ['Whom', 'Whom', 'Who'],
'whose': ['Who', 'Whose', 'Who'],
'which': ['What', 'What', 'What'],
'that': ['What', 'What', 'What'],
'where': [False, 'Where', False],
'when': [False, 'When', False],
'how': [False, 'What', False, ],
'why': [False, 'What', False, ],
'whatsoever': ['What', 'What', False],
'whomsoever': ['Who', 'Who', False]
}
if wpword.text.lower() in conversions.keys():
questionwords = conversions[wpword.text.lower()]
# Find Requirements
root = self.filteratt({
'dep_': ['ROOT'],
}, doc[wpword.i:])
# Rules
# Rule 0
if len(root) > 0:
if self.filteratt({'dep_': ['nsubj', 'nsubjpass']}, list(root[0].children))[0].text in answer.text:
yield (questionwords[0] + " " + doc[VerbChunk(root[0]):].text + "?")
# Rule 1
if questionwords[0]:
pasttenseverb = self.filteratt({
'tag_': 'VBD',
'dep_': 'ROOT'
}, matrix)
bareverb = self.filteratt({
'tag_': 'VB',
'dep_': 'ROOT'
}, matrix)
presentcontinuousverb = self.filteratt({
'tag_': 'VBG',
'dep_': 'ROOT'
}, matrix)
pastparticiple = self.filteratt({
'tag_': 'VBN',
'dep_': 'ROOT'
}, matrix)
presentsimple = self.filteratt({
'tag_': 'VBP',
'dep_': 'ROOT'
}, matrix)
presentsimplethird = self.filteratt({
'tag_': 'VBZ',
'dep_': 'ROOT'
}, matrix)
if len(pasttenseverb) > 0:
if (pasttenseverb[0].lemma_ == "be"):
noun = self.filteratt({
'dep_': 'nsubj'
}, pasttenseverb[0].children)[0]
yield ("%s %s %s?" % (questionwords[0], pasttenseverb[0].text, getNounChunk(noun).text))
else:
pasttenseverb = pasttenseverb[0]
end = (answer.start) if doc[answer.start].pos_ == "ADP" else answer.start
converted = [x.text for x in doc[loc_relative_clause:pasttenseverb.i]] + [
pasttenseverb.lemma_] + [
x.text for x in doc[
pasttenseverb.i + 1:end]]
yield ("%s did %s?" % (questionwords[0], " ".join(converted)))
if len(presentcontinuousverb) > 0 or len(pastparticiple) > 0 or len(bareverb) > 0:
aux = self.filteratt({
'dep_': ['aux', 'auxpass']
}, matrix)[0]
end = (answer.start)
converted = [aux.text] + without(aux.i, aux.i, doc[loc_relative_clause: end])
yield ("%s %s?" % (questionwords[0], " ".join(converted)))
if len(presentsimple) > 0:
if (presentsimple[0].lemma_ == "be"):
noun = self.filteratt({
'dep_': 'nsubj'
}, presentsimple[0].children)[0]
yield ("%s %s %s %s?" % (questionwords[0], presentsimple[0].text, getNounChunk(noun).text,doc[presentsimple[0].i+1:answer.start]))
else:
presentsimple = presentsimple[0]
end = (answer.start) if doc[answer.start].pos_ == "ADP" else answer.start - 1
converted = [x.text for x in doc[loc_relative_clause:presentsimple.i]] + [presentsimple.lemma_] + [
x.text for x in doc[
presentsimple.i + 1:end]]
yield ("%s do %s?" % (questionwords[0], " ".join(converted)))
if len(presentsimplethird) > 0:
if (presentsimplethird[0].lemma_ == "be"):
noun = self.filteratt({
'dep_': 'nsubj'
}, presentsimplethird[0].children)[0]
yield (
"%s %s %s?" % (questionwords[0], presentsimplethird[0].text, getNounChunk(noun).text))
else:
presentsimplethird = presentsimplethird[0]
end = (answer.start) if doc[answer.start].pos_ == "ADP" else answer.start - 1
converted = [x.text for x in doc[loc_relative_clause:presentsimplethird.i]] + [
presentsimplethird.lemma_] + [x.text for x in doc[
presentsimplethird.i + 1:end]]
yield ("%s does %s?" % (questionwords[0], " ".join(converted)))
if questionwords[1]:
# Rule 2
# Find Requirements
pasttenseverb = self.filteratt({
'tag_': 'VBD',
'dep_': 'relcl'
}, relclause)
presentcontinuousverb = self.filteratt({
'tag_': 'VBG',
'dep_': 'relcl'
}, relclause)
pastparticiple = self.filteratt({
'tag_': 'VBN',
'dep_': 'relcl'
}, relclause)
presentsimple = self.filteratt({
'tag_': 'VBP',
'dep_': 'relcl'
}, relclause)
presentsimplethird = self.filteratt({
'tag_': 'VBZ',
'dep_': 'relcl'
}, relclause)
if wpword.dep_ == "nsubj" or wpword.dep_ == "nsubjpass":
# TODO - Mukul says its Hack , Co-authors disagree , Module overlap
if len(root) > 0:
yield ("%s %s?" % (
questionwords[1], " ".join([x.text for x in doc[wpword.i + 1:VerbChunk(root[0])]])))
else:
yield ("%s %s?" % (questionwords[1], " ".join([x.text for x in doc[wpword.i + 1:]])))
else:
# # # Rules
if len(pasttenseverb) > 0:
pasttenseverb = pasttenseverb[0]
converted = [x.text for x in doc[wpword.i + 1:pasttenseverb.i]] + [pasttenseverb.lemma_] + [
x.text
for x in
doc[
pasttenseverb.i + 1:]]
yield ("%s did %s?" % (questionwords[1], " ".join(converted)))
if len(presentcontinuousverb) > 0 or len(pastparticiple) > 0:
aux = self.filteratt({
'dep_': ['aux', 'auxpass']
}, relclause)[0]
converted = [aux.text] + without(aux.i, aux.i, doc[wpword.i + 1:])
yield ("%s %s?" % (questionwords[1], " ".join(converted)))
if len(presentsimple) > 0:
presentsimple = presentsimple[0]
converted = [x.text for x in doc[wpword.i + 1:presentsimple.i]] + [presentsimple.lemma_] + [
x.text
for x in doc[
presentsimple.i + 1:]]
yield ("%s do %s?" % (questionwords[1], " ".join(converted)))
if len(presentsimplethird) > 0:
presentsimplethird = presentsimplethird[0]
converted = [x.text for x in doc[wpword.i + 1:presentsimplethird.i]] + [
presentsimplethird.lemma_] + [x.text for x in doc[
presentsimplethird.i + 1:]]
yield ("%s does %s?" % (questionwords[1], " ".join(converted)))
if questionwords[2]:
# Rule 3
Head_Noun_Chunk = NounParent(wpword)
noun_chunk = PPChunker(doc, Head_Noun_Chunk).text
# Requirements
if not noun_chunk:
print("Subject modified by relative clause not found.")
else:
if (Head_Noun_Chunk.tag_ == "NNS"):
if len(pasttenseverb) > 0:
yield ("%s were %s?" % (questionwords[2], noun_chunk))
else:
yield ("%s are %s?" % (questionwords[2], noun_chunk))
elif (not Head_Noun_Chunk.tag_ == "NNS"):
if len(pasttenseverb) > 0:
yield ("%s was %s?" % (questionwords[2], noun_chunk))
else:
yield ("%s is %s?" % (questionwords[2], noun_chunk))
# Rule 4
if not noun_chunk:
print("Subject modified by relative clause not found.")
else:
if (Head_Noun_Chunk.tag_ == "NNS"):
if len(pasttenseverb) > 0:
yield ("%s were %s %s?" % (questionwords[2], noun_chunk, doc[Head_Noun_Chunk.i + 1:]))
else:
yield ("%s are %s %s?" % (questionwords[2], noun_chunk, doc[Head_Noun_Chunk.i + 1:]))
else:
if len(pasttenseverb) > 0:
yield ("%s was %s %s?" % (questionwords[2], noun_chunk, doc[Head_Noun_Chunk.i + 1:]))
else:
yield ("%s is %s %s?" % (questionwords[2], noun_chunk, doc[Head_Noun_Chunk.i + 1:]))
loc_relative_clause = wpword.i
@lastdec
def genqlist(self, sentence):
sentence = sentence.strip('. ')
doc = self.nlp(sentence)
sentences = self.conjHandling(doc)
return sum([list(self.genq(x.text)) for x in sentences], [])
def genqlistlast(self):
print(self.last)
if self.last:
return self.genqlist(self.last)
def showlast(self):
if self.last:
return self.show(self.last)
def displaylast(self):
if self.last:
return self.display(self.last)
q=WHQuestionGenerator(spacy.load('en_core_web_sm'))
#k=q.genqlist("Mary plays with a boy who is my brother")
#print([i for i in k])
#k=q.display("Mary got second position in the university and John got third")
k=q.show("Some children like football and some don't")