File size: 6,188 Bytes
dd5f5a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Substantive source-preservation and reference-provenance checks."""
import importlib.util
from pathlib import Path
import unittest
import xml.etree.ElementTree as ET

HERE = Path(__file__).resolve().parent


def module(name):
    spec = importlib.util.spec_from_file_location(name, HERE / (name + ".py"))
    value = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(value)
    return value


b = module("build_study")
r = module("retrieval_benchmark")


class SourcePreservation(unittest.TestCase):
    def test_multilingual_sentence_offsets_and_numeric_mentions_are_exact(self):
        text = "  Our results show a −2.5% change among 1,200 respondents (95% CI: −3.0–−2.0). We cannot establish causation from these observations.  "
        spans = list(b.sentence_spans(text))
        self.assertEqual(len(spans), 2)
        self.assertTrue(all(text[start:end] == sentence for start, end, sentence in spans))
        mentions = b.span_cues(text, b.NUMBER, 71)
        for item in mentions:
            self.assertEqual(text[item["start"]:item["end"]], item["text"])
            self.assertEqual(item["source_text_start"], item["start"] + 71)

    def test_main_article_rights_do_not_come_from_figure(self):
        xml = ET.fromstring('<article><front><article-meta/></front><body><fig><permissions><license href="https://creativecommons.org/licenses/by/4.0/"/></permissions></fig></body></article>')
        with self.assertRaises(ValueError):
            b.article_rights(xml)
        xml = ET.fromstring('<article><front><article-meta><permissions><license href="https://creativecommons.org/licenses/by/4.0/">CC BY</license></permissions></article-meta></front></article>')
        self.assertEqual(b.article_rights(xml)["license_scope"], "main_article_prose_only")

    def test_abstract_union_hierarchy_reference_boundaries_and_table_exclusion(self):
        xml = ET.fromstring('''<article><front><article-meta><abstract><p>First abstract.</p></abstract><abstract><p>Second abstract.</p></abstract></article-meta></front><body><sec id="s1" sec-type="results"><title>Results</title><p>\n<xref ref-type="table" rid="t1">\nTable 1</xref> shows the result.<xref ref-type="fn" rid="n1">\n1\n</xref>\n</p><table-wrap><p>Third-party table.</p></table-wrap></sec><ref-list><ref id="b1"><mixed-citation>Author. Title.</mixed-citation></ref></ref-list></body></article>''')
        mapping, citations = b.source_metadata(xml)
        self.assertEqual(mapping[("abstract", 2)]["text"], "Second abstract.")
        paragraph = mapping[("body", 1)]
        self.assertEqual(paragraph["section_path"][0]["id"], "s1")
        for ref in paragraph["references"]:
            self.assertEqual(paragraph["text"][ref["start"]:ref["end"]], ref["text"])
        self.assertTrue(mapping[("body", 2)]["excluded"])
        self.assertEqual(citations[0]["id"], "b1")

    def test_normalized_claim_cannot_retrieve_its_abstract_copy(self):
        claim = "Our results suggest an effect: it is small."
        paragraph = {"id": "c", "paragraph_index": 1}
        duplicate = {"id": "d", "paragraph_index": 4, "semantic_role": "results", "text": "Our results suggest an effect—it is small!"}
        self.assertEqual(b.context_candidates(claim, paragraph, [duplicate], "results"), [])

    def test_all_cross_document_copies_are_quarantined_including_training_copy(self):
        rows = [{"id": "a", "document_id": "paper1", "text": "This shared methods paragraph is duplicated.", "source_role": "train_candidate"},
                {"id": "b", "document_id": "paper2", "text": "This shared methods paragraph is duplicated!", "source_role": "frozen_test_candidate"},
                {"id": "c", "document_id": "paper1", "text": "A distinct original result belongs here.", "source_role": "train_candidate"}]
        admitted, receipt = b.quarantine_duplicates(rows)
        self.assertEqual([x["id"] for x in admitted], ["c"])
        self.assertEqual(receipt["quarantined_positions_in_this_build"], 2)
        self.assertFalse(receipt["whole_document_training_eligible"])
        # A source-subset reproduction still applies the global quarantine.
        subset, _ = b.quarantine_duplicates(rows[:1], receipt)
        self.assertEqual(subset, [])

    def test_missing_original_paragraph_is_reported_as_neighbor_gap(self):
        base = {"document_id": "d", "text": "Exact source.", "source_text_start": 0, "source_text_end": 13,
                "section_title": "Results", "section_path": [], "source_xml_sha256": "abc", "source_section": "body"}
        focal = {**base, "id": "p1", "paragraph_index": 10, "source_xpath": "(/article/body//p)[1]"}
        neighbor = {**base, "id": "p3", "paragraph_index": 12, "source_xpath": "(/article/body//p)[3]", "source_text_start": 30}
        value = b.admitted_neighbor(neighbor, focal)
        self.assertFalse(value["originally_adjacent"])
        self.assertEqual(value["xml_paragraph_ordinal_gap"], 1)
        self.assertEqual(value["source_index_gap"], 1)


class HumanEvidenceMetric(unittest.TestCase):
    def test_exact_chunk_coverage_without_gaps(self):
        text = "Policy analysis αβ. " * 235
        chunks = r.chunks(text, 130)
        self.assertEqual("".join(x["text"] for x in chunks), text)
        self.assertEqual(chunks[0]["start"], 0)
        self.assertEqual(chunks[-1]["end"], len(text))
        self.assertTrue(all(a["end"] == b_["start"] for a, b_ in zip(chunks, chunks[1:])))

    def test_retrieval_hit_is_not_complete_rationale_and_labels_not_inferred(self):
        units = [{"start": 0, "end": 100}, {"start": 100, "end": 200}, {"start": 200, "end": 300}]
        sets = [{"annotator_id": "upstream1", "upstream_label_code": "-1", "spans": [{"start": 50, "end": 170}, {"start": 220, "end": 280}]}]
        value = r.metrics([0, 1, 2], units, sets)
        self.assertEqual(value["human_span_hit@1"], 0)
        self.assertEqual(value["human_span_hit@3"], 1)
        self.assertEqual(value["complete_human_rationale_set@5"], 1)
        # Ranking text cannot manufacture a support/contradict label.
        self.assertNotIn("predicted_label", value)


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