Module constituency_data
[hide private]
[frames] | no frames]

Source Code for Module constituency_data

  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   
10 -class Cnonterminal:
11 """ 12 This class encapsulates a non terminal object 13 """
14 - def __init__(self,node=None):
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
25 - def get_node(self):
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
33 - def get_id(self):
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
41 - def set_id(self,this_id):
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
49 - def get_label(self):
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
57 - def set_label(self,label):
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
66 - def __str__(self):
67 return dump(self.node)
68 69 70
71 -class Cterminal:
72 """ 73 This class encapsulates a terminal object 74 """
75 - def __init__(self,node=None):
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
86 - def get_node(self):
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
94 - def get_id(self):
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
102 - def set_id(self,this_id):
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
110 - def get_span(self):
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
119 - def set_span(self,this_span):
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
127 - def __str__(self):
128 return dump(self.node)
129
130 -class Cedge:
131 """ 132 This class encapsulates an edge object 133 """
134 - def __init__(self,node=None):
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
145 - def get_node(self):
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
153 - def get_id(self):
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
161 - def set_id(self,this_id):
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
169 - def __str__(self):
170 return dump(self.node)
171
172 - def get_from(self):
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
180 - def set_from(self,this_from):
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
188 - def get_to(self):
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
196 - def set_to(self,this_to):
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
204 - def set_as_head(self):
205 """ 206 Sets the edge as a head element 207 """ 208 self.node.set('head','yes')
209
210 - def set_comment(self,c):
211 """ 212 Sets the comment for the element 213 @type c: string 214 @param c: comment for the element 215 """ 216 c = c.replace('--','- -') 217 self.node.insert(0,etree.Comment(c) )
218
219 - def get_head(self):
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
230 -class Ctree:
231 """ 232 This class encapsulates a tree object 233 """
234 - def __init__(self,node=None):
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
245 - def get_node(self):
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
253 - def __str__(self):
254 return dump(self.node)
255 256 ## Fore getting non terminals
257 - def __get_nt_nodes(self):
258 for nt_node in self.node.findall('nt'): 259 yield nt_node
260
261 - def get_non_terminals(self):
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 ## Fore getting terminals
272 - def __get_t_nodes(self):
273 for t_node in self.node.findall('t'): 274 yield t_node
275
276 - def get_terminals(self):
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
287 - def get_terminals_as_list(self):
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 ## Fore getting edges
299 - def __get_edge_nodes(self):
300 for t_node in self.node.findall('edge'): 301 yield t_node
302
303 - def get_edges(self):
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
313 - def append_element(self,this_element):
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
321 - def get_edges_as_list(self):
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
335 -class Cconstituency:
336 """ 337 This class encapsulates the constituency layer 338 """
339 - def __init__(self,node=None):
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
351 - def get_node(self):
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
359 - def to_kaf(self):
360 pass
361
362 - def to_naf(self):
363 pass
364
365 - def __get_tree_nodes(self):
366 for tree_node in self.node.findall('tree'): 367 yield tree_node
368
369 - def get_trees(self):
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
378 - def __str__(self):
379 return dump(self.node)
380
381 - def add_tree(self,this_tree):
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