-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpropbank.py
196 lines (162 loc) · 7.35 KB
/
propbank.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
"""verbnetparser.py
Adaptation of the VerbNet xml parser, loads PropBank instead. This is designed to have as close to the same functionality
so that programs can work with either resource as necessary
"""
import os
import bs4
import re
import json
import config
import verbnet
VN_RE = r"([1-9][0-9]?[0-9]?([.-][0-9]+)+)"
class PropBankParser(object):
"""Parse PropBank XML files, and turn them into a list of BeautifulSoup
objects"""
def __init__(self, directory=None, version="unified"):
PROPBANK_PATH = directory
fnames = [f for f in os.listdir(PROPBANK_PATH) if f.endswith(".xml")]
self.filenames = [os.path.join(PROPBANK_PATH, fname) for fname in fnames]
self.version = version
self.vn2pb = json.load(open(config.EXTERNAL_VN2PB_PATH))
self.pb2vn = {}
for key in self.vn2pb:
for value in self.vn2pb[key]:
if value not in self.pb2vn:
self.pb2vn[value] = []
self.pb2vn[value].append(key)
self.parsed_files = self.parse_files()
self.frame_dict = {}
self.rolesets = {}
for parse in self.parsed_files:
for roleset in parse.findAll("roleset"):
pb_roleset = PropBankRoleset(roleset, version)
self.rolesets[pb_roleset.ID] = pb_roleset
def parse_files(self):
"""Parse a list of XML files using BeautifulSoup. Returns list of parsed
soup objects"""
parsed_files = []
for fname in self.filenames:
parsed_files.append(bs4.BeautifulSoup(open(fname, encoding="utf-8"), "lxml-xml"))
return parsed_files
def get_pb_vn_mappings(self, verbnet):
vn_members = [member.name for member in verbnet.get_members()]
res = {}
# Initial population of rolesets
for roleset in self.rolesets:
res[roleset] = {}
if not self.rolesets[roleset].vnc:
if roleset in self.pb2vn:
print("roleset found in mapping file;;" + roleset + str(self.pb2vn[roleset]))
self.rolesets[roleset].vnc = ["-".join(vnc.split("-")[1:]) for vnc in self.pb2vn[roleset]]
elif roleset.split(".")[0] in vn_members:
print ("no vn mapping, but it's in verbnet;;" + roleset)
for vnc in self.rolesets[roleset].vnc:
new_c = verbnet.find_correct_subclass(vnc, roleset.split(".")[0])
if not new_c:
print ("class not found;;" + roleset + ";;" + vnc)
elif new_c == vnc:
res[roleset][vnc] = {}
print ("class is good;;" + roleset + ";;" + vnc)
else:
res[roleset][new_c] = {}
print ("class/member found, update;;" + roleset + ";;" + vnc + ";;" + new_c)
if len(res[roleset]):
for arg in self.rolesets[roleset].role_mappings:
for vnc in res[roleset]:
if vnc in self.rolesets[roleset].role_mappings[arg]:
res[roleset][vnc][arg] = self.rolesets[roleset].role_mappings[arg][vnc]
# Filling in role mappings from identical roleset mappings
# Implements the assumption that if two PB rolesets for the same verb map to the same class,
# their role mappings will also be identical
for roleset in res:
for roleset2 in res:
if roleset.split(".")[0] == roleset2.split(".")[0]:
for mapping in res[roleset]:
if mapping in res[roleset2]:
for arg in res[roleset][mapping]:
res[roleset2][mapping][arg] = res[roleset][mapping][arg]
# Removing empty mappings
res = {r:res[r] for r in res if res[r] != {}}
return res
def write_pb_vn_mappings(self, verbnet):
mapping_dict = self.get_pb_vn_mappings(verbnet)
print (mapping_dict)
class AbstractXML(object):
"""Abstract class to be inherited by other classes that share the same
features"""
def __init__(self, soup):
self.soup = soup
def get_category(self, cat, special_soup=None):
"""Extracts the category from a soup, with the option to specify a soup.
For MEMBERs, we have:
name (lexeme),
wn (WordNet category)
grouping (PropBank grouping)"""
if not special_soup:
special_soup = self.soup
try:
return special_soup.get(cat)
except AttributeError:
return []
class PropBankRoleset(AbstractXML):
def __init__(self, soup, version="unified"):
super().__init__(soup)
self.ID = self.get_category("id", self.soup)
self.name, self.framenet, self.vnc = None, [], []
self.role_mappings = self.populate_role_mappings()
if version == "unified":
self.name = self.get_category("name", self.soup)
alias = self.soup.find_all("alias")
self.framenet = self.get_category("framenet", alias)
self.vnc = self.get_category("verbnet", alias)
if not self.vnc:
self.parse_extra_vnc()
else:
try:
self.name = self.get_category("name", self.soup)
except IndexError as e:
pass
try:
self.framenet = self.get_category("framnet", self.soup)
self.vnc = self.get_category("vncls", self.soup)
except AttributeError as e:
pass
def __repr__(self):
return str([self.ID, self.framenet, self.vnc, self.name])
def populate_role_mappings(self):
rms = {}
for role in self.soup.find_all("role"):
role_number = "ARG" + self.get_category("n", role)
rms[role_number] = {}
for vn_role in role.find_all("vnrole"):
vntheta = self.get_category("vntheta", vn_role).lower()
vncls = self.get_category("vncls", vn_role)
rms[role_number][vncls] = vntheta
return rms
def parse_extra_vnc(self):
"""
Accounts for cases where the PB file doesn't explicitly map to VN, but the class is in the roles or the notes
"""
try:
vnc_cands = set()
for note in self.soup.find_all("note"):
vnc_cands.update([g[0] for g in re.findall(VN_RE, note.text)])
for vnrole in self.soup.find_all("vnrole"):
vnc_cands.add(vnrole.get("vncls"))
self.vnc = vnc_cands
except IndexError:
return
def test_vn_compatability(vn_path=config.VN_RESOURCE_PATH, mappings_path="../instances/pb-vn2.json"):
vn = verbnet.VerbNetParser(directory=vn_path)
with open(mappings_path) as f:
pb2vn = json.load(f)
verbnet_classes = set(vn.verb_classes_numerical_dict)
semlink_classes = {vncls for pbroleset, vnclasses in pb2vn.items() for vncls in vnclasses}
print ("classes in semlink mapping but not vn version: " + str(len(semlink_classes-verbnet_classes)))
def generate(vn_path=config.VN_RESOURCE_PATH, pb_path=config.PB_RESOURCE_PATH):
vn = verbnet.VerbNetParser(directory=vn_path)
pb = PropBankParser(directory=pb_path)
res = pb.get_pb_vn_mappings(vn)
json.dump(res, open("../instances/pb-vn2.json", "w"))
if __name__ == "__main__":
test_vn_compatability()