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

Source Code for Module opinion_data

  1  """ 
  2  This is a parser for opinions in KAF/NAF files 
  3  """ 
  4   
  5  from lxml import etree 
  6  from lxml.objectify import dump 
  7  from span_data import * 
  8   
  9   
 10   
11 -class Cholder:
12 """ 13 This class encapsulates the holder of the opinions 14 """
15 - def __init__(self,node=None):
16 """ 17 Constructor of the object 18 @type node: xml Element or None (to create and empty one) 19 @param node: this is the node of the element. If it is None it will create a new object 20 """ 21 self.type = 'NAF/KAF' 22 if node is None: 23 self.node = etree.Element('opinion_holder') 24 else: 25 self.node = node
26
27 - def set_span(self,my_span):
28 """ 29 Sets the span with the provided span object 30 @type my_span: L{Cspan} 31 @param my_span: span object 32 """ 33 self.node.append(my_span.get_node())
34
35 - def set_comment(self,c):
36 """ 37 Sets the comment for the element 38 @type c: string 39 @param c: comment for the element 40 """ 41 c = ' '+c.replace('-','').strip()+' ' 42 self.node.insert(0,etree.Comment(c) )
43
44 - def get_span(self):
45 """ 46 Returns the span of the object 47 @rtype: L{Cspan} 48 @return: the span object 49 """ 50 span_obj = self.node.find('span') 51 if span_obj is not None: 52 return Cspan(span_obj) 53 return None
54
55 - def __str__(self):
56 return dump(self.node)
57
58 - def get_node(self):
59 """ 60 Returns the node of the element 61 @rtype: xml Element 62 @return: the node of the element 63 """ 64 return self.node
65 66
67 -class Ctarget:
68 - def __init__(self,node=None):
69 """ 70 Constructor of the object 71 @type node: xml Element or None (to create and empty one) 72 @param node: this is the node of the element. If it is None it will create a new object 73 """ 74 self.type = 'NAF/KAF' 75 if node is None: 76 self.node = etree.Element('opinion_target') 77 else: 78 self.node = node
79
80 - def set_comment(self,c):
81 """ 82 Sets the comment for the element 83 @type c: string 84 @param c: comment for the element 85 """ 86 c = ' '+c.replace('-','').strip()+' ' 87 self.node.insert(0,etree.Comment(c) )
88
89 - def get_comment(self):
90 """ 91 Returns the comment 92 @rtype: string 93 @return: the comment 94 """ 95 return self.node_comment
96
97 - def set_span(self,my_span):
98 """ 99 Sets the span with the provided span object 100 @type my_span: L{Cspan} 101 @param my_span: span object 102 """ 103 self.node.append(my_span.get_node())
104
105 - def get_span(self):
106 """ 107 Returns the span of the object 108 @rtype: L{Cspan} 109 @return: the span object 110 """ 111 span_obj = self.node.find('span') 112 if span_obj is not None: 113 return Cspan(span_obj) 114 return None
115
116 - def __str__(self):
117 return dump(self.node)
118
119 - def get_node(self):
120 """ 121 Returns the node of the element 122 @rtype: xml Element 123 @return: the node of the element 124 """ 125 return self.node
126 127
128 -class Cexpression:
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 self.type = 'NAF/KAF' 136 if node is None: 137 self.node = etree.Element('opinion_expression') 138 else: 139 self.node = node
140
141 - def set_comment(self,c):
142 """ 143 Sets the comment for the element 144 @type c: string 145 @param c: comment for the element 146 """ 147 c = ' '+c.replace('-','').strip()+' ' 148 self.node.insert(0,etree.Comment(c))
149
150 - def set_polarity(self,pol):
151 """ 152 Sets the polarity for the expression 153 @type pol: string 154 @param pol: polarity for the expression 155 """ 156 self.node.set('polarity',pol)
157
158 - def get_polarity(self):
159 """ 160 Returns the polarity for the expression 161 @rtype: string 162 @return: the polarity for the expression 163 """ 164 return self.node.get('polarity')
165
166 - def set_strength(self,st):
167 """ 168 Sets the strength for the expression 169 @type st: string 170 @param st: strength for the expression 171 """ 172 self.node.set('strength',st)
173
174 - def get_strength(self):
175 """ 176 Returns the strength for the expression 177 @rtype: string 178 @return: the strength for the expression 179 """ 180 return self.node.get('strength')
181
182 - def set_span(self,my_span):
183 """ 184 Sets the span with the provided span object 185 @type my_span: L{Cspan} 186 @param my_span: span object 187 """ 188 self.node.append(my_span.get_node())
189
190 - def get_span(self):
191 """ 192 Returns the span of the object 193 @rtype: L{Cspan} 194 @return: the span object 195 """ 196 span_obj = self.node.find('span') 197 if span_obj is not None: 198 return Cspan(span_obj) 199 return None
200
201 - def __str__(self):
202 return dump(self.node)
203
204 - def get_node(self):
205 """ 206 Returns the node of the element 207 @rtype: xml Element 208 @return: the node of the element 209 """ 210 return self.node
211
212 -class Copinion:
213 """ 214 This class encapsulates KAF/NAF opinion elements 215 """
216 - def __init__(self,node=None,type='NAF'):
217 """ 218 Constructor of the object 219 @type node: xml Element or None (to create and empty one) 220 @param node: this is the node of the element. If it is None it will create a new object 221 @type type: string 222 @param type: the type of the object (KAF or NAF) 223 """ 224 self.type = type 225 if node is None: 226 self.node = etree.Element('opinion') 227 else: 228 self.node = node
229
230 - def set_comment(self,c):
231 """ 232 Sets the comment for the element 233 @type c: string 234 @param c: comment for the element 235 """ 236 c = ' '+c.replace('-','').strip()+' ' 237 self.node.insert(0,etree.Comment(c) )
238
239 - def set_id(self,my_id):
240 """ 241 Sets the opinion identifier 242 @type my_id: string 243 @param my_id: the opinion identifier 244 """ 245 if self.type == 'NAF': 246 self.node.set('id',my_id) 247 elif self.type == 'KAF': 248 self.node.set('oid',my_id)
249
250 - def get_id(self):
251 """ 252 Returns the opinion identifier 253 @rtype: string 254 @return: the opinion identifier 255 """ 256 if self.type == 'NAF': 257 return self.node.get('id') 258 elif self.type == 'KAF': 259 return self.node.get('oid')
260
261 - def set_holder(self,hol):
262 """ 263 Sets the opinion holder 264 @type hol: L{Cholder} 265 @param hol: the opinion holder 266 """ 267 self.node.append(hol.get_node())
268
269 - def get_holder(self):
270 """ 271 Returns the opinion holder 272 @rtype: L{Cholder} 273 @return: the opinion holder 274 """ 275 node_hol = self.node.find('opinion_holder') 276 if node_hol is not None: 277 return Cholder(node_hol) 278 else: 279 return None
280
281 - def set_target(self,tar):
282 """ 283 Sets the opinion target 284 @type tar: L{Ctarget} 285 @param tar: the opinion target 286 """ 287 self.node.append(tar.get_node())
288
289 - def get_target(self):
290 """ 291 Returns the opinion target 292 @rtype: L{Ctarget} 293 @return: the opinion target 294 """ 295 node_target = self.node.find('opinion_target') 296 if node_target is not None: 297 return Ctarget(node_target) 298 else: 299 return None
300
301 - def set_expression(self,exp):
302 """ 303 Sets the opinion expression 304 @type exp: L{Cexpression} 305 @param exp: the opinion expression 306 """ 307 self.node.append(exp.get_node())
308
309 - def get_expression(self):
310 """ 311 Returns the opinion expression 312 @rtype: L{Cexpression} 313 @return: the opinion expression 314 """ 315 node_exp = self.node.find('opinion_expression') 316 if node_exp is not None: 317 return Cexpression(node_exp) 318 else: 319 return None
320
321 - def __str__(self):
322 return dump(self.node)
323
324 - def get_node(self):
325 """ 326 Returns the node of the element 327 @rtype: xml Element 328 @return: the node of the element 329 """ 330 return self.node
331 332 333 334 335
336 -class Copinions:
337 """ 338 This class encapsulates the opinion layer in KAF/NAF (set of opinions) 339 """
340 - def __init__(self,node=None,type='NAF'):
341 """ 342 Constructor of the object 343 @type node: xml Element or None (to create and empty one) 344 @param node: this is the node of the element. If it is None it will create a new object 345 @type type: string 346 @param type: the type of the object (KAF or NAF) 347 """ 348 self.type = type 349 if node is None: 350 self.node = etree.Element('opinions') 351 else: 352 self.node = node
353
354 - def __get_opinion_nodes(self):
355 for node in self.node.findall('opinion'): 356 yield node
357
358 - def get_opinions(self):
359 """ 360 Iterator that returns all the opinions in the layer 361 @rtype: L{Copinion} 362 @return: iterator for the opinions 363 """ 364 for node in self.__get_opinion_nodes(): 365 yield Copinion(node,self.type)
366
367 - def to_kaf(self):
368 """ 369 Converts the opinion layer to KAF 370 """ 371 if self.type == 'NAF': 372 for node in self.__get_opinion_nodes(): 373 node.set('oid',node.get('id')) 374 del node.attrib['id']
375
376 - def to_naf(self):
377 """ 378 Converts the opinion layer to NAF 379 """ 380 if self.type == 'KAF': 381 for node in self.__get_opinion_nodes(): 382 node.set('id',node.get('oid')) 383 del node.attrib['oid']
384 385
386 - def add_opinion(self,opi_obj):
387 """ 388 Adds the opinion object to the layer 389 @type opi_obj: L{Copinion} 390 @param opi_obj: the opinion object 391 """ 392 self.node.append(opi_obj.get_node())
393
394 - def get_node(self):
395 """ 396 Returns the node of the element 397 @rtype: xml Element 398 @return: the node of the element 399 """ 400 return self.node
401
402 - def remove_this_opinion(self,opinion_id):
403 """ 404 Removes the opinion for the given opinion identifier 405 @type opinion_id: string 406 @param opinion_id: the opinion identifier to be removed 407 """ 408 for opi in self.get_opinions(): 409 if opi.get_id() == opinion_id: 410 self.node.remove(opi.get_node()) 411 break
412