-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrcqg.py
529 lines (452 loc) · 21.1 KB
/
rcqg.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
import copy
import inspect
from spacy import displacy
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("=========================Text Lemma POS Tag Dep Shape Is_Alpha Is_Stop")
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):
if type(doc) == str:
doc = self.nlp(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:end].noun_chunks]
if len(noun_chunks) > 0:
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
return Head_Noun_Chunk
def getNounChunk(noun):
result = next(filter(lambda x: x.start <= noun.i and x.end >= noun.i, doc.noun_chunks), None)
if result:
return result
else:
return doc[noun.i:noun.i + 1]
def PPChunker(doc, Head_Noun_Chunk):
end = Head_Noun_Chunk
while True:
if Head_Noun_Chunk.dep_ == "ROOT":
break
elif 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
adjusted_answer = False
is_whose = False
rule_zero = False
for wpindex, wpword in enumerate(relativeclauseswh):
adjusted_answer = False
is_whose = False
rule_zero = False
'''
Rule 1: Using the matrix clause
Rule 2: Using the embedded clause
Rule 3: Relative clause modifying the NP Constituent
'''
def subs_answer():
index = wpword.i
while (doc[index - 1].pos_ not in ["NOUN", "DET", "PROPN", "PRP"]):
index = index - 1
if index <= 0:
return False
# flag set if ccomp or advcl occurs
answer = getNounChunk(doc[index - 1])
return answer
if wpword.text == "whose":
answer = PPChunker(doc, wpword.head.head.head)
is_whose == True
elif wpword.head.dep_ in ['clausecomp', 'advcl']:
adjusted_answer = True
answer = subs_answer()
else:
answer = PPChunker(doc, NounParent(wpword))
matrix = doc[loc_relative_clause:wpword.i]
ending = doc[wpword.i:]
punctCheck = self.filteratt({
'pos_': ['PUNCT']
}, ending)
if (len(punctCheck) > 0):
end = punctCheck[0].i
else:
if wpindex + 1 < len(relativeclauseswh):
end = relativeclauseswh[wpindex + 1].i
else:
end = None
relclause = doc[wpword.i:end]
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': [False, 'What', 'What'],
'what': ['What', 'What', False],
'that': ['What', 'What', 'What'],
'where': ['Where', 'Where', 'Where'],
'when': [False, 'When', False],
'how': ['What', 'How', False, ],
'why': ['What', 'Why', False, ],
'whatsoever': ['What', 'What', False],
'whomsoever': ['Who', 'Who', False]
}
if wpword.text.lower() in conversions.keys():
if answer:
end_1 = answer.start
else:
end_1 = wpword.i
questionwords = conversions[wpword.text.lower()]
# Find Requirements - Special case where root comes after relative clause
root = self.filteratt({
'dep_': ['ROOT'],
}, doc[wpword.i:])
# Rules
# Rule 0
# Rule 1
if questionwords[0]:
if len(root) > 0:
if self.filteratt({'dep_': ['nsubj', 'nsubjpass']}, list(root[0].children))[0].text in answer.text:
rule_zero = True
yield (1, questionwords[0] + " " + doc[VerbChunk(root[0]):].text + "?")
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 (2, "%s %s %s?" % (questionwords[0], pasttenseverb[0].text, " ".join(
without(pasttenseverb[0].i, pasttenseverb[0].i, doc[loc_relative_clause:end_1]))))
else:
pasttenseverb = pasttenseverb[0]
converted = [x.text for x in doc[loc_relative_clause:pasttenseverb.i]] + [
pasttenseverb.lemma_] + [
x.text for x in doc[
pasttenseverb.i + 1:end_1]]
yield (3, "%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]
converted = [aux.text] + without(aux.i, aux.i, doc[loc_relative_clause: end_1])
yield (4, "%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 (5, "%s %s %s %s?" % (
questionwords[0], presentsimple[0].text, getNounChunk(noun).text,
doc[presentsimple[0].i + 1:end_1]))
else:
presentsimple = presentsimple[0]
converted = [x.text for x in doc[loc_relative_clause:presentsimple.i]] + [
presentsimple.lemma_] + [
x.text for x in doc[
presentsimple.i + 1:end_1]]
yield (6, "%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 (
7,
"%s %s %s?" % (questionwords[0], presentsimplethird[0].text, getNounChunk(noun).text))
else:
presentsimplethird = presentsimplethird[0]
converted = [x.text for x in doc[loc_relative_clause:presentsimplethird.i]] + [
presentsimplethird.lemma_] + [x.text for x in doc[
presentsimplethird.i + 1:end_1]]
yield (8, "%s does %s?" % (questionwords[0], " ".join(converted)))
if questionwords[1]:
# Rule 2
# Find Requirements
pasttenseverb = self.filteratt({
'tag_': 'VBD',
'dep_': ['relcl', 'ccomp', 'advcl']
}, relclause)
presentcontinuousverb = self.filteratt({
'tag_': 'VBG',
'dep_': ['relcl', 'ccomp', 'advcl']
}, relclause)
pastparticiple = self.filteratt({
'tag_': 'VBN',
'dep_': ['relcl', 'ccomp', 'advcl']
}, relclause)
presentsimple = self.filteratt({
'tag_': 'VBP',
'dep_': ['relcl', 'ccomp', 'advcl']
}, relclause)
presentsimplethird = self.filteratt({
'tag_': 'VBZ',
'dep_': ['relcl', 'ccomp', 'advcl']
}, relclause)
if wpword.dep_ == "nsubj" or wpword.dep_ == "nsubjpass" or is_whose:
# TODO - Mukul says its Hack , Co-authors disagree , Module overlap
if len(root) > 0:
yield (9, "%s %s?" % (
questionwords[1], " ".join([x.text for x in doc[wpword.i + 1:VerbChunk(root[0])]])))
else:
yield (10, "%s %s?" % (questionwords[1], " ".join([x.text for x in doc[wpword.i + 1:end]])))
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:end]]
yield (11, "%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:end])
yield (12, "%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:end]]
yield (13, "%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:end]]
yield (14, "%s does %s?" % (questionwords[1], " ".join(converted)))
if questionwords[2]:
# Rule 3
temp_head = answer
checker = self.filteratt({
'dep_': ['nsubj', 'relcl']
}, sum([list(x.children) for x in answer], []))
if (len(checker) == 0 or adjusted_answer):
head = doc[answer.end - 1]
elif (len(checker[0].head)):
head = checker[0].head
if not head:
print("Subject modified by relative clause not found.")
elif (head.pos_ == "PROPN"):
# stop rule 3 questions at the noun_chunk only
if (head.tag_ == "NNS"):
if len(pasttenseverb) > 0:
yield (15, "%s were %s?" % (questionwords[2], answer))
else:
yield (16, "%s are %s?" % (questionwords[2], answer))
elif (not head.tag_ == "NNS"):
if len(pasttenseverb) > 0:
yield (17, "%s was %s?" % (questionwords[2], answer))
else:
yield (18, "%s is %s?" % (questionwords[2], answer))
else:
ending = doc[wpword.i:]
punctCheck = self.filteratt({
'pos_': ['PUNCT']
}, ending)
if (len(punctCheck) > 0):
end = punctCheck[0].i
else:
if wpindex + 1 < len(relativeclauseswh):
end = relativeclauseswh[wpindex + 1].i
# elif wpindex + 1 == len(relativeclauseswh):
# end = wpword.i
else:
end = None
if (head.tag_ == "NNS"):
if len(pasttenseverb) > 0:
yield (19, "%s were %s %s?" % (questionwords[2], answer, doc[head.i + 1:end]))
else:
yield (20, "%s are %s %s?" % (questionwords[2], answer, doc[head.i + 1:end]))
else:
if len(pasttenseverb) > 0:
yield (21, "%s was %s %s?" % (questionwords[2], answer, doc[head.i + 1:end]))
else:
yield (22, "%s is %s %s?" % (questionwords[2], answer, doc[head.i + 1:end]))
# When matrix sentence is immediately complete, waiting for a verb , it's called appositoin- in this case,
loc_relative_clause = wpword.i
def preprocessing(self, sentence):
sentence = sentence.strip('.')
return sentence
@lastdec
def genqlistdev(self, sentence):
sentence = self.preprocessing(sentence)
doc = self.nlp(sentence)
sentences = self.conjHandling(doc)
return sum([list(self.genq(x.text)) for x in sentences], [])
@lastdec
def genqlist(self, sentence):
sentence = self.preprocessing(sentence)
doc = self.nlp(sentence)
sentences = self.conjHandling(doc)
return sum([[x[1] for x in self.genq(x.text)] for x in sentences], [])
def genqlistlast(self):
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)