File size: 2,394 Bytes
1f5647a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Meaningful source-offset, reference-count and held-out-access controls."""
import importlib.util
from pathlib import Path
import tempfile
import unittest

import numpy as np

spec = importlib.util.spec_from_file_location("span_probe", Path(__file__).with_name("probe.py"))
p = importlib.util.module_from_spec(spec)
spec.loader.exec_module(p)


class SpanReferenceChecks(unittest.TestCase):
    def test_unicode_codepoint_offsets_and_two_independent_types(self):
        text = "A😀 firm’s policy."
        words = [[m.start(), m.end()] for m in p.WORD.finditer(text)]
        row = {"text": text, "words": words, "gold": [{"start": 1, "end": 2, "head": "lex"},
                {"start": 1, "end": 9, "head": "inf"}]}
        labels = p.labels_for(row)
        emoji = next(i for i, (a, b) in enumerate(words) if text[a:b] == "😀")
        self.assertEqual(labels[emoji].tolist(), [1, 1])
        self.assertEqual(text[words[emoji][0]:words[emoji][1]], "😀")

    def test_prediction_does_not_bridge_a_negative_source_word(self):
        text = "one not two"
        row = {"text": text, "words": [[m.start(), m.end()] for m in p.WORD.finditer(text)]}
        self.assertEqual(p.predicted_spans(row, [True, False, True]), [[0, 3], [8, 11]])
        self.assertEqual(p.predicted_spans(row, [True, True, True]), [[0, 11]])

    def test_identical_reference_records_do_not_inflate_span_recall(self):
        value = p.span_counts([[0, 4], [0, 4]], [[0, 4]])
        self.assertEqual(value, {"tp": 1, "fp": 0, "fn": 0, "tn": 0})

    def test_relaxed_matching_is_one_to_one_and_distinct_from_exact(self):
        gold, predicted = [[0, 10], [15, 25]], [[0, 11], [14, 25]]
        self.assertEqual(p.span_counts(gold, predicted)["tp"], 0)
        self.assertEqual(p.span_counts(gold, predicted, True)["tp"], 2)
        self.assertEqual(p.span_counts([[0, 10], [0, 9]], [[0, 10]], True)["tp"], 1)

    def test_test_access_is_rejected_before_missing_source_can_be_opened(self):
        old_here, old_source = p.HERE, p.SOURCE
        with tempfile.TemporaryDirectory() as directory:
            p.HERE = Path(directory)
            p.SOURCE = Path(directory) / "does-not-exist"
            with self.assertRaisesRegex(RuntimeError, "Test remains locked"):
                p.read_split("test")
        p.HERE, p.SOURCE = old_here, old_source


if __name__ == "__main__":
    unittest.main()