1 """
2 Parser for the factvalue layer in KAF/NAF
3 """
4
5 from lxml import etree
6 from span_data import *
7
8
10
12 """
13 Constructor of the object
14 @type node: xml Element or None (to create and empty one)
15 @param node: this is the node of the element. If it is None it will create a new object
16 """
17 self.type = 'NAF/KAF'
18 if node is None:
19 self.node = etree.Element('factVal')
20 else:
21 self.node = node
22
24 """
25 Returns the node
26 """
27 return self.node
28
30 """
31 Returns the resource that defines the factuality value
32 """
33 return self.node.get('resource')
34
36 """
37 Sets the resource that defines the factuality value
38 @type r: string
39 @param r: the resource defining factuality
40 """
41 self.node.set('resource',r)
42
43
45 """
46 Returns the value of the factVal element
47 """
48 return self.node.get('value')
49
50
52 """
53 Sets the value for the factVal element
54 @type v: string
55 @param v: the value for the element
56 """
57 self.node.set('value',v)
58
60 """
61 Returns the confidence of the factVal element
62 """
63 return self.node.get('confidence')
64
65
67 """
68 Sets confidence for the factVal element
69 @type c: string
70 @param c: the value for the element
71 """
72 self.node.set('confidence',c)
73
74
76
77 - def __init__(self,node=None,type='NAF'):
78 """
79 Constructor of the object
80 @type node: xml Element or None (to create and empty one)
81 @param node: this is the node of the element. If it is None it will create a new object
82 @type type: string
83 @param type: the type of the object (KAF or NAF)
84 """
85 self.type = type
86 if node is None:
87 self.node = etree.Element('factuality')
88 else:
89 self.node = node
90
92 """
93 Returns the node
94 """
95 return self.node
96
97
99 """
100 Returns the identifier of the factuality element
101 """
102 return self.node.get('id')
103
105 """
106 Sets the id of the element
107 @type this_id: string
108 @param this_id: the resource defining factuality
109 """
110 self.node.set('id',this_id)
111
112
114 """
115 Returns the span of the factuality element
116 @type my_span: L{Cspan}
117 @param my_span: span object
118 """
119 span_obj = self.node.find('span')
120 if span_obj is not None:
121 return Cspan(span_obj)
122 return None
123
125 """
126 Sets the id of the element
127 @type this_id: L{Cspan}
128 @param this_id: the resource defining factuality
129 """
130 self.node.append(my_span.get_node())
131
133 """
134 Sets the id of the element
135 @type this_id: L{Cspan}
136 @param this_id: the resource defining factuality
137 """
138 self.node.append(fval.get_node())
139
140
142 """
143 Iterator to get the factuality values
144 @rtype: L{Cfactuality}
145 @return: iterator for getting the factuality's value objects
146 """
147 for node_pre in self.node.findall('factVal'):
148 yield Cfactval(node_pre)
149
150
152 """
153 This class represents the new factuality layer
154 """
155 - def __init__(self,node=None,type='NAF'):
156 """
157 Constructor of the object
158 @type node: xml ELement or None (to create an empty one)
159 @param node: this is the node of the element. If it is None it will create a new object
160 @type type: string
161 @param type: the type of the object (KAF or NAF), NAF is default
162 """
163 self.type = type
164 if node is None:
165 self.node = etree.Element('factualities')
166 else:
167 self.node = node
168
170 """
171 Returns the node of the element
172 @rtype: xml Element
173 @return: the node of the element
174 """
175 return self.node
176
177
179 """
180 Adds a factuality element to the layer
181 """
182 self.node.append(factval.get_node())
183
184
186 """
187 Iterator to get the factualities
188 @rtype: L{Cfactuality}
189 @return: iterator for getting the factuality objects
190 """
191 for node_pre in self.node.findall('factuality'):
192 yield Cfactuality(node_pre)
193
196
199
201 return dump(self.node)
202
203
204
205
206
208 """
209 This class encapsulates a factvalue object in KAF/NAF (old version)
210 """
212 """
213 Constructor of the object
214 @type node: xml Element or None (to create and empty one)
215 @param node: this is the node of the element.
216 If it is None it will create a new object
217 """
218 if node is None:
219 self.node = etree.Element('factvalue')
220 else:
221 self.node = node
222
224 """
225 Returns the node of the element
226 @rtype: xml Element
227 @return: the node of the element
228 """
229 return self.node
230
232 """
233 Returns the fact identifier
234 @rtype: string
235 @return: the fact identifier
236 """
237 return self.node.get('id')
238
240 """
241 Returns the prediction attribute of the factvalue
242 @rtype: string
243 @return: the prediction attribute
244 """
245 return self.node.get('prediction')
246
248 """
249 Returns the confidence of the element
250 @rtype: string
251 @return: the confidence of the element
252 """
253 return self.node.get('confidence')
254
256 """
257 Set the identifier for the fact
258 @type this_id: string
259 @param this_id: the identifier
260 """
261 return self.node.set('id',this_id)
262
264 """
265 Sets the prediction attribute
266 @type prediction: string
267 @param prediction: the prediction attribute
268 """
269 self.node.set('prediction',prediction)
270
272 """
273 Sets the confidence attribute
274 @type confidence: string
275 @param confidence: the confidence attribute
276 """
277 self.node.set('confidence',confidence)
278
280 return dump(self.node)
281
283 """
284 This class encapsulates the factvalue layer in KAF/NAF (old version)
285 """
287 """
288 Constructor of the object
289 @type node: xml Element or None (to create and empty one)
290 @param node: this is the node of the element.
291 If it is None it will create a new object
292 """
293 if node is None:
294 self.node = etree.Element('factualitylayer')
295 else:
296 self.node = node
297
299 """
300 Returns the node of the element
301 @rtype: xml Element
302 @return: the node of the element
303 """
304 return self.node
305
308
311
313 return dump(self.node)
314
316 for node_factvalue in self.node.findall('factvalue'):
317 yield node_factvalue
318
320 """
321 Iterator that returns all the factualitylayer in the layer
322 @rtype: L{Cfactvalue}
323 @return: list of factualitylayer (iterator)
324 """
325 for node in self.__get_factvalue_nodes():
326 yield Cfactvalue(node)
327
329 """
330 Adds a factvalue object to the layer
331 @type my_factvalue: L{Cfactvalue}
332 @param my_factvalue: the factvalue object to be added
333 """
334 self.node.append(my_factvalue.get_node())
335
337 """
338 Removes the factvalue for the given factvalue identifier
339 @type factvalue_id: string
340 @param factvalue_id: the factvalue identifier to be removed
341 """
342 for fact in self.get_factvalues():
343 if fact.get_id() == factvalue_id:
344 self.node.remove(fact.get_node())
345 break
346