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