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

Source Code for Module features_data

  1  """ 
  2  Parser for the feature layer in KAF/NAF 
  3  """ 
  4   
  5  from lxml import etree 
  6  from lxml.objectify import dump 
  7  from references_data import * 
  8   
  9   
 10   
11 -class Cproperty:
12 """ 13 This class encapsulates the property element in KAF/NAF 14 """
15 - def __init__(self,node=None,type='NAF'):
16 """ 17 Constructor of the object 18 @type node: xml Element or None (to create and empty one) 19 @param node: this is the node of the element. If it is None it will create a new object 20 @type type: string 21 @param type: the type of the object (KAF or NAF) 22 """ 23 self.type = type 24 if node is None: 25 self.node = etree.Element('property') 26 else: 27 self.node = node
28
29 - def get_node(self):
30 """ 31 Returns the node of the element 32 @rtype: xml Element 33 @return: the node of the element 34 """ 35 return self.node
36
37 - def get_id(self):
38 """ 39 Returns the identifier of the element 40 @rtype: string 41 @return: the identifier of the element 42 """ 43 if self.type == 'KAF': 44 return self.node.get('pid') 45 elif self.type == 'NAF': 46 return self.node.get('id')
47
48 - def set_id(self,pid):
49 """ 50 Set the property identifier 51 @type pid: string 52 @param pid: property identifier 53 """ 54 if self.type == 'KAF': 55 return self.node.set('pid',pid) 56 elif self.type == 'NAF': 57 return self.node.set('id',pid)
58
59 - def get_type(self):
60 """ 61 Returns the type of the property 62 @rtype: string 63 @return: the type of the element 64 """ 65 return self.node.get('lemma')
66
67 - def set_type(self,t):
68 """ 69 Set the property type 70 @type t: string 71 @param t: property type 72 """ 73 return self.node.set('lemma',t)
74
75 - def get_references(self):
76 """ 77 Returns the references of the element 78 @rtype: L{Creferences} 79 @return: the references object of the element (iterator) 80 """ 81 for ref_node in self.node.findall('references'): 82 yield Creferences(ref_node)
83
84 - def set_reference(self,ref):
85 """ 86 Set the property references 87 @type ref: L{Creferences} 88 @param ref: property references 89 """ 90 self.node.append(ref.get_node())
91 92 93
94 -class Cproperties:
95 """ 96 This class encapsulates the property layer in KAF/NAF 97 """
98 - def __init__(self,node=None,type='NAF'):
99 """ 100 Constructor of the object 101 @type node: xml Element or None (to create and empty one) 102 @param node: this is the node of the element. If it is None it will create a new object 103 @type type: string 104 @param type: the type of the object (KAF or NAF) 105 """ 106 self.type=type 107 if node is None: 108 self.node = etree.Element('properties') 109 else: 110 self.node = node
111
112 - def get_node(self):
113 """ 114 Returns the node of the element 115 @rtype: xml Element 116 @return: the node of the element 117 """ 118 return self.node
119
120 - def __iter__(self):
121 """ 122 Iterator that returns all the properties 123 @rtype: L{Cproperty} 124 @return: list of properties (iterator) 125 """ 126 for prop_node in self.node.findall('property'): 127 yield Cproperty(prop_node,self.type)
128
129 - def add_property(self,pid, label,term_span):
130 """ 131 Adds a new property to the property layer 132 @type pid: string 133 @param pid: property identifier 134 @type label: string 135 @param label: the label of the property 136 @type term_span: list 137 @param term_span: list of term identifiers 138 """ 139 new_property = Cproperty(type=self.type) 140 self.node.append(new_property.get_node()) 141 ##Set the id 142 if pid is None: 143 ##Generate a new pid 144 existing_pids = [property.get_id() for property in self] 145 n = 0 146 new_pid = '' 147 while True: 148 new_pid = 'p'+str(n) 149 if new_pid not in existing_pids: break 150 n += 1 151 pid = new_pid 152 new_property.set_id(pid) 153 154 new_property.set_type(label) 155 156 new_ref = Creferences() 157 new_ref.add_span(term_span) 158 new_property.set_reference(new_ref)
159 160 161
162 -class Cfeatures:
163 """ 164 This class encapsulates the features layer in KAF/NAF 165 """
166 - def __init__(self,node=None,type='NAF'):
167 """ 168 Constructor of the object 169 @type node: xml Element or None (to create and empty one) 170 @param node: this is the node of the element. If it is None it will create a new object 171 @type type: string 172 @param type: the type of the object (KAF or NAF) 173 """ 174 self.type = type 175 if node is None: 176 self.node = etree.Element('features') 177 else: 178 self.node = node
179
180 - def get_node(self):
181 """ 182 Returns the node of the element 183 @rtype: xml Element 184 @return: the node of the element 185 """ 186 return self.node
187
188 - def to_kaf(self):
189 """ 190 Converts the element to NAF 191 """ 192 if self.type == 'NAF': 193 ##convert all the properties 194 for node in self.node.findall('properties/property'): 195 node.set('pid',node.get('id')) 196 del node.attrib['id']
197
198 - def to_naf(self):
199 """ 200 Converts the element to KAF 201 """ 202 if self.type == 'KAF': 203 ##convert all the properties 204 for node in self.node.findall('properties/property'): 205 node.set('id',node.get('pid')) 206 del node.attrib['pid']
207
208 - def add_property(self,pid, label,term_span):
209 """ 210 Adds a new property to the property layer 211 @type pid: string 212 @param pid: property identifier 213 @type label: string 214 @param label: the label of the property 215 @type term_span: list 216 @param term_span: list of term identifiers 217 """ 218 node_prop = self.node.find('properties') 219 if node_prop is None: 220 properties = Cproperties(type=self.type) 221 self.node.append(properties.get_node()) 222 else: 223 properties = Cproperties(node=node_prop,type=self.type) 224 225 properties.add_property(pid, label,term_span)
226 227
228 - def get_properties(self):
229 """ 230 Iterator that returns all the properties of the layuer 231 @rtype: L{Cproperty} 232 @return: list of property objects (iterator) 233 """ 234 node_prop = self.node.find('properties') 235 if node_prop is not None: 236 obj_properties = Cproperties(node_prop,self.type) 237 for prop in obj_properties: 238 yield prop
239 240
241 - def remove_properties(self):
242 """ 243 Removes the property layer, if exists 244 """ 245 node_prop = self.node.find('properties') 246 if node_prop is not None: 247 self.node.remove(node_prop)
248