1 """
2 Parser for the semantic role labelling layer in KAF/NAF
3 """
4
5 from span_data import *
6 from external_references_data import *
7 from lxml import etree
8
10 """
11 This class encapsulates a single role in the layer
12 """
14 """
15 Constructor of the object
16 @type node: xml Element or None (to create and empty one)
17 @param node: this is the node of the element. If it is None it will create a new object
18 """
19 if node is None:
20 self.node = etree.Element('role')
21 else:
22 self.node = node
23
25 """
26 Returns the node of the element
27 @rtype: xml Element
28 @return: the node of the element
29 """
30 return self.node
31
33 """
34 Returns the identifier of the element
35 @rtype: string
36 @return: the identifier of the element
37 """
38 return self.node.get('id')
39
40
42 """
43 Sets the identifier of the role
44 @param i: the identififier of the role
45 @type i: string
46 """
47 self.node.set('id',i)
48
50 """
51 Returns the semRole attribute of the element
52 @rtype: string
53 @return: the semRole of the element
54 """
55 return self.node.get('semRole')
56
57
59 """
60 Sets the semantic role
61 @param sRole: the semantic role
62 @type sRole: string
63 """
64 self.node.set('semRole',sRole)
65
67 """
68 Returns the external references of the element
69 @rtype: L{CexternalReference}
70 @return: the external references (iterator)
71 """
72 node = self.node.find('externalReferences')
73 if node is not None:
74 ext_refs = CexternalReferences(node)
75 for ext_ref in ext_refs:
76 yield ext_ref
77
79 """
80 Adds an external reference to the role
81 @param ext_ref: the external reference object
82 @type ext_ref: L{CexternalReference}
83 """
84
85 node_ext_refs = self.node.find('externalReferences')
86 ext_refs = None
87 if node_ext_refs == None:
88 ext_refs = CexternalReferences()
89 self.node.append(ext_refs.get_node())
90 else:
91 ext_refs = CexternalReferences(node_ext_refs)
92
93 ext_refs.add_external_reference(ext_ref)
94
95
97 """
98 Removes any external reference from the role
99 """
100 for ex_ref_node in self.node.findall('externalReferences'):
101 self.node.remove(ex_ref_node)
102
104 """
105 Returns the span of the role
106 @rtype: L{Cspan}
107 @return: the span object of the element
108 """
109 node = self.node.find('span')
110 if node is not None:
111 return Cspan(node)
112 else:
113 return None
114
115
117 """
118 Sets the span for the role
119 @type this_span: L{Cspan}
120 @param this_span: the span object
121 """
122 self.node.append(this_span.get_node())
123
125 """
126 Class defining predicates
127 """
128
130 """
131 Constructor of the object
132 @type node: xml Element or None (to create and empty one)
133 @param node: this is the node of the element. If it is None it will create a new object
134 """
135 if node is None:
136 self.node = etree.Element('predicate')
137 else:
138 self.node = node
139
140
142 """
143 Returns the node of the element
144 @rtype: xml Element
145 @return: the node of the element
146 """
147 return self.node
148
150 """
151 Returns the identifier of the element
152 @rtype: string
153 @return: the identifier of the element
154 """
155 return self.node.get('id')
156
158 """
159 Assigns the identifier to the element
160 @param i: the identififier of the predicate
161 @type i: string
162 """
163 self.node.set('id',i)
164
166 """
167 Returns the URI of the element
168 @rtype: string
169 @return: the URI of the element
170 """
171 return self.node.get('uri')
172
174 """
175 Assigns the URI to the element
176 @param uri: the uri of the predicate
177 @type uri: string
178 """
179 self.node.set('uri',uri)
180
182 """
183 Returns the confidence of the element
184 @rtype: string
185 @return: the confidence of the element
186 """
187 return self.node.get('confidence')
188
190 """
191 Assigns the confidence to the element
192 @param conf: the confidence of the predicate
193 @type conf: string
194 """
195 self.node.set('uri',uri)
196
198 """
199 Returns the span object of the element
200 @rtype: L{Cspan}
201 @return: the span object of the element
202 """
203 node = self.node.find('span')
204 if node is not None:
205 return Cspan(node)
206 else:
207 return None
208
210 """
211 Sets the span for the predicate
212 @type this_span: L{Cspan}
213 @param this_span: the span object
214 """
215 self.node.append(this_span.get_node())
216
218 """
219 Iterator to get the external references
220 @rtype: L{CexternalReference}
221 @return: iterator for external references
222 """
223 node = self.node.find('externalReferences')
224 if node is not None:
225 ext_refs = CexternalReferences(node)
226 for ext_ref in ext_refs:
227 yield ext_ref
228
230 """
231 Adds an external reference to the role
232 @param ext_ref: the external reference object
233 @type ext_ref: L{CexternalReference}
234 """
235
236 node_ext_refs = self.node.find('externalReferences')
237 ext_refs = None
238 if node_ext_refs == None:
239 ext_refs = CexternalReferences()
240 self.node.append(ext_refs.get_node())
241 else:
242 ext_refs = CexternalReferences(node_ext_refs)
243
244 ext_refs.add_external_reference(ext_ref)
245
247 """
248 Removes any external reference from the predicate
249 """
250 for ex_ref_node in self.node.findall('externalReferences'):
251 self.node.remove(ex_ref_node)
252
253
255 """
256 Removes any external references on any of the roles from the predicate
257 """
258
259 for node_role in self.node.findall('role'):
260 role = Crole(node_role)
261 role.remove_external_references()
262
264 """
265 Iterator to get the roles
266 @rtype: L{Crole}
267 @return: iterator for getting the role objects
268 """
269 for node_role in self.node.findall('role'):
270 yield Crole(node_role)
271
273 """
274 Adds a list of roles to the predicate
275 @type list_of_roles: list
276 @param list_of_roles: list of roles
277 """
278 for role in list_of_roles:
279 role_node = role.get_node()
280 self.node.append(role_node)
281
283 """
284 Add a role to the predicate
285 @type role_obj: L{Crole}
286 @param role_obj: the role object
287 """
288 role_node = role_obj.get_node()
289 self.node.append(role_node)
290
292 """
293 This class encapsulates the semantic role labelling layer
294 """
295 - def __init__(self,node=None,type='NAF'):
296 """
297 Constructor of the object
298 @type node: xml Element or None (to create and empty one)
299 @param node: this is the node of the element. If it is None it will create a new object
300 @type type: string
301 @param type: the type of the object (KAF or NAF)
302 """
303 self.type = type
304 self.idx = {}
305 if node is None:
306 self.node = etree.Element('srl')
307 else:
308 self.node = node
309 for node_pred in self.__get_node_preds():
310 pred_obj = Cpredicate(node_pred)
311 self.idx[pred_obj.get_id()] = node_pred
312
313
314 self.map_roleid_node = {}
315 for predicate in self.get_predicates():
316 for role in predicate.get_roles():
317 self.map_roleid_node[role.get_id()] = role.get_node()
318
319
321 """
322 Returns the node of the element
323 @rtype: xml Element
324 @return: the node of the element
325 """
326 return self.node
327
329 for node_p in self.node.findall('predicate'):
330 yield node_p
331
333 """
334 Iterator to get the roles
335 @rtype: L{Cpredicate}
336 @return: iterator for getting the predicate objects
337 """
338 for node_pre in self.node.findall('predicate'):
339 yield Cpredicate(node_pre)
340
342 """
343 Adds an external reference to a role identifier
344 @param role_id: the role identifier
345 @type role_id: string
346 @param ext_ref: the external reference
347 @type ext_ref: L{CexternalReference}
348 """
349 node_role = self.map_roleid_node[role_id]
350 obj_role = Crole(node_role)
351 obj_role.add_external_reference(ext_ref)
352
353
355 """
356 Adds a predicate object to the layer
357 @type pred_obj: L{Cpredicate}
358 @param pred_obj: the predicate object
359 """
360 pred_id = pred_obj.get_id()
361 if not pred_id in self.idx:
362 pred_node = pred_obj.get_node()
363 self.node.append(pred_node)
364 self.idx[pred_id] = pred_node
365 else:
366
367 print 'Error: trying to add new element, but id has already been given'
368