Module srl_data
[hide private]
[frames] | no frames]

Source Code for Module srl_data

  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   
9 -class Crole:
10 """ 11 This class encapsulates a single role in the layer 12 """
13 - def __init__(self,node=None):
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
24 - def get_node(self):
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
32 - def get_id(self):
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
41 - def set_id(self, i):
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
49 - def get_sem_role(self):
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
58 - def set_sem_role(self, sRole):
59 """ 60 Sets the semantic role 61 @param sRole: the semantic role 62 @type sRole: string 63 """ 64 self.node.set('semRole',sRole)
65
66 - def get_external_references(self):
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
78 - def add_external_reference(self,ext_ref):
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 #check if the externalreferences sublayer exist for the role, and create it in case 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
103 - def get_span(self):
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
116 - def set_span(self, this_span):
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
124 -class Cpredicate:
125 """ 126 Class defining predicates 127 """ 128
129 - def __init__(self,node=None):
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
141 - def get_node(self):
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
149 - def get_id(self):
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
157 - def set_id(self, i):
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
165 - def get_uri(self):
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
173 - def set_uri(self, uri):
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
181 - def get_confidence(self):
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
189 - def set_confidence(self, conf):
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
197 - def get_span(self):
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
209 - def set_span(self, this_span):
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
217 - def get_external_references(self):
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
229 - def add_external_reference(self,ext_ref):
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 #check if the externalreferences sublayer exist for the role, and create it in case 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
263 - def get_roles(self):
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
272 - def add_roles(self, list_of_roles):
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
282 - def add_role(self, role_obj):
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
291 -class Csrl:
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 # Create a mapping role id --> node 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
320 - def get_node(self):
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
328 - def __get_node_preds(self):
329 for node_p in self.node.findall('predicate'): 330 yield node_p
331
332 - def get_predicates(self):
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
341 - def add_external_reference_to_role(self,role_id,ext_ref):
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
354 - def add_predicate(self, pred_obj):
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 #FIXME we want new id rather than ignoring the element 367 print 'Error: trying to add new element, but id has already been given'
368