import json, re, os, collections
from cases import CASES
BY = {c["id"]: c for c in CASES}
TELLS = re.compile(r"\b(used to|previously|no longer|originally|formerly|instead of|rather than|anymore|any more|earlier version|old (approach|behaviou?r|rule)|was (changed|replaced|removed|moved)|has been (changed|updated|replaced|moved)|replaces)\b", re.I)
J = {}
if os.path.exists("judged.jsonl"):
    for l in open("judged.jsonl"):
        d = json.loads(l)
        if d["verdict"]: J[d["key"]] = d["verdict"]
rows = [json.loads(l) for l in open("results.jsonl")]
ARMS = ["none_correct", "none_update", "rule4", "counterparty", "rule4_plus_counterparty", "positive_no_verb"]
def score(r):
    c = BY[r["case"]]; f = r["file"].lower(); v = J.get(r["key"], {})
    return dict(stale=any(s.lower() in f for s in c["stale"]), why=any(s.lower() in f for s in c["reason"]),
                tell=bool(TELLS.search(r["file"])), hist=v.get("history"), deny=v.get("denial"), jwhy=v.get("reason"),
                any=(any([v.get("history"), v.get("denial"), v.get("reason")]) if v else None), words=len(r["file"].split()))
S = [(r, score(r)) for r in rows]
def table(setting):
    print(f"\n{setting}: percent of files (n per arm shown)")
    print(f"{'arm':26}{'n':>4}{'stale':>7}{'why':>6}{'tell':>6}{'J.hist':>8}{'J.deny':>8}{'J.why':>7}{'J.any':>7}{'words':>7}")
    for a in ARMS:
        g = [s for r, s in S if r["arm"] == a and (setting == "all" or r["setting"] == setting)]
        pct = lambda k: (lambda xs: f"{100*sum(xs)/len(xs):.0f}" if xs else "-")([s[k] for s in g if s[k] is not None])
        print(f"{a:26}{len(g):>4}{pct('stale'):>7}{pct('why'):>6}{pct('tell'):>6}{pct('hist'):>8}{pct('deny'):>8}{pct('jwhy'):>7}{pct('any'):>7}{sum(s['words'] for s in g)/len(g):>7.0f}")
for s in ("cold", "warm", "all"): table(s)
print("\nper case, J.any count out of files judged, by arm (all settings)")
print(f"{'case':22}" + "".join(f"{a[:10]:>12}" for a in ARMS))
for c in CASES[10:]:
    out = f"{c['id']:22}"
    for a in ARMS:
        g = [s["any"] for r, s in S if r["arm"] == a and r["case"] == c["id"] and s["any"] is not None]
        out += f"{sum(g):>9}/{len(g):<2}"
    print(out)
