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

Source Code for Module time_data

  1  """ 
  2  Parser for time expressions (following timex) in KAF/NAF 
  3  """ 
  4   
  5  from span_data import * 
  6  from lxml import etree 
  7   
  8   
9 -class Ctime:
10 """ 11 This class encapsulates a <timex> object in KAF/NAF 12 """ 13
14 - def __init__(self,node=None):
15 """ 16 Constructor of the object 17 @param node: this is the node of the element. If None it will create a new object. 18 """ 19 if node is None: 20 self.node = etree.Element('timex3') 21 else: 22 self.node = node
23 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
34 - def get_id(self):
35 """ 36 Returns the timex identifier 37 @rtype: string 38 @return: the timex identifier 39 """ 40 return self.node.get('id')
41 42
43 - def set_id(self, i):
44 """ 45 Sets the identifier for the timex 46 @type i: string 47 @param i: timex identifier 48 """ 49 self.node.set('id',i)
50 51 52
53 - def get_type(self):
54 """ 55 Returns the timex type 56 @rtype: string 57 @return: the timex type 58 """ 59 return self.node.get('type')
60 61
62 - def set_type(self, t):
63 """ 64 Sets the type for the timex 65 @type t: string 66 @param t: timex type 67 """ 68 self.node.set('type',t)
69 70
71 - def set_timex_type(self, t):
72 """ 73 Sets the type for the timex 74 @type t: string 75 @param t: timex type 76 """ 77 self.node.set('type',t)
78 79
80 - def get_value(self):
81 """ 82 Returns the timex valuee 83 @rtype: string 84 @return: the timex value 85 """ 86 return self.node.get('value')
87 88
89 - def set_value(self, v):
90 """ 91 Sets the value for the timex 92 @type v: string 93 @param v: timex value 94 """ 95 self.node.set('value',v)
96
97 - def get_span(self):
98 """ 99 Returns the span object of the timex 100 @rtype: L{Cspan} 101 @return: the timex span 102 """ 103 node_span = self.node.find('span') 104 if node_span is not None: 105 return Cspan(node_span) 106 else: 107 return None
108
109 - def set_span(self,this_span):
110 """ 111 Sets the span for the timex 112 @type this_span: L{Cspan} 113 @param this_span: timex identifier 114 """ 115 self.node.append(this_span.get_node())
116 117
118 - def get_functionInDocument(self):
119 """ 120 Returns the timex functionnInDocument 121 @rtype: string 122 @return: the timex functionnInDocument 123 """ 124 return self.node.get('functionnInDocument')
125 126
127 - def set_functionInDocument(self, f):
128 """ 129 Sets the functionInDocument for the timex 130 @type f: string 131 @param f: timex functionInDocument 132 """ 133 self.node.set('functionInDocument',f)
134 135
136 -class CtimeExpressions:
137 """ 138 This class encapsulates the timeExpressions layer (collection of timex3 objects) 139 """ 140
141 - def __init__(self,node=None):
142 """ 143 Constructor of the object 144 @type node: xml Element or None (to create an empty one) 145 @param node: this is the node of the element. If it is None, a new element will be created. 146 """ 147 self.idx = {} 148 if node is None: 149 self.node = etree.Element('timeExpressions') 150 else: 151 self.node = node 152 for node_timex in self.__get_node_timex3s(): 153 timex_obj = Ctime(node_timex) 154 self.idx[timex_obj.get_id()] = node_timex
155 156 157
158 - def get_node(self):
159 """ 160 Returns the node of the element 161 @rtype: xml Element 162 @return: the node of the element 163 """ 164 return self.node
165 166
167 - def __get_node_timex3s(self):
168 for node_timex in self.node.findall('timex3'): 169 yield node_timex
170 171
172 - def __iter__(self):
173 """ 174 Iterator that returns single timex objects in the layer 175 @rtype: L{Ctime} 176 @return: timex objects 177 """ 178 for node_timex in self.__get_node_timex3s(): 179 yield Ctime(node_timex)
180 181
182 - def get_timex(self, timex_id):
183 """ 184 Returns the timex object for the supplied identifier 185 @type timex_id: string 186 @param timex_id: timex identifier 187 """ 188 if timex_id in self.idx: 189 return Ctime(self.idx[timex_id]) 190 else: 191 return None
192
193 - def get_timeExpressions(self):
194 """ 195 Iterator to get the timex objects 196 @rtype: L{Ctime} 197 @return: iterator for getting the timex object 198 """ 199 for node_pre in self.node.findall('timex3'): 200 yield Ctime(node_pre)
201 202
203 - def add_timex(self, timex_obj):
204 """ 205 Adds a timex object to the layer. 206 @type timex_obj: L{Ctime} 207 @param timex_obj: the timex object 208 """ 209 timex_id = timex_obj.get_id() 210 #check if id is not already present 211 if not timex_id in self.idx: 212 213 timex_node = timex_obj.get_node() 214 self.node.append(timex_node) 215 self.idx[timex_id] = timex_node 216 else: 217 #FIXME: what we want is that the element receives a new identifier that 218 #is not present in current element yet 219 print 'Error: trying to add new element with existing identifier'
220 221
222 - def remove_timex3s(self, list_timex_ids):
223 """ 224 Removes a list of terms from the layer 225 @type list_timex_ids: list (of strings) 226 @param list_timex_ids: list of timex identifier to be removed 227 """ 228 nodes_to_remove = set() 229 for timex in self: 230 if timex.get_id() in list_timex_ids: 231 nodes_to_remove.add(timex.get_node()) 232 #for removing the previous comment (expected the layer will look like termlayer) 233 prv = timex.get_node().getprevious() 234 if prv is not None: 235 nodes_to_remove.add(prv) 236 237 for node in nodes_to_remove: 238 self.node.remove(node)
239