forked from cltl/KafNafParserPy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
constituency_data.py
387 lines (330 loc) · 10.1 KB
/
constituency_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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
"""
This module parses the constituency tree layer in KAF/NAF
"""
from lxml import etree
from lxml.objectify import dump
from span_data import Cspan
class Cnonterminal:
"""
This class encapsulates a non terminal object
"""
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('nt')
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 identifier of the object
@rtype: string
@return: the non terminal identifier
"""
return self.node.get('id')
def set_id(self,this_id):
"""
Sets the identifier for the element
@param this_id: identifier
@type this_id: string
"""
self.node.set('id',this_id)
def get_label(self):
"""
Returns the label of the object
@rtype: string
@return: the label of the object
"""
return self.node.get('label')
def set_label(self,label):
"""
Sets the label of the non terminal
@param label: label
@type label: string
"""
self.node.set('label',label)
def __str__(self):
return dump(self.node)
class Cterminal:
"""
This class encapsulates a terminal object
"""
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('t')
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 identifier of the object
@rtype: string
@return: identifier of the terminal object
"""
return self.node.get('id')
def set_id(self,this_id):
"""
Sets the identifier for the element
@param this_id: identifier
@type this_id: string
"""
self.node.set('id',this_id)
def get_span(self):
"""
Returns the term span of the object
@rtype: L{Cspan}
@return: the span object
"""
span_node = self.node.find('span')
return Cspan(span_node)
def set_span(self,this_span):
"""
Sets the span for the terminal
@type this_span: L{Cspan}
@param this_span: span
"""
self.node.append(this_span.get_node())
def __str__(self):
return dump(self.node)
class Cedge:
"""
This class encapsulates an edge object
"""
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('edge')
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 identifier of the object
@rtype: string
@return: identifier of the terminal object
"""
return self.node.get('id')
def set_id(self,this_id):
"""
Sets the identifier for the element
@param this_id: identifier
@type this_id: string
"""
self.node.set('id',this_id)
def __str__(self):
return dump(self.node)
def get_from(self):
"""
Returns the from label
@rtype: string
@return: the from label of the relation
"""
return self.node.get('from')
def set_from(self,this_from):
"""
Sets the identifier for the element
@param this_from: from label
@type this_from: string
"""
self.node.set('from',this_from)
def get_to(self):
"""
Returns the to label
@rtype: string
@return: the to label of the relation
"""
return self.node.get('to')
def set_to(self,this_to):
"""
Sets the identifier for the element
@param this_to: to label
@type this_to: string
"""
self.node.set('to',this_to)
def set_as_head(self):
"""
Sets the edge as a head element
"""
self.node.set('head','yes')
def set_comment(self,c):
"""
Sets the comment for the element
@type c: string
@param c: comment for the element
"""
c = c.replace('--','- -')
self.node.insert(0,etree.Comment(c) )
def get_head(self):
"""
Returns whether the from is head of the constituent (None if not)
@rtype: string
@return: the to label of the relation
"""
return self.node.get('head')
class Ctree:
"""
This class encapsulates a tree object
"""
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('tree')
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 __str__(self):
return dump(self.node)
## Fore getting non terminals
def __get_nt_nodes(self):
for nt_node in self.node.findall('nt'):
yield nt_node
def get_non_terminals(self):
"""
Iterator that returns all the non terminal objects
@rtype: L{Cnonterminal}
@return: non terminal objects (iterator)
"""
for nt_node in self.__get_nt_nodes():
yield Cnonterminal(nt_node)
##################################
## Fore getting terminals
def __get_t_nodes(self):
for t_node in self.node.findall('t'):
yield t_node
def get_terminals(self):
"""
Iterator that returns all the terminal objects
@rtype: L{Cterminal}
@return: terminal objects (iterator)
"""
for t_node in self.__get_t_nodes():
yield Cterminal(t_node)
##################################
def get_terminals_as_list(self):
"""
Iterator that returns all the terminal objects
@rtype: L{Cterminal}
@return: terminal objects as list
"""
terminalList = []
for t_node in self.__get_t_nodes():
terminalList.append(Cterminal(t_node))
return terminalList
## Fore getting edges
def __get_edge_nodes(self):
for t_node in self.node.findall('edge'):
yield t_node
def get_edges(self):
"""
Iterator that returns all the edge objects
@rtype: L{Cedge}
@return: terminal objects (iterator)
"""
for edge_node in self.__get_edge_nodes():
yield Cedge(edge_node)
##################################
def append_element(self,this_element):
"""
Appends a node to the tree, could be a terminal or non terminal or edge
@param this_element: the element to be appended
@type this_element: object
"""
self.node.append(this_element.get_node())
def get_edges_as_list(self):
"""
Iterator that returns all the edge objects
@rtype: L{Cedge}
@return: terminal objects (iterator)
"""
my_edges = []
for edge_node in self.__get_edge_nodes():
my_edges.append(Cedge(edge_node))
return my_edges
##################################
class Cconstituency:
"""
This class encapsulates the constituency layer
"""
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/NAF'
if node is None:
self.node = etree.Element('constituency')
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 __get_tree_nodes(self):
for tree_node in self.node.findall('tree'):
yield tree_node
def get_trees(self):
"""
Iterator that returns all the tree objects
@rtype: L{Ctree}
@return: tree objects (iterator)
"""
for tree_node in self.__get_tree_nodes():
yield Ctree(tree_node)
def __str__(self):
return dump(self.node)
def add_tree(self,this_tree):
"""
Adds a tree to the constituency layer
@param this_tree: the constituency tree
@type this_tree: L{Ctree}
"""
self.node.append(this_tree.get_node())