-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest_nereval.py
183 lines (155 loc) · 6.07 KB
/
test_nereval.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
import os
import pytest
from nereval import (
correct_text, correct_type, count_correct, has_overlap, Entity, precision, recall, evaluate,
_parse_json, evaluate_json, sign_test
)
def test_has_overlap():
a = Entity('CILINDRISCHE PLUG', 'Productname', 0)
b = Entity('PLUG', 'Productname', 13)
assert has_overlap(a, b) is True
assert has_overlap(b, a) is True
b = Entity('PLUG', 'Productname', 18)
assert has_overlap(a, b) is False
def test_has_overlap_open_interval():
a = Entity('PLUG', 'Productname', 0)
b = Entity('AB', 'Productname', 4)
assert has_overlap(a, b) is False
assert has_overlap(b, a) is False
def test_entity():
e = Entity('CILINDRISCHE PLUG', 'Productname', 0)
assert e.text == 'CILINDRISCHE PLUG'
assert e.type == 'Productname'
assert e.start == 0
def test_correct_text_symmetry():
true = Entity('CILINDRISCHE PLUG', 'Productname', 0)
pred = Entity('CILINDRISCHE', 'Productname', 0)
assert correct_text(true, pred) is False
assert correct_text(pred, true) is False
assert correct_text(true, true) is True
assert correct_text(pred, pred) is True
def test_correct_text_without_overlap():
true = Entity('CILINDRISCHE PLUG', 'Productname', 0)
pred = Entity('CILINDRISCHE PLUG', 'Productname', 11)
assert correct_text(true, pred) is False
def test_correct_text_type_mismatch():
true = Entity('a', 'Productname', 0)
pred = Entity('a', 'Material', 0)
assert correct_text(true, pred) is True
def test_correct_type_symmetry():
true = Entity('CILINDRISCHE PLUG', 'Productname', 0)
pred = Entity('PLUG', 'Productname', 13)
assert correct_type(true, pred) is True
assert correct_type(pred, true) is True
assert correct_type(true, true) is True
assert correct_type(pred, pred) is True
def test_correct_type_with_overlap():
true = Entity('CILINDRISCHE', 'Productname', 0)
pred = Entity('CILINDRISCHE PLUG', 'Productname', 0)
assert correct_type(true, pred) is True
def test_correct_type_without_overlap():
true = Entity('PLUG', 'Productname', 0)
pred = Entity('CILINDRISCHE PLUG', 'Productname', 21)
assert correct_type(true, pred) is False
def test_correct_type_with_mismatch():
true = Entity('PLUG', 'Productname', 0)
pred = Entity('PLUG', 'Material', 0)
assert correct_type(true, pred) is False
def test_count_correct():
# CILINDRISCHE PLUG DIN908 M10X1 foo
# B_PROD I_PROD B_PROD B_DIM O
x = [
Entity('CILINDRISCHE PLUG', 'Productname', 0),
Entity('DIN908', 'Productname', 18),
Entity('M10X1', 'Dimension', 25)
]
# CILINDRISCHE PLUG DIN908 M10X1 foo
# B_PROD B_PROD B_PROD B_PROD B_PROD
y = [
# correct type, wrong text
Entity('CILINDRISCHE', 'Productname', 0),
# correct type, wrong text
Entity('PLUG', 'Productname', 13),
# correct type, correct text
Entity('DIN908', 'Productname', 18),
# wrong type, correct text
Entity('M10X1', 'Productname', 25),
# wrong type, wrong text (no entity)
Entity('foo', 'Productname', 35)
]
count_correct_text, count_correct_type = count_correct(x, y)
assert count_correct_text == 2
assert count_correct_type == 2
# is not necessarily symmetric!
count_correct_text, count_correct_type = count_correct(y, x)
assert count_correct_text == 2
assert count_correct_type == 3
count_correct_text, count_correct_type = count_correct([], [])
assert count_correct_text == 0
assert count_correct_type == 0
def test_precision():
assert precision(0, 10) == 0
assert precision(0, 0) == 0
assert precision(10, 10) == 1
assert precision(5, 10) == 0.5
def test_recall():
assert recall(0, 0) == 0
assert recall(0, 10) == 0
assert recall(10, 10) == 1
assert precision(5, 10) == 0.5
def test_evaluate():
# CILINDRISCHE PLUG DIN908 M10X1 foo
# B_PROD I_PROD B_PROD B_DIM O
x = [
Entity('CILINDRISCHE PLUG', 'Productname', 0),
Entity('DIN908', 'Productname', 18),
Entity('M10X1', 'Dimension', 25)
]
# CILINDRISCHE PLUG DIN908 M10X1 foo
# B_PROD B_PROD B_PROD B_PROD B_PROD
y = [
# correct type, wrong text
Entity('CILINDRISCHE', 'Productname', 0),
# correct type, wrong text
Entity('PLUG', 'Productname', 13),
# correct type, correct text
Entity('DIN908', 'Productname', 18),
# wrong type, correct text
Entity('M10X1', 'Productname', 25),
# wrong type, wrong text (no entity)
Entity('foo', 'Productname', 35)
]
# dataset containing a single description
assert evaluate([x], [y]) == 0.5
assert evaluate([y], [x]) == 0.625
# multiple descriptions
assert evaluate([x, y], [x, y]) == 1
assert evaluate([x, y], [y, x]) == 0.5625
# edge cases
assert evaluate([x], [[]]) == 0
assert evaluate([[]], [x]) == 0
def test_evaluate_different_shapes():
x = [[], []]
y = [[], [], []]
with pytest.raises(ValueError):
evaluate(x, y)
def test_sign_test():
x = [Entity('CILINDRISCHE PLUG', 'Productname', 0)]
y = [Entity('CILINDRISCHE', 'Productname', 0)]
assert sign_test([x], [x], [y]) == (0, 1)
assert sign_test([x], [y], [x]) == (1, 0)
assert sign_test([x], [x], [x]) == (0, 0)
assert sign_test([x, y], [[], []], [x, y]) == (2, 0)
assert sign_test([x, y], [x, y], [[], []]) == (0, 2)
def test_parse_json():
file_name = os.path.join(os.path.dirname(__file__), 'input.json')
predictions = _parse_json(file_name)
assert len(predictions) == 1
instance = predictions[0]
assert instance['text'] == 'CILINDRISCHE PLUG'
assert instance['true'][0] == Entity('CILINDRISCHE PLUG', 'Productname', 0)
assert instance['predicted'][0] == Entity('CILINDRISCHE', 'Productname', 0)
assert instance['predicted'][1] == Entity('PLUG', 'Productname', 13)
def test_evaluate_json():
file_name = os.path.join(os.path.dirname(__file__), 'input.json')
assert isinstance(evaluate_json(file_name), float)