forked from irit-melodi/educe
/
tests.py
359 lines (287 loc) · 11.6 KB
/
tests.py
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# -*- coding: utf-8 -*-
#
# Author: Eric Kow
# License: BSD3
# pylint: disable=too-many-public-methods, invalid-name
"""
Tests for educe
"""
import unittest
from educe.annotation import (Span, RelSpan,
Annotation,
Unit, Relation, Schema, Document)
import educe.graph as educe
from educe.graph import EnclosureGraph
from educe.util import relative_indices
# ---------------------------------------------------------------------
# spans
# ---------------------------------------------------------------------
class SpanTest(unittest.TestCase):
"tests for educe.annotation.Span"
def __init__(self, *args, **kwargs):
super(SpanTest, self).__init__(*args, **kwargs)
self.addTypeEqualityFunc(Span, self.assertEqualStrFail)
def assertEqualStrFail(self, a, b, msg):
"""
just like assertEqual but display both sides with str on failure
"""
if a != b:
msg = msg or u"{0} != {1}".format(a, b)
raise self.failureException(msg)
def assertOverlap(self, expected, pair1, pair2, **kwargs):
"true if `pair1.overlaps(pair2) == expected` (modulo boxing)"
(x1, y1) = pair1
(x2, y2) = pair2
(rx, ry) = expected
o = Span(x1, y1).overlaps(Span(x2, y2), **kwargs)
self.assertTrue(o)
self.assertEqual(Span(rx, ry), o)
def assertNotOverlap(self, pair1, pair2, **kwargs):
"true if `pair1.overlaps(pair2) == expected` (modulo boxing)"
(x1, y1) = pair1
(x2, y2) = pair2
self.assertFalse(Span(x1, y1).overlaps(Span(x2, y2), **kwargs))
def test_overlap(self):
"Span.overlaps() function"
self.assertNotOverlap((5, 10), (11, 12))
self.assertNotOverlap((11, 12), (5, 10))
# should not overlap at edges
self.assertNotOverlap((5, 10), (10, 15))
self.assertOverlap((10, 10), (5, 10), (10, 15), inclusive=True)
self.assertOverlap((6, 9), (5, 10), (6, 9))
self.assertOverlap((6, 9), (6, 9), (5, 10))
self.assertOverlap((7, 10), (5, 10), (7, 12))
self.assertOverlap((7, 10), (7, 12), (5, 10))
def test_overlap_empty(self):
"Span.overlaps() on empty spans"
self.assertOverlap((5, 5), (5, 5), (4, 6))
self.assertOverlap((5, 5), (5, 5), (4, 5))
self.assertOverlap((5, 5), (5, 5), (5, 6))
self.assertOverlap((5, 5), (5, 5), (4, 6), inclusive=True)
self.assertOverlap((5, 5), (5, 5), (4, 5), inclusive=True)
self.assertOverlap((5, 5), (5, 5), (5, 6), inclusive=True)
class NullAnno(Span, Annotation):
def __init__(self, start, end, type="null"):
super(NullAnno, self).__init__(start, end)
self.span = self
self.type = type
def local_id(self):
return str(self)
def __eq__(self, other):
return self.char_start == other.char_start\
and self.char_end == other.char_end\
and self.type == other.type
def __ne__(self, other):
return not self == other
def __hash__(self):
return hash((self.char_start, self.char_end, self.type))
def __repr__(self):
return "%s [%s]" % (super(NullAnno,self).__str__(),self.type)
def __str__(self):
return repr(self)
class EnclosureTest(unittest.TestCase):
def test_trivial(self):
g = EnclosureGraph([])
self.assertEqual(0, len(g.nodes()))
def test_singleton(self):
s0 = NullAnno(1,5)
g = EnclosureGraph([s0])
self.assertEqual([s0.local_id()], g.nodes())
self.assertEqual([], g.inside(s0))
self.assertEqual([], g.outside(s0))
def test_simple_enclosure(self):
s1_5 = NullAnno(1,5)
s2_3 = NullAnno(2,3)
g = EnclosureGraph([s1_5, s2_3])
self.assertEqual([s2_3], g.inside(s1_5))
self.assertEqual([s1_5], g.outside(s2_3))
def test_indirect_enclosure(self):
s1_5 = NullAnno(1,5,'a')
s2_4 = NullAnno(2,4,'b')
s3_4 = NullAnno(3,4,'c')
g = EnclosureGraph([s1_5, s2_4, s3_4])
self.assertEqual([s2_4], g.inside(s1_5))
self.assertEqual([s1_5], g.outside(s2_4))
self.assertEqual([s2_4], g.outside(s3_4))
def test_same_span(self):
s1_5 = NullAnno(1,5,'out')
s2_4a = NullAnno(2,4,'a')
s2_4b = NullAnno(2,4,'b')
s3_4 = NullAnno(3,4,'in')
g = EnclosureGraph([s1_5, s2_4a, s2_4b, s3_4])
self.assertEqual([s2_4a, s2_4b], g.inside(s1_5))
self.assertEqual([s3_4], g.inside(s2_4a))
self.assertEqual([s3_4], g.inside(s2_4b))
self.assertEqual([s2_4a, s2_4b], g.outside(s3_4))
def test_layers(self):
s3_4 = NullAnno(3, 4)
s2_4 = NullAnno(2, 4)
s3_5 = NullAnno(3, 5)
g = EnclosureGraph([s3_4, s2_4, s3_5])
self.assertEqual([s3_4], g.inside(s3_5))
self.assertEqual([s3_4], g.inside(s2_4))
self.assertEqual([s2_4, s3_5], g.outside(s3_4))
def test_indirect_enclosure_untyped(self):
"""
reduce only pays attention to nodes of different type
"""
s_1_5 = NullAnno(1,5)
s_2_4 = NullAnno(2,4)
s_3_4 = NullAnno(3,4)
g = EnclosureGraph([s_1_5, s_2_4, s_3_4])
self.assertEqual([s_2_4, s_3_4], g.inside(s_1_5))
self.assertEqual([s_1_5], g.outside(s_2_4))
self.assertEqual([s_1_5, s_2_4], g.outside(s_3_4))
# ---------------------------------------------------------------------
# annotations
# ---------------------------------------------------------------------
class TestUnit(Unit):
def __init__(self, id, start, end):
Unit.__init__(self, id, Span(start, end), '', {})
class TestRelation(Relation):
def __init__(self, id, start, end):
Relation.__init__(self, id, RelSpan(start, end), '', {})
class TestSchema(Schema):
def __init__(self, id, units, relations, schemas):
Schema.__init__(self, id, frozenset(units), frozenset(relations), frozenset(schemas), '', {})
class TestDocument(Document):
def __init__(self, units, rels, schemas, txt):
Document.__init__(self, units, rels, schemas, txt)
def test_members():
u1 = TestUnit('u1', 2, 4)
u2 = TestUnit('u2', 3, 9)
u3 = TestUnit('distractor', 1,10)
u4 = TestUnit('u4', 12,13)
u5 = TestUnit('u5', 4,12)
u6 = TestUnit('u6', 7,14)
s1 = TestSchema('s1', ['u4','u5','u6'], [], [])
r1 = TestRelation('r1', 's1','u2')
doc = TestDocument([u1,u2,u3,u4,u5,u6],[r1],[s1], "why hello there!")
assert u1._members() is None
assert sorted(s1._members()) == sorted([u4,u5,u6])
assert sorted(r1._members()) == sorted([u2,s1])
assert u1._terminals() == [u1]
assert sorted(s1._terminals()) == sorted([u4,u5,u6])
assert sorted(r1._terminals()) == sorted([u2,u4,u5,u6])
doc_sp = doc.text_span()
for x in doc.annotations():
sp = x.text_span()
assert sp.char_start <= sp.char_end
assert sp.char_start >= doc_sp.char_start
assert sp.char_end <= doc_sp.char_end
# ---------------------------------------------------------------------
# graph
# ---------------------------------------------------------------------
class FakeGraph(educe.Graph):
"""
Stand-in for educe.graph.Graph
"""
def __init__(self):
educe.Graph.__init__(self)
self.corpus = {}
self.doc_key = None
self.doc = None
def _add_fake_node(self, anno_id, type):
attrs = { 'type' : type
}
self.add_node(anno_id)
for x in attrs.items():
self.add_node_attribute(anno_id, x)
def _add_fake_edge(self, anno_id, type, members):
attrs = { 'type' : type
, 'mirror' : anno_id
}
self.add_node(anno_id)
self.add_edge(anno_id)
for x in attrs.items():
self.add_edge_attribute(anno_id, x)
self.add_node_attribute(anno_id, x)
for l in members: self.link(l, anno_id)
def add_edus(self, *anno_ids):
for anno_id in anno_ids: self.add_edu(str(anno_id))
def add_edu(self, anno_id):
self._add_fake_node(anno_id, 'EDU')
def add_non_edu(self, anno_id):
self._add_fake_node(anno_id, 'not-an-EDU')
def add_rel(self, anno_id, node1, node2):
self._add_fake_edge(anno_id, 'rel', [str(node1), str(node2)])
def add_cdu(self, anno_id, members):
self._add_fake_edge(anno_id, 'CDU', list(map(str,members)))
class BasicGraphTest(unittest.TestCase):
def test_cdu_members_trivial(self):
"trivial CDU membership"
gr = FakeGraph()
gr.add_edus(1,2,3)
gr.add_rel('a',1,2)
gr.add_cdu('X',[1,2])
members = gr.cdu_members('X')
expected = frozenset(['1','2'])
self.assertEqual(expected, members)
# this is probably not a desirable property, but is a consequence
# of CDUs being represented as hyperedges
#
# the API may have to expose a notion of being a neighbor only
# via relations
def test_cdu_neighbors(self):
"does belong in the same CDU make you a neighbour?"
gr = FakeGraph()
gr.add_edus('a1','a2','b')
gr.add_cdu('A',['a1','a2'])
ns1 = frozenset(gr.neighbors('a1'))
expected1 = frozenset(['a2'])
self.assertEqual(expected1, ns1)
ns2 = frozenset(gr.neighbors('a2'))
expected2 = frozenset(['a1'])
self.assertEqual(expected2,ns2)
ns3 = frozenset(gr.neighbors('b'))
expected3 = frozenset([])
self.assertEqual(expected3,ns3)
def test_copy(self):
"""
graph in essentially two components but some links
what happens if we copy just one or the other of the
components wrt links etc?
"""
gr = FakeGraph()
gr.add_edus(*range(1,4))
gr.add_edus(*range(10,14))
gr.add_rel('1.2', 1, 2)
gr.add_rel('1.3', 1, 3)
gr.add_rel('2.11', 2, 11) # bridge!
gr.add_rel('11.12', 11, 12)
gr.add_rel('12.13', 11, 12)
gr.add_cdu('X1', [2,3])
gr.add_cdu('X2', ['X1',1]) # should be copied
gr.add_cdu('Y1', [12,13])
gr.add_cdu('XY', [1,13]) # should not be copied
xset2 = set(map(str,[1,2,3]))
gr2 = gr.copy(nodeset=xset2)
self.assertEqual(xset2, gr2.edus())
self.assertEqual(set(['1.2','1.3']), gr2.relations())
self.assertEqual(set(['X1', 'X2']), gr2.cdus())
self.assertEqual(gr.links('X2'), gr2.links('X2'))
# some nonsense copies
xset3 = xset2 | set(['X1'])
gr3 = gr.copy(nodeset=xset3)
self.assertEqual(xset2, gr3.edus()) #not xset3
# including CDU should also result in members being included
xset4 = set(['X2'])
gr4 = gr.copy(nodeset=xset4)
self.assertEqual(xset2, gr4.edus())
self.assertEqual(set(['X1', 'X2']), gr4.cdus())
def test_relative_indices():
"""Test for relative_indices"""
# first example: common case
example1 = [0, 1, 1, 1, 2, 3, 3]
rel_exa1 = [0, 0, 1, 2, 0, 0, 1]
assert relative_indices(example1) == rel_exa1
inv_exa1 = [0, 2, 1, 0, 0, 1, 0]
assert relative_indices(example1, reverse=True) == inv_exa1
# second example: case with None
# each None has relative index 'valna'
# so if valna=0, each None is considered to be a distinct subgroup
example2 = [None, 0, 1, 1, None, None]
rel_exa2 = [0, 0, 0, 1, 0, 0]
assert relative_indices(example2, valna=0) == rel_exa2
inv_exa2 = [0, 0, 1, 0, 0, 0]
assert relative_indices(example2, reverse=True, valna=0) == inv_exa2