File size: 7,317 Bytes
1f231e9
24c3b46
1f231e9
 
 
 
1bcc6c0
 
 
 
 
 
1f231e9
 
85f00da
1f231e9
 
4a0d2b0
ecfe7f5
 
4a0d2b0
1f231e9
4a0d2b0
1f231e9
4a0d2b0
8373f72
4a0d2b0
 
 
 
 
 
8373f72
 
4a0d2b0
ecfe7f5
 
8373f72
 
 
 
 
 
 
4a0d2b0
 
85f00da
 
ecfe7f5
85f00da
 
 
ecfe7f5
 
 
85f00da
 
ecfe7f5
 
 
 
 
 
 
 
4a0d2b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24c3b46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1bcc6c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from collections import Counter, defaultdict
import json
from pathlib import Path

import pyarrow.parquet as pq

from scripts.metrical_lines import (
    PUBLIC_METRICAL_LINE_FIELD_SET,
    load_public_metrical_lines,
    sanitize_metrical_lines,
)


DATA_ROOT = Path(__file__).resolve().parents[1] / "data"
BASE_CONFIGS = ("prose", "verse_sentence", "verse_metre")


def test_atomic_splits_are_balanced_and_100_chunkable() -> None:
    for base_config in BASE_CONFIGS:
        config = f"{base_config}_1"
        counts = defaultdict(Counter)
        for path in sorted((DATA_ROOT / config).glob("*.parquet")):
            table = pq.read_table(path, columns=["author", "split"])
            for row in table.to_pylist():
                counts[row["author"]][row["split"]] += 1

        assert counts
        for author, author_counts in counts.items():
            assert author_counts["train"] > 0, (config, author)
            assert author_counts["validation"] == author_counts["test"], (config, author)
            assert author_counts["validation"] >= 100, (config, author)
            assert author_counts["validation"] % 100 == 0, (config, author)


def test_each_genre_retains_approximately_balanced_source_splits() -> None:
    for base_config in BASE_CONFIGS:
        config = f"{base_config}_1"
        counts = Counter()
        for path in sorted((DATA_ROOT / config).glob("*.parquet")):
            splits = pq.read_table(path, columns=["split"])["split"].to_pylist()
            counts.update(splits)

        total = sum(counts.values())
        assert total > 0
        assert counts["validation"] == counts["test"]
        assert 0.78 <= counts["train"] / total <= 0.83, (config, counts)


def test_chunkable_variants_have_atomic_train_and_exact_evaluation_chunks() -> None:
    for base_config in BASE_CONFIGS:
        for threshold in (10, 100):
            config = f"{base_config}_{threshold}"
            train_path = DATA_ROOT / config / "train-00000-of-00001.parquet"
            train_sizes = pq.read_table(train_path, columns=["chunk_size"])["chunk_size"].to_pylist()
            assert train_sizes and set(train_sizes) == {1}
            for split in ("validation", "test"):
                path = DATA_ROOT / config / f"{split}-00000-of-00001.parquet"
                table = pq.read_table(
                    path, columns=["author", "chunk_size", "constituent_ids"],
                )
                assert table.num_rows > 0
                assert set(table["chunk_size"].to_pylist()) == {threshold}
                assert all(
                    len(ids) == threshold for ids in table["constituent_ids"].to_pylist()
                )


def test_all_task_sizes_cover_exactly_the_same_atomic_rows() -> None:
    for base_config in BASE_CONFIGS:
        atomic_rows = {}
        for split in ("train", "validation", "test"):
            path = DATA_ROOT / f"{base_config}_1" / f"{split}-00000-of-00001.parquet"
            atomic_rows[split] = {
                row["id"]: row["text"]
                for row in pq.read_table(path, columns=["id", "text"]).to_pylist()
            }

        for threshold in (10, 100):
            for split in ("train", "validation", "test"):
                path = (
                    DATA_ROOT / f"{base_config}_{threshold}"
                    / f"{split}-00000-of-00001.parquet"
                )
                chunks = pq.read_table(
                    path, columns=["text", "constituent_ids"],
                ).to_pylist()
                represented_ids = [
                    row_id for chunk in chunks for row_id in chunk["constituent_ids"]
                ]
                assert len(represented_ids) == len(set(represented_ids))
                assert set(represented_ids) == set(atomic_rows[split]), (
                    base_config,
                    threshold,
                    split,
                )
                separator = "\n" if base_config == "verse_metre" else "\n\n"
                for chunk in chunks:
                    expected_text = (
                        atomic_rows[split][chunk["constituent_ids"][0]]
                        if split == "train"
                        else separator.join(
                            atomic_rows[split][row_id].strip()
                            for row_id in chunk["constituent_ids"]
                        )
                    )
                    assert chunk["text"] == expected_text


def test_verse_metre_chunks_concatenate_every_constituent_syllable() -> None:
    atomic_syllables = {}
    for split in ("train", "validation", "test"):
        path = DATA_ROOT / "verse_metre_1" / f"{split}-00000-of-00001.parquet"
        table = pq.read_table(path, columns=["id", "syllables"])
        atomic_syllables[split] = {
            row["id"]: json.loads(row["syllables"])
            for row in table.to_pylist()
        }

    for threshold in (10, 100):
        for split in ("train", "validation", "test"):
            path = (
                DATA_ROOT / f"verse_metre_{threshold}"
                / f"{split}-00000-of-00001.parquet"
            )
            table = pq.read_table(path, columns=["constituent_ids", "syllables"])
            for row in table.to_pylist():
                expected = [
                    syllable
                    for row_id in row["constituent_ids"]
                    for syllable in atomic_syllables[split][row_id]
                ]
                assert json.loads(row["syllables"]) == expected


def test_scansion_is_not_published() -> None:
    for suffix in ("1", "10", "100"):
        for split in ("train", "validation", "test"):
            path = (
                DATA_ROOT / f"verse_metre_{suffix}"
                / f"{split}-00000-of-00001.parquet"
            )
            assert "scansion" not in pq.ParquetFile(path).schema_arrow.names



def test_metrical_lines_publish_only_content_allowlist_everywhere() -> None:
    for suffix in ("1", "10", "100"):
        for split in ("train", "validation", "test"):
            path = (
                DATA_ROOT / f"verse_sentence_{suffix}"
                / f"{split}-00000-of-00001.parquet"
            )
            table = pq.read_table(
                path, columns=["metrical_line_ids", "metrical_lines"],
            )
            for row in table.to_pylist():
                lines = load_public_metrical_lines(row["metrical_lines"])
                assert len(lines) == len(row["metrical_line_ids"])
                assert all(set(line) == PUBLIC_METRICAL_LINE_FIELD_SET for line in lines)


def test_metrical_line_sanitizer_drops_all_provenance_and_identifiers() -> None:
    unsafe = json.dumps([{
        "text": "μῆνιν ἄειδε",
        "metre": "hexameter",
        "syllables": [{"text": "μῆ", "quantity": "long", "features": []}],
        "hypotactic_file": "iliad1.html",
        "hypotactic_author": "Homer",
        "hypotactic_work": "Iliad",
        "book": "1",
        "poem_sequence": "1",
        "number": "1",
        "speaker": "Achilles",
        "id": "vm-secret",
        "coverage_in_sentence": "full",
        "scansion": "–",
    }], ensure_ascii=False)
    lines = json.loads(sanitize_metrical_lines(unsafe))
    assert len(lines) == 1
    assert set(lines[0]) == PUBLIC_METRICAL_LINE_FIELD_SET