-
Notifications
You must be signed in to change notification settings - Fork 0
/
svo.py
279 lines (242 loc) · 9.64 KB
/
svo.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
from nltk.stem.wordnet import WordNetLemmatizer
from spacy.lang.en import English
import spacy
SUBJECTS = ["nsubj", "nsubjpass", "csubj", "csubjpass", "agent", "expl"]
OBJECTS = ["dobj", "dative", "attr", "oprd"]
def getSubsFromConjunctions(subs):
moreSubs = []
for sub in subs:
# rights is a generator
rights = list(sub.rights)
rightDeps = {tok.lower_ for tok in rights}
if "and" in rightDeps:
moreSubs.extend([tok for tok in rights if tok.dep_ in SUBJECTS or tok.pos_ == "NOUN"])
if len(moreSubs) > 0:
moreSubs.extend(getSubsFromConjunctions(moreSubs))
return moreSubs
def getObjsFromConjunctions(objs):
moreObjs = []
for obj in objs:
# rights is a generator
rights = list(obj.rights)
rightDeps = {tok.lower_ for tok in rights}
if "and" in rightDeps:
moreObjs.extend([tok for tok in rights if tok.dep_ in OBJECTS or tok.pos_ == "NOUN"])
if len(moreObjs) > 0:
moreObjs.extend(getObjsFromConjunctions(moreObjs))
return moreObjs
def getVerbsFromConjunctions(verbs):
moreVerbs = []
for verb in verbs:
rightDeps = {tok.lower_ for tok in verb.rights}
if "and" in rightDeps:
moreVerbs.extend([tok for tok in verb.rights if tok.pos_ == "VERB"])
if len(moreVerbs) > 0:
moreVerbs.extend(getVerbsFromConjunctions(moreVerbs))
return moreVerbs
def findSubs(tok):
head = tok.head
while head.pos_ != "VERB" and head.pos_ != "NOUN" and head.head != head:
head = head.head
if head.pos_ == "VERB":
subs = [tok for tok in head.lefts if tok.dep_ == "SUB"]
if len(subs) > 0:
verbNegated = isNegated(head)
subs.extend(getSubsFromConjunctions(subs))
return subs, verbNegated
elif head.head != head:
return findSubs(head)
elif head.pos_ == "NOUN":
return [head], isNegated(tok)
return [], False
def isNegated(tok):
negations = {"no", "not", "n't", "never", "none"}
for dep in list(tok.lefts) + list(tok.rights):
if dep.lower_ in negations:
return True
return False
def findSVs(tokens):
svs = []
verbs = [tok for tok in tokens if tok.pos_ == "VERB"]
for v in verbs:
subs, verbNegated = getAllSubs(v)
if len(subs) > 0:
for sub in subs:
svs.append((sub.orth_, "!" + v.orth_ if verbNegated else v.orth_))
return svs
def getObjsFromPrepositions(deps):
objs = []
for dep in deps:
if dep.pos_ == "ADP" and dep.dep_ == "prep":
objs.extend([tok for tok in dep.rights if tok.dep_ in OBJECTS or (tok.pos_ == "PRON" and tok.lower_ == "me")])
return objs
def getObjsFromAttrs(deps):
for dep in deps:
if dep.pos_ == "NOUN" and dep.dep_ == "attr":
verbs = [tok for tok in dep.rights if tok.pos_ == "VERB"]
if len(verbs) > 0:
for v in verbs:
rights = list(v.rights)
objs = [tok for tok in rights if tok.dep_ in OBJECTS]
objs.extend(getObjsFromPrepositions(rights))
if len(objs) > 0:
return v, objs
return None, None
def getObjFromXComp(deps):
for dep in deps:
if dep.pos_ == "VERB" and dep.dep_ == "xcomp":
v = dep
rights = list(v.rights)
objs = [tok for tok in rights if tok.dep_ in OBJECTS]
objs.extend(getObjsFromPrepositions(rights))
if len(objs) > 0:
return v, objs
return None, None
def getAllSubs(v):
verbNegated = isNegated(v)
subs = [tok for tok in v.lefts if tok.dep_ in SUBJECTS and tok.pos_ != "DET"]
if len(subs) > 0:
subs.extend(getSubsFromConjunctions(subs))
else:
foundSubs, verbNegated = findSubs(v)
subs.extend(foundSubs)
return subs, verbNegated
def getAllObjs(v):
# rights is a generator
rights = list(v.rights)
objs = [tok for tok in rights if tok.dep_ in OBJECTS]
objs.extend(getObjsFromPrepositions(rights))
#potentialNewVerb, potentialNewObjs = getObjsFromAttrs(rights)
#if potentialNewVerb is not None and potentialNewObjs is not None and len(potentialNewObjs) > 0:
# objs.extend(potentialNewObjs)
# v = potentialNewVerb
potentialNewVerb, potentialNewObjs = getObjFromXComp(rights)
if potentialNewVerb is not None and potentialNewObjs is not None and len(potentialNewObjs) > 0:
objs.extend(potentialNewObjs)
v = potentialNewVerb
if len(objs) > 0:
objs.extend(getObjsFromConjunctions(objs))
return v, objs
def findSVOs(tokens):
svos = []
verbs = [tok for tok in tokens if tok.pos_ == "VERB" and tok.dep_ != "aux"]
for v in verbs:
subs, verbNegated = getAllSubs(v)
# hopefully there are subs, if not, don't examine this verb any longer
if len(subs) > 0:
v, objs = getAllObjs(v)
for sub in subs:
for obj in objs:
objNegated = isNegated(obj)
svos.append((sub.lower_, "!" + v.lower_ if verbNegated or objNegated else v.lower_, obj.lower_))
return svos
def printDeps(toks):
for tok in toks:
print(tok.orth_, tok.dep_, tok.pos_, tok.head.orth_, [t.orth_ for t in tok.lefts], [t.orth_ for t in tok.rights])
def testSVOs():
# nlp = English()
# nlp = she pacy.load("en_core_web_sm")
tok = nlp("making $12 an hour? where am i going to go? i have no other financial assistance available and he certainly won't provide support.")
svos = findSVOs(tok)
# printDeps(tok)
assert set(svos) == {('i', '!have', 'assistance'), ('he', '!provide', 'support')}
print(svos)
print('otoyoyoyoyooyoooYOYOY')
print("-----------------------------------------------")
tok = nlp("They ate the pizza with anchovies.")
svos = findSVOs(tok)
printDeps(tok)
print(svos)
assert set(svos) == {('they', 'ate', 'pizza')}
print("--------------------------------------------------")
tok = nlp("I have no other financial assistance available and he certainly won't provide support.")
svos = findSVOs(tok)
printDeps(tok)
print(svos)
assert set(svos) == {('i', '!have', 'assistance'), ('he', '!provide', 'support')}
print("--------------------------------------------------")
tok = nlp("I have no other financial assistance available, and he certainly won't provide support.")
svos = findSVOs(tok)
printDeps(tok)
print(svos)
assert set(svos) == {('i', '!have', 'assistance'), ('he', '!provide', 'support')}
print("--------------------------------------------------")
tok = nlp("he did not kill me")
svos = findSVOs(tok)
printDeps(tok)
print(svos)
assert set(svos) == {('he', '!kill', 'me')}
print("--------------------------------------------------")
tok = nlp("he told me i would die alone with nothing but my career someday")
svos = findSVOs(tok)
printDeps(tok)
print(svos)
assert set(svos) == {('he', 'told', 'me')}
print("--------------------------------------------------")
tok = nlp("I wanted to kill him with a hammer.")
svos = findSVOs(tok)
printDeps(tok)
print(svos)
assert set(svos) == {('i', 'kill', 'him')}
print("--------------------------------------------------")
tok = nlp("because he hit me and also made me so angry i wanted to kill him with a hammer.")
svos = findSVOs(tok)
printDeps(tok)
print(svos)
assert set(svos) == {('he', 'hit', 'me'), ('i', 'kill', 'him')}
print("--------------------------------------------------")
tok = nlp("he and his brother shot me")
svos = findSVOs(tok)
printDeps(tok)
print(svos)
assert set(svos) == {('he', 'shot', 'me'), ('brother', 'shot', 'me')}
print("--------------------------------------------------")
tok = nlp("the annoying person that was my boyfriend hit me")
svos = findSVOs(tok)
printDeps(tok)
print(svos)
assert set(svos) == {('person', 'was', 'boyfriend'), ('person', 'hit', 'me')}
print("--------------------------------------------------")
tok = nlp("the boy raced the girl who had a hat that had spots.")
svos = findSVOs(tok)
printDeps(tok)
print(svos)
assert set(svos) == {('boy', 'raced', 'girl'), ('who', 'had', 'hat'), ('hat', 'had', 'spots')}
print("--------------------------------------------------")
tok = nlp("he spit on me")
svos = findSVOs(tok)
printDeps(tok)
print(svos)
assert set(svos) == {('he', 'spit', 'me')}
print("--------------------------------------------------")
tok = nlp("he didn't spit on me")
svos = findSVOs(tok)
printDeps(tok)
print(svos)
assert set(svos) == {('he', '!spit', 'me')}
print("--------------------------------------------------")
tok = nlp("the boy raced the girl who had a hat that didn't have spots.")
svos = findSVOs(tok)
printDeps(tok)
print(svos)
assert set(svos) == {('boy', 'raced', 'girl'), ('who', 'had', 'hat'), ('hat', '!have', 'spots')}
print("--------------------------------------------------")
tok = nlp("he didn't spit on me and my child")
svos = findSVOs(tok)
printDeps(tok)
print(svos)
assert set(svos) == {('he', '!spit', 'me'), ('he', '!spit', 'child')}
print("--------------------------------------------------")
tok = nlp("he beat and hurt me")
svos = findSVOs(tok)
printDeps(tok)
print(svos)
def main():
nlp = spacy.load("en_core_web_sm")
# testSVOs()
inp = input("enter a sentence: ")
tok = nlp(inp)
svos = findSVOs(tok)
print(svos)
if __name__ == "__main__":
main()