File size: 2,967 Bytes
1367bac
 
 
 
 
 
 
 
 
 
 
 
 
79dc2b0
 
1367bac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Lokalny kontrakt-test artefaktu european_hplt_v3_pl (wzorzec: test_europeana_contract.py).

Ćwiczy inwarianty bramki merge DynaWord, które da się sprawdzić bez repo/CI:
canonical schema (typy+kolejność), licencja open+niepusta, brak pustego tekstu, token_count>0,
stats.json spójny z parquetem. NIE zastępuje `dsbench audit`/CI po stronie repo (licencja finalnie
podnoszona z src/sources.py — wpis MUSI być w PR).
"""
import json, sys
from pathlib import Path
import pyarrow as pa, pyarrow.parquet as pq
import pyarrow.compute as pc

ROOT = Path(__file__).resolve().parent.parent
D = ROOT / "data" / "european_hplt_v3_pl"
PARQUET = D / "european_hplt_v3_pl.parquet"
STATS = D / "european_hplt_v3_pl.stats.json"
ACCEPTED = {"cc0-1.0", "cc-by-3.0", "cc-by-4.0", "cc-by-sa-3.0", "cc-by-sa-4.0",
            "public-domain", "mit", "apache-2.0", "bsd-3-clause"}
CANON = ["id", "text", "source", "added", "created", "token_count", "license", "author"]
CANON_TYPES = {"id": "string", "text": "string", "source": "string", "added": "string",
               "created": "string", "token_count": "int64", "license": "string", "author": "string"}

def main():
    fails = []
    pf = pq.ParquetFile(PARQUET)
    sch = pf.schema_arrow
    # 1. schema exact (order + types)
    if sch.names != CANON:
        fails.append(f"schema names != canonical: {sch.names}")
    for name in CANON:
        if name in sch.names and str(sch.field(name).type) != CANON_TYPES[name]:
            fails.append(f"type {name}={sch.field(name).type} != {CANON_TYPES[name]}")
    tbl = pf.read()
    n = tbl.num_rows
    # 2. licenses open + non-empty
    lic = set(pc.unique(tbl["license"]).to_pylist())
    bad = {l for l in lic if (l or "").lower() not in ACCEPTED}
    if bad:
        fails.append(f"non-accepted/empty licenses: {bad}")
    # 3. no empty text
    empty = pc.sum(pc.equal(pc.utf8_trim_whitespace(tbl["text"]), "")).as_py() or 0
    if empty:
        fails.append(f"empty text rows: {empty}")
    # 4. token_count > 0
    nonpos = pc.sum(pc.less_equal(tbl["token_count"], 0)).as_py() or 0
    if nonpos:
        fails.append(f"token_count<=0 rows: {nonpos}")
    # 5. source uniform
    srcs = set(pc.unique(tbl["source"]).to_pylist())
    if srcs != {"european_hplt_v3_pl"}:
        fails.append(f"source not uniform: {srcs}")
    # 6. stats.json consistent
    st = json.loads(STATS.read_text(encoding="utf-8"))
    if st["kept"] != n:
        fails.append(f"stats.kept={st['kept']} != parquet rows={n}")
    tok_sum = pc.sum(tbl["token_count"]).as_py()
    if st["tokens"] != tok_sum:
        fails.append(f"stats.tokens={st['tokens']} != parquet sum={tok_sum}")

    print(f"rows={n:,} tokens={tok_sum:,} licenses={lic} source={srcs}")
    if fails:
        print("CONTRACT FAIL:"); [print("  -", f) for f in fails]; sys.exit(1)
    print("CONTRACT PASS (schema+license+text+token+stats invariants)")

if __name__ == "__main__":
    main()