-
Notifications
You must be signed in to change notification settings - Fork 0
/
final_graph.py
505 lines (447 loc) · 12.7 KB
/
final_graph.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
import verb_tree as vtree
from copy import deepcopy
nouns = ['NP','NX','NN','NNS','NNP','NNPS','PRP']# for noun phrases also consider the NX
adjectives = ['JJ','JJR','JJS']
adverb = ['RB','RBR','RBS']
verbs = ['VB','VBD','VBG','VBN','VBP','VBZ']
def prepcfg(root,t):
for j in xrange(t):
print '\t',
if(root.data == 'NP'):
root = augment_NP(root)
if(root.data == 'VP'):
root = augment_VP(root)
if(root.data == 'PP'):
root = augment_PP(root)
if(root.data == 'ADJP'):
root = augment_ADJP(root)
if(root.data =='S'):
root = augment_S(root)
if('ADJP' in [c.data for c in root.children]):
ac,at,vt,vc = None, None, None, None
for c in root.children:
if(c.data == 'ADJP'):
ac,at = deepcopy(c),c
if(c.data in verbs):
vc,vt = deepcopy(c),c
root.children.remove(at)
if(vc is not None):
root.children.remove(vt)
Children = []
Children += root.children
ac = augment_ADJP(ac)
for c in ac.children:
Children.append(c)
if(vc is not None):
root.phrase = vc.phrase
root.children = Children
# print '%s{%s}'%(root.data,root.phrase),
print 'DATA:%s | PHRASE:%s | HEAD:%s'%(root.data,root.phrase,root.head),
if root.relation!=None:
print '| RELATION:%s'%(root.relation),
# print '<<%s>>'%(root.relation),
else:
print '| RELATION:[]',
if root.tag!=None:
print '| TAG:%s'%(root.tag),
# print '<%s>'%(root.tag),
else:
print '| TAG:[]',
print '| LEVEL:%d'%t,
print '\n',
if root.children==None:
return
t=t+1
for i in root.children:
prepcfg(i,deepcopy(t))
def augment_PP(root):
# print '*'*20,root.data,root.phrase,'*'*20
if(root.children == None):
return root
# Rule PP <- IN NP
PREP_child,NP_child=None,None
P_temp,NP_temp = None,None
for c in root.children:
if(c.data == 'IN' or c.data == 'TO'):
PREP_child, P_temp= deepcopy(c),c
elif (c.data in nouns ):# (c.data == 'NP'):
NP_child, NP_temp= deepcopy(c),c
root.children.remove(NP_temp)
root.children.remove(P_temp)
if(root.relation is not None): # and (IN_child is not None):
root.data, root.children, root.phrase, root.head = 'NP', NP_child.children, NP_child.phrase, NP_child.head
root.relation.append( PREP_child.phrase)
else:
root.data, root.children, root.phrase, root.relation ,root.head = 'NP', NP_child.children, NP_child.phrase, [PREP_child.phrase], NP_child.head
# print '{{',root.data,root.phrase,root.relation,'}}'
root = augment_NP(root)
return root
def augment_NP_VBG(root):
# Rule NP <- NP (VP) <- NP (VBG NP)
NP = False
for n in nouns:
if n in [c.data for c in root.children]:
NP=True
if(root.data == 'NP') and NP and ('VP'in [c.data for c in root.children]):
parent = root
vc,nc,vt,nt = None, None, None, None
for c in parent.children:
if(c.data == 'VP'):
vc,vt = deepcopy(c),c
if(c.data in nouns ):#(c.data == 'NP'):
nc, nt = deepcopy(c),c
parent.children.remove(nt)
parent.children.remove(vt)
root = vc
NP_child, VBG_child = None,None
V_temp,N_temp = None,None
for c in root.children:
if(c.data == 'VBG'):
VBG_child, V_temp= deepcopy(c),c
elif(c.data in nouns ):#(c.data == 'NP'):
NP_child, N_temp= deepcopy(c),c
if(VBG_child == None):
return parent
root.children.remove(N_temp)
root.children.remove(V_temp)
root.data, root.children, root.phrase, root.relation ,root.head = 'NP', NP_child.children, NP_child.phrase, VBG_child.phrase, NP_child.head
root = augment_NP(root)
nc = augment_NP(nc)
nc.children.append(root)
return nc
return root
def augment_ADJP(root):
RB,PP,CC,JJ = False,False,False,False
for c in root.children:
if(c.data in adverb):
RB = True
if(c.data in adjectives):
JJ = True
elif(c.data == 'PP'):
PP = True
elif(c.data == 'CC'):
CC = True
# RULE ADJP <- RB* ADJ PP
if (root.data == 'ADJP') and RB and PP and (not CC):
jc, rbc, pc, jt, rbt , pt = None, [], None, None, [], None
for c in root.children:
if(c.data == 'PP'):
pc = deepcopy(c)
pt = c
if(c.data in adjectives):
jc = deepcopy(c)
jt = c
if(c.data in adverb ):#(c.data == 'NP'):
rbc.append(deepcopy(c))
rbt.append(c)
root.children.remove(pt)
root.children.remove(jt)
for j in rbt:
root.children.remove(j)
root.children = []
jc.relation.append('modifier')
jc.children = []
jc.children.append(augment_PP(pc))
for r in rbc:
r.relation.append('adjectival-modifier')
jc.children.append(r)
root.children.append(jc)
return root
# RULE ADJP <- RB* ADJ
elif (root.data == 'ADJP') and RB and (not PP and not CC):
jc, rbc, jt, rbt = None, [], None, []
for c in root.children:
if(c.data in adjectives):
jc = deepcopy(c)
jt = c
if(c.data in adverb ):#(c.data == 'NP'):
rbc.append(deepcopy(c))
rbt.append(c)
root.children.remove(jt)
for j in rbt:
root.children.remove(j)
root.children = []
jc.relation.append('modifier')
jc.children = []
for r in rbc:
r.tag.append('adjectival-modifier')
jc.children.append(r)
root.children.append(jc)
return root
# RULE ADJP <- ADJ PP
elif (root.data == 'ADJP') and PP and (not CC and not RB):
jc, pc, jt , pt = None, None, None, None
for c in root.children:
if(c.data == 'PP'):
pc = deepcopy(c)
pt = c
if(c.data in adjectives):
jc = deepcopy(c)
jt = c
root.children.remove(pt)
root.children.remove(jt)
root.children = []
jc.relation.append('modifier')
jc.children = [augment_PP(pc)]
root.children.append(jc)
return root
# RULE ADJP <- ADJP (CC ADJP (CC ADJ)*)*
elif (root.data == 'ADJP') and CC and (not PP and not RB):
jc, ac, jt , at, faltu = [], [], [], [], []
for c in root.children:
if(c.data == 'DT')or(c.data == 'CC'):
faltu.append(c)
if(c.data in adjectives):
jc.append(deepcopy(c))
jt.append(c)
if(c.data == 'ADJP' ):#(c.data == 'NP'):
ac.append(deepcopy(c))
at.append(c)
for f in faltu:
root.children.remove(f)
for a in at:
root.children.remove(a)
for j in jt:
root.children.remove(j)
Children = []
for j in jc:
j.relation.append('modifier')
Children.append(j)
for a in ac:
Children.append(augment_ADJP(a))
root.children = Children
return root
# default RULE ADJP <- something ADJ*
elif (root.data == 'ADJP') and (JJ):
jc,jt, faltu = [], [], []
for c in root.children:
if(c.data == 'DT')or(c.data == 'CC'):
faltu.append(c)
if(c.data in adjectives):
jc.append(deepcopy(c))
jt.append(c)
for f in faltu:
root.children.remove(f)
for j in jt:
root.children.remove(j)
Children = []
for j in jc:
j.relation.append('modifier')
Children.append(j)
root.children = Children
return root
return root
def augment_NP(root):
# print '*'*30
# print '#'*20,root.data,root.phrase,'#'*20
if(root.children == None):
return None
only_nouns = True # only nouns in NP
ADJP = False# any ADJP in np
NP = False # any nouns in np
ADJ = False# any adjectives in NP
for c in root.children:
if not (c.data in nouns):
only_nouns = False
if(c.data == 'ADJP'):
ADJP = True
if(c.data in nouns):
NP=True
if(c.data in adjectives):
ADJ=True
# RULE NP <- NP PP
if(root.data == 'NP') and NP and ('PP'in [c.data for c in root.children]):
# print 'HERERERERERE ',root.data,root.phrase
pc,nc,pt,nt = None, None, None, None
for c in root.children:
if(c.data == 'PP'):
pc,pt = deepcopy(c),c
if(c.data in nouns ):#(c.data == 'NP'):
nc, nt = deepcopy(c),c
root.children.remove(nt)
root.children.remove(pt)
parent = root
nc = augment_NP(nc)
nc.children+=[augment_PP(pc)]
if(parent.relation is not None):
parent.data, parent.children, parent.phrase, parent.head = nc.data, nc.children, nc.phrase, nc.head
# parent.relation = pc.relation
else:
parent.data, parent.children, parent.phrase, parent.head = nc.data, nc.children, nc.phrase, nc.head
# print '#'*20,nc.phrase
# print '\\\\',parent.data,parent.phrase,parent.relation,'//'
return parent
# Rule NP <- NP (VP) <- NP (VBG NP)
if(root.data == 'NP') and NP and ('VP'in [c.data for c in root.children]):
nc = augment_NP_VBG(root)
if(root.relation is not None): # and (IN_child is not None):
root.data, root.children, root.phrase, root.head = nc.data, nc.children, nc.phrase, nc.head
# print '#'*20,nc.relation
# root.relation.add(nc.relation)
else:
root.data, root.children, root.phrase, root.relation ,root.head = nc.data, nc.children, nc.phrase, nc.relation, nc.head
# RULE ADJP in NP
elif(root.data == 'NP') and ADJP and NP:
is_NP = False
ac,at,nc,nt = None, None, [], []
for c in root.children:
if(c.data == 'NP'):
is_NP = True
if(c.data in nouns):
nc.append(deepcopy(c))
nt.append(c)
if(c.data == 'ADJP'):
ac,at = deepcopy(c),c
root.children.remove(at)
for n in nt:
if not(n.data == 'NP'):
root.children.remove(n)
phrase = ' '
for c in nc:
c = augment_NP(c)
phrase += c.phrase+' '
ac = augment_ADJP(ac)
Children = []
for a in ac.children:
a.relation.append('modifier')
Children.append(a)
root.children = Children
root.phrase = phrase
return root
# RULE NP <- DT ADJ* Noun
elif(root.data == 'NP') and NP and ADJ:
JJc,nc,JJt,nt,dt = [], [], [], [], None
for c in root.children:
if(c.data == 'DT'):
dt = c
if(c.data in adjectives):
JJc.append(deepcopy(c))
JJt.append(c)
if(c.data in nouns ):#(c.data == 'NP'):
nc.append(deepcopy(c))
nt.append(c)
if(dt is not None):
root.children.remove(dt)
for n in nt:
root.children.remove(n)
for j in JJt:
root.children.remove(j)
Children = []
phrase = ''
for n in nc:
nn = augment_NP(n)
phrase+= nn.phrase+' '
for j in JJc:
j.relation.append('modifier')
Children.append(j)
root.data, root.children, root.phrase, root.head = 'NP', Children, phrase, None
# print '#'*20
# for i in root.children:
# print i.phrase,i.data, i.relation
return root
# RULE NP <- only_nouns
elif(root.data == 'NP') and only_nouns:
# print '3'*20, root.data, root.phrase
nc,nt = [],[]
for c in root.children:
if(c.data in nouns):# (c.data == 'NP'):
nc.append(deepcopy(c))
nt.append(c)
phrase = ''
for c in nc:
phrase += c.phrase+' '
if not(phrase == ''):
root.phrase = phrase
# print '{{{',root.data,phrase,'}}}'
root.children = []
return root
# RULE NP <- SOMETHING NP
elif (root.data == 'NP') and ('NP'in [c.data for c in root.children]):
nc = None
for c in root.children:
if(c.data == 'NP'):
nc = c
nc = augment_NP(nc)
# Keep the relation of the root and update all other things # root.relation not <- nc.relation
root.data, root.children, root.phrase,root.head = nc.data, nc.children, nc.phrase, nc.head
# print '//',root.children[0].data,root.children[0].phrase,root.children[0].relation,'\\\\'
return root
# RULE NP <- SOMETHING(DT) noun
elif(root.data == 'NP') and NP:
# print '*'*20, root.data, root.phrase
nc,nt = [],[]
for c in root.children:
if(c.data in nouns):# (c.data == 'NP'):
nc.append(deepcopy(c))
nt.append(c)
is_NP = ('NP' in [c.data for c in root.children])
root.children = []
phrase = ''
for c in nc:
phrase += c.phrase+' '
root.phrase = phrase
if(is_NP):
for c in nc:
if(c.data == 'NP'):
c = augment_NP(c)
root.children.append(c)
# print "<><>",root.children[0].children[0].phrase,"<><>"
return root
# Default
return root
def augment_VP(root):
if(root.children is None):
return root
# RULE verb
if(root.data in verbs):
return root
VP =False
for c in root.children:
if(c.data in verbs):
VP = True
# RULE VP <- verb ...
if (root.data == 'VP') and VP:
# print '#'*20,'<%s>'%root.phrase
vc,vt,vpt,vpc = None, None, None, None
for c in root.children:
if(c.data == 'VP'):
vpc,vpt = deepcopy(c),c
if(c.data in verbs):
vc,vt = deepcopy(c),c
# root.children.remove(vt)
if(vpt is not None):
root.children.remove(vpt)
vpc = augment_VP(vpc)
Children = []
Children += root.children
Children += vpc.children
root.children = Children
root.phrase = vpc.phrase
else:
root.phrase = vc.phrase
root.children.remove(vt)
return root
return root
def augment_S(root):
if('VP' in [c.data for c in root.children]):
vpc,vpt = None,None
for c in root.children:
if c.data == 'VP':
vpc,vpt = deepcopy(c),c
root.children.remove(vpt)
root.data = 'VP'
vpc = augment_VP(vpc)
root.phrase = vpc.phrase
root.head = vpc.head
Children = []
Children += root.children
Children += vpc.children
root.children = Children
return root
# def augment(root):
# if(root.data =='PP'):
# root = augment_PP(root)
root = vtree.root
prepcfg(root,0)
# vt.prepcfg(parent.children[0],0)
# vt.prepcfg(root,0)