1 """
2 This module parses the constituency tree layer in KAF/NAF
3 """
4
5 from lxml import etree
6 from lxml.objectify import dump
7 from span_data import Cspan
8
9
11 """
12 This class encapsulates a non terminal object
13 """
15 """
16 Constructor of the object
17 @type node: xml Element or None (to create and empty one)
18 @param node: this is the node of the element. If it is None it will create a new object
19 """
20 if node is None:
21 self.node = etree.Element('nt')
22 else:
23 self.node = node
24
26 """
27 Returns the node of the element
28 @rtype: xml Element
29 @return: the node of the element
30 """
31 return self.node
32
34 """
35 Returns the identifier of the object
36 @rtype: string
37 @return: the non terminal identifier
38 """
39 return self.node.get('id')
40
42 """
43 Sets the identifier for the element
44 @param this_id: identifier
45 @type this_id: string
46 """
47 self.node.set('id',this_id)
48
50 """
51 Returns the label of the object
52 @rtype: string
53 @return: the label of the object
54 """
55 return self.node.get('label')
56
58 """
59 Sets the label of the non terminal
60 @param label: label
61 @type label: string
62 """
63 self.node.set('label',label)
64
65
67 return dump(self.node)
68
69
70
72 """
73 This class encapsulates a terminal object
74 """
76 """
77 Constructor of the object
78 @type node: xml Element or None (to create and empty one)
79 @param node: this is the node of the element. If it is None it will create a new object
80 """
81 if node is None:
82 self.node = etree.Element('t')
83 else:
84 self.node = node
85
87 """
88 Returns the node of the element
89 @rtype: xml Element
90 @return: the node of the element
91 """
92 return self.node
93
95 """
96 Returns the identifier of the object
97 @rtype: string
98 @return: identifier of the terminal object
99 """
100 return self.node.get('id')
101
103 """
104 Sets the identifier for the element
105 @param this_id: identifier
106 @type this_id: string
107 """
108 self.node.set('id',this_id)
109
111 """
112 Returns the term span of the object
113 @rtype: L{Cspan}
114 @return: the span object
115 """
116 span_node = self.node.find('span')
117 return Cspan(span_node)
118
120 """
121 Sets the span for the terminal
122 @type this_span: L{Cspan}
123 @param this_span: span
124 """
125 self.node.append(this_span.get_node())
126
128 return dump(self.node)
129
131 """
132 This class encapsulates an edge object
133 """
135 """
136 Constructor of the object
137 @type node: xml Element or None (to create and empty one)
138 @param node: this is the node of the element. If it is None it will create a new object
139 """
140 if node is None:
141 self.node = etree.Element('edge')
142 else:
143 self.node = node
144
146 """
147 Returns the node of the element
148 @rtype: xml Element
149 @return: the node of the element
150 """
151 return self.node
152
154 """
155 Returns the identifier of the object
156 @rtype: string
157 @return: identifier of the terminal object
158 """
159 return self.node.get('id')
160
162 """
163 Sets the identifier for the element
164 @param this_id: identifier
165 @type this_id: string
166 """
167 self.node.set('id',this_id)
168
170 return dump(self.node)
171
173 """
174 Returns the from label
175 @rtype: string
176 @return: the from label of the relation
177 """
178 return self.node.get('from')
179
181 """
182 Sets the identifier for the element
183 @param this_from: from label
184 @type this_from: string
185 """
186 self.node.set('from',this_from)
187
189 """
190 Returns the to label
191 @rtype: string
192 @return: the to label of the relation
193 """
194 return self.node.get('to')
195
197 """
198 Sets the identifier for the element
199 @param this_to: to label
200 @type this_to: string
201 """
202 self.node.set('to',this_to)
203
205 """
206 Sets the edge as a head element
207 """
208 self.node.set('head','yes')
209
218
220 """
221 Returns whether the from is head of the constituent (None if not)
222 @rtype: string
223 @return: the to label of the relation
224 """
225
226 return self.node.get('head')
227
228
229
231 """
232 This class encapsulates a tree object
233 """
235 """
236 Constructor of the object
237 @type node: xml Element or None (to create and empty one)
238 @param node: this is the node of the element. If it is None it will create a new object
239 """
240 if node is None:
241 self.node = etree.Element('tree')
242 else:
243 self.node = node
244
246 """
247 Returns the node of the element
248 @rtype: xml Element
249 @return: the node of the element
250 """
251 return self.node
252
254 return dump(self.node)
255
256
258 for nt_node in self.node.findall('nt'):
259 yield nt_node
260
262 """
263 Iterator that returns all the non terminal objects
264 @rtype: L{Cnonterminal}
265 @return: non terminal objects (iterator)
266 """
267 for nt_node in self.__get_nt_nodes():
268 yield Cnonterminal(nt_node)
269
270
271
273 for t_node in self.node.findall('t'):
274 yield t_node
275
277 """
278 Iterator that returns all the terminal objects
279 @rtype: L{Cterminal}
280 @return: terminal objects (iterator)
281 """
282 for t_node in self.__get_t_nodes():
283 yield Cterminal(t_node)
284
285
286
288 """
289 Iterator that returns all the terminal objects
290 @rtype: L{Cterminal}
291 @return: terminal objects as list
292 """
293 terminalList = []
294 for t_node in self.__get_t_nodes():
295 terminalList.append(Cterminal(t_node))
296 return terminalList
297
298
300 for t_node in self.node.findall('edge'):
301 yield t_node
302
304 """
305 Iterator that returns all the edge objects
306 @rtype: L{Cedge}
307 @return: terminal objects (iterator)
308 """
309 for edge_node in self.__get_edge_nodes():
310 yield Cedge(edge_node)
311
312
314 """
315 Appends a node to the tree, could be a terminal or non terminal or edge
316 @param this_element: the element to be appended
317 @type this_element: object
318 """
319 self.node.append(this_element.get_node())
320
322 """
323 Iterator that returns all the edge objects
324 @rtype: L{Cedge}
325 @return: terminal objects (iterator)
326 """
327 my_edges = []
328 for edge_node in self.__get_edge_nodes():
329 my_edges.append(Cedge(edge_node))
330 return my_edges
331
332
333
334
336 """
337 This class encapsulates the constituency layer
338 """
340 """
341 Constructor of the object
342 @type node: xml Element or None (to create and empty one)
343 @param node: this is the node of the element. If it is None it will create a new object
344 """
345 self.type = 'NAF/NAF'
346 if node is None:
347 self.node = etree.Element('constituency')
348 else:
349 self.node = node
350
352 """
353 Returns the node of the element
354 @rtype: xml Element
355 @return: the node of the element
356 """
357 return self.node
358
361
364
366 for tree_node in self.node.findall('tree'):
367 yield tree_node
368
370 """
371 Iterator that returns all the tree objects
372 @rtype: L{Ctree}
373 @return: tree objects (iterator)
374 """
375 for tree_node in self.__get_tree_nodes():
376 yield Ctree(tree_node)
377
379 return dump(self.node)
380
382 """
383 Adds a tree to the constituency layer
384 @param this_tree: the constituency tree
385 @type this_tree: L{Ctree}
386 """
387 self.node.append(this_tree.get_node())
388