-
Notifications
You must be signed in to change notification settings - Fork 1
/
handles.py
55 lines (41 loc) · 1.42 KB
/
handles.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
from network import Concept
from nltk.stem.porter import PorterStemmer
from nltk.stem.snowball import SnowballStemmer
ps = SnowballStemmer("english")
def handle_cop(node, subj, nodes):
vbz = ps.stem(node["word"])
cop = get_concept(nodes[node["head"]], nodes)
subj[vbz] = cop
def handle_dep(node, obj, nodes):
verb = ps.stem(nodes[node["head"]]["word"])
dobj = get_concept(node, nodes)
dobj[verb] = obj
def handle_dobj(node, subj, nodes):
verb = ps.stem(nodes[node["head"]]["word"])
dobj = get_concept(node, nodes)
subj[verb] = dobj
def get_concept(node, nodes):
amods = None
if "amod" in node["deps"].keys():
amods = [ps.stem(nodes[mod]["word"]) for mod in node["deps"]["amod"]]
return Concept.get(ps.stem(node["word"]), amods)
def from_root(root, nodes, prev_subj=None):
if "nsubj" in root["deps"]:
subj = get_concept(nodes[root["deps"]["nsubj"][0]], nodes)
elif "nsubjpass" in root["deps"]:
subj = get_concept(nodes[root["deps"]["nsubjpass"][0]], nodes)
else:
return
# Referring to previous sentence's subject
if subj.name in ["they", "it"]:
subj = prev_subj or subj
for key, fn in handles.items():
if key in root["deps"]:
for node in root["deps"][key]:
fn(nodes[node], subj, nodes)
return subj
handles = {
"cop" : handle_cop,
"dep" : handle_dep,
"dobj" : handle_dobj
}