import json, pathlib, random, statistics as st, re, collections
from score import kei_rate, text, TS
from materials import TASKS
G = []  # (who, task, i, {cond: rank})
for who in ["claude","codex"]:
    for t in TASKS:
        for i in range(8):
            j = json.loads(pathlib.Path(f"judge/{who}_{t}_{i}.json").read_text()); o = j["order"]
            G.append((who, t, i, {o[int(k)-1]: pos+1 for pos, k in enumerate(j["rank"])}))
PAIRS = [("S3","base"),("S10","base"),("S30","base"),("S10","S3"),("S30","S10"),("S30","S3"),("S10","S10raw"),
         ("S10N","S10"),("S10Q","S10"),("S10A","S10"),("S10R","S10"),("S10P","S10"),("S10ALL","S10N"),("S10Q","S10A"),("S10N","S10Q"),("S10N","S10A")]
rng = random.Random(1)
out = {}
print("A vs B: Aが上位の回数/48(同率なし) 平均順位差(B-A) 95%区間(48判定をブートストラップ)")
for a, b in PAIRS:
    d = [g[3][b] - g[3][a] for g in G]; win = sum(x > 0 for x in d)
    bs = sorted(st.mean(rng.choices(d, k=len(d))) for _ in range(5000))
    cl = sum(g[3][b] > g[3][a] for g in G if g[0]=="claude"); cx = sum(g[3][b] > g[3][a] for g in G if g[0]=="codex")
    out[f"{a}>{b}"] = dict(win=win, n=len(d), claude=cl, codex=cx, diff=st.mean(d), lo=bs[125], hi=bs[4875])
    print(f"{a:7}>{b:7} {win}/48 (Claude {cl}/24, GPT {cx}/24)  {st.mean(d):+.2f} [{bs[125]:+.2f},{bs[4875]:+.2f}]")
json.dump(out, open("pairs.json","w"), ensure_ascii=False, indent=1)
print("\n敬体率 お題別")
from run_experiment import CONDS
for c in CONDS:
    print(f"{c:7}", " ".join(f"{t}={st.mean(kei_rate(text(c,t,i)) for i in range(8)):.2f}" for t in TS))
