-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSBAR.py
160 lines (155 loc) · 3.34 KB
/
SBAR.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
sent8='While eating food Ram is singing a song .'
sent9='After she ate the cake , Emma visited Tony in his room .'
sent10='While talking to Sita , Ram is playing guitar .'
sent11='While eating food and drinking water Ram is singing a song .'
sent12='While playing piano Ram is singing a song in a room and Shyam is playing violin .'
sent13='After Shyam came , Ram went .'
sent1='Are you kidding , or are you damn serious ?'
sent2='Ram is a boy and Sita is a girl .'
sent3="Sam 's dad gave Sam 39 nickels and 31 quarters ."
sent4='He eats an apple , she sings a song .'
sent5='Ram is a boy who is six years old .'
sent6='Ram is playing guiter while talking to sita .'
sent7='Because I was late I became angry .'
sent=sent7
def simplify(sent):
from anytree import NodeMixin, Node,AnyNode,RenderTree
from nltk.parse.stanford import StanfordParser
def make_tree(tree,t,sent_list):
#this fn. converts nltk tree to anytree
if tree not in sent_list:
ttt=AnyNode(id=str(tree.label()),parent=t)
for tt in tree:
make_tree(tt,ttt,sent_list)
else:
AnyNode(id=str(tree),parent=t)
parser=StanfordParser()
#SBAR CASE
def find_sbar(t):
if t.id=='SBAR':
global sbar
sbar=t
for tt in t.children:
find_sbar(tt)
def find_vp_in_sbar(t):
if t.id=='VP':
global vp_sbar
vp_sbar.append(t)
for tt in t.children:
find_vp_in_sbar(tt)
def find_np_in_sbar(t):
global f
global ff
if t.id=='VP':
ff=False
if (t.id=='NP') and f==True and ff==True:
global np_sbar
np_sbar=t
f=False
for tt in t.children:
find_np_in_sbar(tt)
def find_vp(t):
if t.id=='SBAR':
return
global f
if t.id=='VP' and f==True:
global vp
vp=t
f=False
for tt in t.children:
find_vp(tt)
def find_np(t):
if t.id=='SBAR':
return
global f
if t.id=='NP' and f==True:
global np
np=t
f=False
for tt in t.children:
find_np(tt)
def find_vbz(t):
if t.id=='SBAR':
return
global f
if t.id=='VBZ' and f==True:
global vbz
vbz=t.children[0].id
f=False
for tt in t.children:
find_vbz(tt)
def make_sent(t):
global simple_sentences
if t.id in sent_list:
simple_sentences[-1].append(t.id)
for tt in t.children:
make_sent(tt)
#sent=sent8
parse_trees=parser.raw_parse(sent)
global sent_list
sent_list=[s for s in sent.split()]
tree=next(parse_trees)[0]
#tree.draw()
t=AnyNode(id='ROOT')
make_tree(tree,t,sent_list)
global sbar
sbar=t
global vp_sbar
global f
global ff
global np_sbar
global vp
global np
global vbz
vp_sbar=[]
vp=t
np=t
vbz='bn2'
np_sbar=t
find_sbar(t)
find_vp_in_sbar(sbar)
f=True
ff=True
find_np_in_sbar(sbar)
f=True
find_vp(t)
f=True
find_np(t)
f=True
find_vbz(t)
global simple_sentences
simple_sentences=[]
simple_sentences.append([])
make_sent(np)
make_sent(vp)
for i in range(len(vp_sbar)):
simple_sentences.append([])
if np_sbar==t:
make_sent(np)
else:
make_sent(np_sbar)
if vbz!='bn2':
simple_sentences[-1].append(vbz)
make_sent(vp_sbar[i])
#print (simple_sentences)
simple=[]
for sentence in simple_sentences:
string=''
for word in sentence:
string+=word+' '
string+='.'
simple.append(string)
def is_any_sbar(t):
if t.id=='SBAR':
global f
f=True
return
for tt in t.children:
is_any_sbar(tt)
f=False
is_any_sbar(t)
if f==False:
simple=[sent]
return simple
#print(simplify(sent10))
#print RenderTree(t)