forked from cltl/KafNafParserPy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcausal_data.py
More file actions
163 lines (141 loc) · 4.16 KB
/
Copy pathcausal_data.py
File metadata and controls
163 lines (141 loc) · 4.16 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
"""
Parser for the clink layer in KAF/NAF
"""
from lxml import etree
class Cclink:
"""
This class encapsulates a clink 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
"""
if node is None:
self.node = etree.Element('clink')
else:
self.node = node
def get_node_comment(self):
"""
Returns the lxml element for the comment
@rtype: lxml Element
@return: the lxml element for the comment
"""
return self.node_comment
def get_node(self):
"""
Returns the node of the element
@rtype: xml Element
@return: the node of the element
"""
return self.node
def get_id(self):
"""
Returns the token identifier
@rtype: string
@return: the token identifier
"""
return self.node.get('id')
def get_from(self):
"""
Returns the from attribute of the clink
@rtype: string
@return: the from attribute
"""
return self.node.get('from')
def get_to(self):
"""
Returns the to attribute of the clink
@rtype: string
@return: the to attribute
"""
return self.node.get('to')
def set_id(self,this_id):
"""
Set the identifier for the token
@type this_id: string
@param this_id: the identifier
"""
return self.node.set('id',this_id)
def set_from(self, f):
"""
Sets the from attribute
@type f: string
@param f: the from attribute
"""
self.node.set('from',f)
def set_to(self,t):
"""
Sets the to attribute
@type t: string
@param t: the to attribute
"""
self.node.set('to',t)
def set_comment(self,c):
"""
Sets the XML comment for the clink
@type c: string
@param c: the string comment
"""
c = c.replace('--','- -')
self.node.insert(0,etree.Comment(c))
def __str__(self):
return dump(self.node)
class CcausalRelations:
"""
This class encapsulates the clink layer 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
"""
if node is None:
self.node = etree.Element('causalRelations')
else:
self.node = node
def get_node(self):
"""
Returns the node of the element
@rtype: xml Element
@return: the node of the element
"""
return self.node
def to_kaf(self):
pass
def to_naf(self):
pass
def __str__(self):
return dump(self.node)
def __get_node_causalRelations(self):
for node_clink in self.node.findall('clink'):
yield node_clink
def get_clinks(self):
"""
Iterator that returns all the causalRelations in the layer
@rtype: L{Cclink}
@return: list of causalRelations (iterator)
"""
for node in self.__get_node_causalRelations():
yield Cclink(node)
def add_clink(self,my_clink):
"""
Adds a clink object to the layer
@type my_clink: L{Cclink}
@param my_clink: the clink object to be added
"""
self.node.append(my_clink.get_node())
def remove_this_clink(self,clink_id):
"""
Removes the clink for the given clink identifier
@type clink_id: string
@param clink_id: the clink identifier to be removed
"""
for clink in self.get_clinks():
if clink.get_id() == clink_id:
self.node.remove(clink.get_node())
break