"""合成离线样本检查。与同名 JSON 及文中列出的附件放在同一目录运行。"""
from pathlib import Path, PurePosixPath
import base64, hashlib, io, inspect, json, math, re, subprocess, sys, tempfile, wave, zipfile
from collections import Counter, defaultdict
from datetime import date, datetime
from decimal import Decimal
sys.dont_write_bytecode=True

def sha(b): return hashlib.sha256(b).hexdigest()
def rejected(fn):
    try: fn()
    except (ValueError,TypeError,KeyError,UnicodeError,OverflowError): return True
    return False

def git(root,*args,input=None,check=True):
    r=subprocess.run(['git','-c','core.autocrlf=false',*args],cwd=root,input=input,capture_output=True,check=check)
    return r

def verify(d,base):
    y=d['labels'];p=d['predictions'];assert len(y)==len(p)==100
    scores={}
    for label in ('ordinary','urgent'):
        tp=sum(a==label and b==label for a,b in zip(y,p));fp=sum(a!=label and b==label for a,b in zip(y,p));fn=sum(a==label and b!=label for a,b in zip(y,p))
        precision=tp/(tp+fp) if tp+fp else d['zero_division'];recall=tp/(tp+fn) if tp+fn else d['zero_division']
        f1=2*precision*recall/(precision+recall) if precision+recall else 0
        scores[label]={'tp':tp,'fp':fp,'fn':fn,'support':y.count(label),'precision':precision,'recall':recall,'f1':f1}
    out={'accuracy':sum(a==b for a,b in zip(y,p))/len(y),'macro_f1':sum(x['f1'] for x in scores.values())/2,'weighted_f1':sum(x['f1']*x['support'] for x in scores.values())/len(y),'urgent_recall':scores['urgent']['recall']}
    assert all(math.isclose(out[k],v) for k,v in d['expected'].items());assert scores['urgent']['tp']==0
    return {'per_class':scores,'summary':out,'synthetic_only':True}

if __name__=='__main__':
    base=Path(__file__).resolve().parent
    data=json.loads((base/'a035.json').read_text('utf-8'))
    result=verify(data,base)
    print(json.dumps({'id':'A035','passed':True,'result':result},ensure_ascii=True))
