"""Sample docs per source and score corpus quality with cheap heuristics. ponytail: heuristic eyeball score, not an LLM judge. Add LLM-judge pass if these metrics look ambiguous and you need semantic quality, not just surface garble. """ import sys, re, random import pyarrow.parquet as pq SOURCES = ["eurlex", "parliamentary", "wikisource", "wikipedia", "dziennik_ustaw", "wolne_lektury"] N = int(sys.argv[1]) if len(sys.argv) > 1 else 200 # docs sampled per source SHOW = 2 # raw samples printed per source PL_DIAC = set("ąćęłńóśźżĄĆĘŁŃÓŚŹŻ") LETTER = re.compile(r"[^\W\d_]", re.UNICODE) def sample_rows(path, n): """Grab n docs spread across scattered row groups (cheap pseudo-random).""" pf = pq.ParquetFile(path) ng = pf.num_row_groups groups = sorted(random.sample(range(ng), min(ng, 8))) out = [] per = max(1, n // len(groups)) for g in groups: tbl = pf.read_row_group(g, columns=["text"]) texts = tbl.column("text").to_pylist() out += random.sample(texts, min(per, len(texts))) if len(out) >= n: break return out[:n] def score(text): t = text or "" n = len(t) if n == 0: return None letters = sum(1 for c in t if LETTER.match(c)) diac = sum(1 for c in t if c in PL_DIAC) repl = t.count("�") # OCR/decode garbage digits = sum(c.isdigit() for c in t) space = sum(c.isspace() for c in t) words = t.split() uniq = len(set(words)) / len(words) if words else 0 return dict( chars=n, letter_ratio=letters / n, diac_per_kchar=1000 * diac / n, repl_per_kchar=1000 * repl / n, digit_ratio=digits / n, space_ratio=space / n, word_uniq=uniq, ) def avg(rows, k): v = [r[k] for r in rows if r] return sum(v) / len(v) if v else 0 print(f"sampling {N} docs/source\n") print(f"{'source':<16} {'chars':>8} {'letter%':>8} {'diac/k':>7} " f"{'repl/k':>7} {'digit%':>7} {'uniq':>6} {'<200ch':>7}") for s in SOURCES: path = f"data/{s}/{s}.parquet" docs = sample_rows(path, N) scored = [score(d) for d in docs] scored = [x for x in scored if x] short = sum(1 for x in scored if x["chars"] < 200) / len(scored) print(f"{s:<16} {avg(scored,'chars'):>8.0f} " f"{100*avg(scored,'letter_ratio'):>7.1f}% " f"{avg(scored,'diac_per_kchar'):>7.1f} " f"{avg(scored,'repl_per_kchar'):>7.2f} " f"{100*avg(scored,'digit_ratio'):>6.1f}% " f"{avg(scored,'word_uniq'):>6.2f} " f"{100*short:>6.1f}%") print("\n=== raw samples ===") for s in SOURCES: docs = sample_rows(f"data/{s}/{s}.parquet", SHOW) print(f"\n--- {s} ---") for d in docs: snippet = re.sub(r"\s+", " ", d)[:300] print(f" [{len(d)} ch] {snippet}")