import json, pathlib, re, collections, statistics as st
from materials import *
ORDER = ["base","noguard","P","N","S","A","F","ALL","F2","ALL2"]; TS = list(TASKS); N=8
EMOJI = re.compile("[\U0001F300-\U0001FAFF☀-➿]")
def text(c,t,i): return pathlib.Path(f"runs/{c}_{t}_{i}.txt").read_text().strip()
# 見本・直し例から、丸写しを見つける（10文字以上の一致）
src = [re.sub(r"^V\d+：","",l) for l in SAMPLES.splitlines()[1:]]
fix = re.findall(r"直し：「(.+?)」", FIXES)
def windows(s,n=10): return {s[k:k+n] for k in range(len(s)-n+1)}
SW = set().union(*[windows(x) for x in src]); FW = set().union(*[windows(x) for x in fix])
def copied(x,W): return any(w in x for w in W)
def post_only(x): return x
print("条件 件 ！ 絵文字 メタ(boy) 見本丸写し 直し例丸写し メモ固有")
for c in ORDER:
    m = collections.Counter()
    for t in TS:
        for i in range(N):
            x = text(c,t,i); m["n"]+=1
            m["excl"] += ("！" in x); m["emoji"] += bool(EMOJI.search(x))
            m["meta"] += (t=="boy" and bool(re.search("資料|【|\\[|作ら|空け", x)))
            m["sw"] += copied(x,SW); m["fw"] += copied(x,FW)
            m["notes"] += bool(re.search("体感|半分|去年の冬|2か月|15分|中学生|これだけでいいんですか", x))
    print(f"{c:7}{m['n']:3}{m['excl']:3}{m['emoji']:4}{m['meta']:4}{m['sw']:5}{m['fw']:5}{m['notes']:5}")
for who in ["claude","codex"]:
    ranks = collections.defaultdict(list); byt = collections.defaultdict(lambda: collections.defaultdict(list)); ver = collections.defaultdict(collections.Counter)
    for t in TS:
        for i in range(N):
            j = json.loads(pathlib.Path(f"judge/{who}_{t}_{i}.json").read_text()); o = j["order"]
            assert sorted(j["rank"])==list(range(1,len(o)+1)), (who,t,i)
            for pos, k in enumerate(j["rank"]):
                c = o[int(k)-1]; ranks[c].append(pos+1); byt[t][c].append(pos+1)
            for k,v in j["verdicts"].items(): ver[o[int(k)-1]][v]+=1
    print("\n##", who, "平均順位(1位=最も本人らしい)  そのまま/小修正/論点違い/不可")
    for c in sorted(ORDER, key=lambda c: st.mean(ranks[c])):
        v=ver[c]; print(f"{c:7} {st.mean(ranks[c]):.2f}  " + "/".join(str(v[x]) for x in ["そのまま出せる","小修正で出せる","論点から違う","不可"]) + "  " + " ".join(f"{t}={st.mean(byt[t][c]):.1f}" for t in TS))
