"""Executable claim thresholds. Run: python3 paper4_checks.py""" import json import hashlib import random import statistics import datetime as dt import platform import sys from pathlib import Path ROOT=Path(__file__).resolve().parent def claim_1(r): """GPT-2 Hindi/English token ratio exceeds seven on the paired sample.""" ci=r['stats']['GPT-2']['Hindi']['ratio_ci95'] ok=ci[0]>7.0 return ok,f"GPT-2 Hindi/English {r['stats']['GPT-2']['Hindi']['ratio_to_english']:.2f}x; 95% CI {ci[0]:.2f}–{ci[1]:.2f}; lower bound > 7.0" def claim_2(r): """All five tokenizers use more Hindi than English tokens.""" ratios=[r['stats'][m]['Hindi']['ratio_ci95'][0] for m in r['model_order']] ok=all(x>1.0 for x in ratios) return ok,f"{sum(x>1 for x in ratios)}/5 lower 95% bounds > 1.0; minimum {min(ratios):.2f}" def claim_3(r): """GPT-2 uses over seven times English tokens for each selected Indian language.""" vals=[r['stats']['GPT-2'][l]['ratio_ci95'][0] for l in r['language_codes'] if l!='English'] ok=all(x>7.0 for x in vals) return ok,f"{sum(x>7 for x in vals)}/5 lower 95% bounds > 7.0; minimum {min(vals):.2f}" def claim_4(r): """XLM-R uses fewer Hindi tokens per sentence than UMT5 here.""" a=r['stats']['XLM-R']['Hindi']['tokens_per_sentence'];b=r['stats']['UMT5']['Hindi']['tokens_per_sentence'] ok=a0 for x in english),'RAW_COUNTS') token_file=ROOT/'assets/paper4/tokenizers'/f'{m}.json' tokenizer=Tokenizer.from_file(str(token_file)) raw=json.loads(token_file.read_text()) marker=raw['model'].get('unk_token') or ('' if m in ('XLM-R','UMT5') else None) unknown_id=raw['model'].get('unk_id') if unknown_id is None and marker:unknown_id=tokenizer.token_to_id(marker) require(r['tokenizer_config'][m]['unknown_id']==unknown_id and r['tokenizer_config'][m]['unknown_token']==marker,'UNKNOWN_ID_METADATA') for li,l in enumerate(r['language_codes']): values=r['counts'][m][l];s=r['stats'][m][l] require(len(values)==300 and all(type(x) is int and x>0 for x in values),'RAW_COUNTS') selected=[texts[l][i] for i in r['sample_indices_zero_based']] encoded=tokenizer.encode_batch(selected,add_special_tokens=False) require(values==[len(e.ids) for e in encoded],'TOKENIZATION_MISMATCH') require(r['unknown_token_counts'][m][l]==(sum(t==unknown_id for e in encoded for t in e.ids) if unknown_id is not None else 0),'UNKNOWN_COUNT') require(abs(s['tokens_per_sentence']-statistics.mean(values))<0.00006,'MEAN_DRIFT') require(abs(s['ratio_to_english']-sum(values)/sum(english))<0.00006,'RATIO_DRIFT') require(abs(s['tokens_per_character']-sum(values)/sum(map(len,selected)))<0.00006,'CHARACTER_RATE_DRIFT') require(s['context_4096_sentences']==int(4096//statistics.mean(values)),'CONTEXT_DRIFT') require(s['ratio_ci95']==interval(values,english,28004+100*li+mi),'INTERVAL_DRIFT') def run(): result_bytes=(ROOT/'evidence/paper4/results.json').read_bytes() r=json.loads(result_bytes) validate_raw(r) lines=['claim | outcome | observed check','--- | --- | ---'] results=[] for i,fn in enumerate(CHECKS,1): ok,output=fn(r);results.append({'number':i,'passed':ok,'output':output,'threshold':fn.__doc__}) lines.append(f'{i} | {"PASS" if ok else "FAIL"} | {output}') table='\n'.join(lines)+'\n' (ROOT/'evidence/paper4/check-table.md').write_text(table) check_bytes=(json.dumps(results,indent=2)+'\n').encode() (ROOT/'evidence/paper4/checks.json').write_bytes(check_bytes) sources=['paper4_checks.py','paper4_measure.py','paper4-assets.json'] (ROOT/'evidence/paper4/check-run.json').write_text(json.dumps({'timestamp_utc':dt.datetime.now(dt.timezone.utc).isoformat(),'run_id':r['run_id'],'results_sha256':hashlib.sha256(result_bytes).hexdigest(),'checks_sha256':hashlib.sha256(check_bytes).hexdigest(),'source_sha256':{name:hashlib.sha256((ROOT/name).read_bytes()).hexdigest() for name in sources},'python':sys.version.split()[0],'platform':platform.platform(),'tokenizers_version':__import__('tokenizers').__version__,'command':'python3 paper4_checks.py','passed':sum(x['passed'] for x in results),'failed':sum(not x['passed'] for x in results)},indent=2)+'\n') print(table,end='') return all(x['passed'] for x in results) if __name__=='__main__': raise SystemExit(0 if run() else 1)