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
12 """
13 This class encapsulates the holder of the opinions
14 """
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
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
43
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
56 return dump(self.node)
57
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
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
88
96
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
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
117 return dump(self.node)
118
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
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
149
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
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
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
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
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
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
202 return dump(self.node)
203
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
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
238
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
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
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
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
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
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
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
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
322 return dump(self.node)
323
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
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
355 for node in self.node.findall('opinion'):
356 yield node
357
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
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
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
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
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
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