forked from cltl/KafNafParserPy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspan_data.py
More file actions
167 lines (143 loc) · 4.51 KB
/
Copy pathspan_data.py
File metadata and controls
167 lines (143 loc) · 4.51 KB
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
"""
Parser for the span element
"""
# Modified for KAF/NAF
from lxml import etree
from lxml.objectify import dump
class Ctarget:
"""
This class encapsulates the target element within a span 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
"""
self.type = 'NAF/KAF'
if node is None:
self.node = etree.Element('target')
else:
self.node = node
def get_id(self):
"""
Returns the id of the element
@rtype: string
@return: the id of the element
"""
return self.node.get('id')
def set_id(self,this_id):
"""
Set the id of the element
@type this_id: string
@param this_id: the id for the element
"""
self.node.set('id',this_id)
def set_head(self,head):
"""
Sets value of head
"""
self.node.set('head',head)
def is_head(self):
"""
Returns whether this target is set as head or not
@rtype: boolean
@return: whether this target is set as head or not
"""
head = self.node.get('head')
return (head is not None)
def get_node(self):
"""
Returns the node of the element
@rtype: xml Element
@return: the node of the element
"""
return self.node
class Cspan:
"""
This class encapsulates a span object in KAF/NAF
"""
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/KAF'
if node is None:
self.node = etree.Element('span')
else:
self.node = node
def get_id_head(self):
'''
Returns the id of the target that is set as "head"
@rtype: string
@return: the target id (or None) of the head target
'''
id_head = None
for target_node in self:
if target_node.is_head():
id_head = target_node.get_id()
break
return id_head
def add_target_id(self,this_id):
"""
Adds a new target to the span with the specified id
@type this_id: string
@param this_id: the id of the new target
"""
new_target = Ctarget()
new_target.set_id(this_id)
self.node.append(new_target.get_node())
def create_from_ids(self,list_ids):
"""
Adds new targets to the span with the specified ids
@type list_ids: list
@param list_ids: list of identifiers
"""
for this_id in list_ids:
new_target = Ctarget()
new_target.set_id(this_id)
self.node.append(new_target.get_node())
def create_from_targets(self,list_targs):
"""
Adds new targets to the span that are defined in a list
@type list_targs: list
@param list_targs: list of Ctargets
"""
for this_target in list_targs:
self.node.append(this_target.get_node())
def add_target(self,target):
"""
Adds a target object to the span
@type target: L{Ctarget}
@param target: target object
"""
self.node.append(target.get_node())
def __get_target_nodes(self):
for target_node in self.node.findall('target'):
yield target_node
def __iter__(self):
"""
Iterator taht returns the target objects
@rtype: L{Ctarget}
@return: list of target objects (iterator)
"""
for target_node in self.__get_target_nodes():
yield Ctarget(target_node)
def get_span_ids(self):
"""
Returns the list of target ids for the span
@rtype: list
@return: list of target ids
"""
return [t_obj.get_id() for t_obj in self]
def __str__(self):
return dump(self.node)
def get_node(self):
"""
Returns the node of the element
@rtype: xml Element
@return: the node of the element
"""
return self.node