forked from cltl/KafNafParserPy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
temporal_data.py
367 lines (307 loc) · 9.37 KB
/
temporal_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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
"""
Parser for the tlink layer in KAF/NAF
"""
from lxml import etree
class Ctlink:
"""
This class encapsulates a tlink object in KAF/NAF
"""
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('tlink')
else:
self.node = node
def get_node_comment(self):
"""
Returns the lxml element for the comment
@rtype: lxml Element
@return: the lxml element for the comment
"""
return self.node_comment
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 token identifier
@rtype: string
@return: the token identifier
"""
return self.node.get('id')
def get_from(self):
"""
Returns the from attribute of the tlink
@rtype: string
@return: the from attribute
"""
return self.node.get('from')
def get_fromType(self):
"""
Returns the from attribute of the tlink
@rtype: string
@return: the from attribute
"""
return self.node.get('fromType')
def get_to(self):
"""
Returns the to attribute of the tlink
@rtype: string
@return: the to attribute
"""
return self.node.get('to')
def get_toType(self):
"""
Returns the to attribute of the tlink
@rtype: string
@return: the to attribute
"""
return self.node.get('toType')
def get_relType(self):
"""
Returns the to attribute of the tlink
@rtype: string
@return: the to attribute
"""
return self.node.get('relType')
def set_id(self,this_id):
"""
Set the identifier for the token
@type this_id: string
@param this_id: the identifier
"""
return self.node.set('id',this_id)
def set_from(self, f):
"""
Sets the from attribute
@type f: string
@param f: the from attribute
"""
self.node.set('from',f)
def set_fromType(self, f):
"""
Sets the from attribute
@type f: string
@param f: the from attribute
"""
self.node.set('fromType',f)
def set_to(self,t):
"""
Sets the to attribute
@type t: string
@param t: the to attribute
"""
self.node.set('to',t)
def set_toType(self,t):
"""
Sets the to attribute
@type t: string
@param t: the to attribute
"""
self.node.set('toType',t)
def set_relType(self,t):
"""
Sets the to attribute
@type t: string
@param t: the to attribute
"""
self.node.set('relType',t)
def set_comment(self,c):
"""
Sets the XML comment for the tlink
@type c: string
@param c: the string comment
"""
c = c.replace('--','- -')
self.node.insert(0,etree.Comment(c))
def __str__(self):
return dump(self.node)
class CpredicateAnchor:
"""
This class encapsulates the predicateAnchor object in KAF/NAF
"""
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('predicateAnchor')
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 set_id(self,this_id):
"""
Set the identifier for the token
@type this_id: string
@param this_id: the identifier
"""
return self.node.set('id',this_id)
def get_id(self):
"""
Returns the token identifier
@rtype: string
@return: the token identifier
"""
return self.node.get('id')
def get_anchorTime(self):
"""
Returns the anchorTime
@rtype: string
@return: the anchorTime
"""
return self.node.get('anchorTime')
def set_anchorTime(self,anchorTime):
"""
Set the anchor time for the event
@type anchorTime: string
@param anchorTime: the anchorTime id
"""
return self.node.set('anchorTime',anchorTime)
def get_endPoint(self):
"""
Returns the endPoint
@rtype: string
@return: the endPoint
"""
return self.node.get('endPoint')
def set_endPoint(self,endPoint):
"""
Set the endPoint for the event
@type endPoint: string
@param endPoint: the endPoint id
"""
return self.node.set('endPoint',endPoint)
def get_beginPoint(self):
"""
Returns the beginPoint
@rtype: string
@return: the beginPoint
"""
return self.node.get('beginPoint')
def set_beginPoint(self,beginPoint):
"""
Set the beginPoint for the event
@type beginPoint: string
@param beginPoint: the beginPoint id
"""
return self.node.set('beginPoint',beginPoint)
def get_span(self):
"""
Returns the span object of the term
@rtype: L{Cspan}
@return: the term span
"""
node_span = self.node.find('span')
if node_span is not None:
return Cspan(node_span)
else:
return None
def set_span(self,this_span):
"""
Sets the span for the lemma
@type this_span: L{Cspan}
@param this_span: lemma identifier
"""
self.node.append(this_span.get_node())
class CtemporalRelations:
"""
This class encapsulates the tlink layer in KAF/NAF
"""
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('temporalRelations')
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_node_temporalRelations(self):
for node_tlink in self.node.findall('tlink'):
yield node_tlink
def __get_node_predicateAnchors(self):
for node_predAnch in self.node.findall('predicateAnchor'):
yield node_predAnch
def get_tlinks(self):
"""
Iterator that returns all the temporalRelations in the layer
@rtype: L{Ctlink}
@return: list of temporalRelations (iterator)
"""
for node in self.__get_node_temporalRelations():
yield Ctlink(node)
def get_predicateAnchors(self):
"""
Iterator that returns all the temporalRelation anchors in the layer
@rtype: L{CpredicateAnchor}
@return: list of temporalRelations (iterator)
"""
for node in self.__get_node_predicateAnchors():
yield CpredicateAnchor(node)
def add_tlink(self,my_tlink):
"""
Adds a tlink object to the layer
@type my_tlink: L{Ctlink}
@param my_tlink: the tlink object to be added
"""
self.node.append(my_tlink.get_node())
def remove_this_tlink(self,tlink_id):
"""
Removes the tlink for the given tlink identifier
@type tlink_id: string
@param tlink_id: the tlink identifier to be removed
"""
for tlink in self.get_tlinks():
if tlink.get_id() == tlink_id:
self.node.remove(tlink.get_node())
break
def add_predicateAnchor(self,my_predAnch):
"""
Adds a predAnch object to the layer
@type my_predAnch: L{CpredAnch}
@param my_predAnch: the predAnc object to be added
"""
self.node.append(my_predAnch.get_node())
def remove_this_predicateAnchor(self,predAnch_id):
"""
Removes the predicate anchor for the given predicate anchor identifier
@type predAnch_id: string
@param predAnch_id: the predicate anchor identifier to be removed
"""
for predAnch in self.get_predicateAnchors():
if predAnch.get_id() == predAnch_id:
self.node.remove(predAnch.get_node())
break