"""The design of arrow2.py also reads the two quotation sets of the first run with the frozen sign; this does that."""
import pathlib as _pl, sys as _sy; _sy.path.insert(0, str(next(p for p in _pl.Path(globals().get("__file__") or _pl.Path.cwd() / "_").resolve().parents if (p / "paths.py").exists()))); from paths import UM as _UM  # where the corpora are: see paths.py
import re, json, sys
import arrow2 as B, arrow as A
src = open(str(_UM)+"/scripture/x1.py", encoding="utf-8").read()
EXPL = eval(re.search(r"EXPL=(\[.*?\])\n", src, re.S).group(1)); TAC = eval(re.search(r"TAC=(\[.*?\])\n", src, re.S).group(1))
from rev_lxx_sealed import ROWS as RR
crit = json.load(open("arrow2.json"))["calibration"]
def lxx_rows(pairs, tb):
    rows = []
    for spec, b, c, v in pairs:
        Tk = B.ntkeys(tb, spec); T = [x for k in Tk for x in B.UNITS[tb][k]]
        S = A.lxxspan(b, c, v, T)
        if S and T: rows.append((S, T, b, tb))
    return rows
def per_row(rows, reps=20):
    import random, collections; rng = random.Random(5); out = []
    for i, (S, T, sb, tb) in enumerate(rows):
        f = A.feats(S, T, sb, tb); ex = collections.defaultdict(list)
        for _ in range(reps):
            j = rng.randrange(len(rows))
            if j != i:
                for k, v in A.feats(S, rows[j][1], sb, rows[j][3]).items(): ex[k].append(v)
        out.append({k: f[k] - sum(ex[k]) / len(ex[k]) for k in f if ex.get(k)})
    return out
res = {}
for name, rows in (("LXX→Matt", lxx_rows([r[:4] for r in EXPL + TAC], "Matt")), ("LXX→Rev", lxx_rows([(r[0], r[1], r[2], r[3]) for r in RR], "Rev"))):
    pr = per_row(rows)
    res[name] = {f: B.recovery([r[f] for r in pr if f in r], crit[f]["frozen_sign"]) for f in ("NV", "NAMES")}
print(json.dumps(res, indent=1)); json.dump(res, open("arrow2_quotation.json", "w"), indent=1)
