import json
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams.update({"font.family":"DejaVu Sans","font.size":11,"axes.spines.top":False,
                     "axes.spines.right":False,"axes.titleweight":"bold","figure.dpi":140})
ca=json.load(open('chapter_alpha.json'))  # m -> [exact_a, chap_a, lo, hi]
chac=json.load(open('bootstrap_ci.json')) # m -> [ca, ca_lo, ca_hi, al, al_lo, al_hi]
TIER={"gpt-5.4-nano":"budget","gpt-5.4":"mid","gemini-2.5-flash-lite":"budget",
      "gemini-2.5-flash":"budget-mid","claude-haiku-4-5":"budget-mid","claude-sonnet-4-6":"mid"}
TIER_COLOR={"budget":"#d98c5f","budget-mid":"#e0b050","mid":"#4a8fb5"}
# order by chapter accuracy (desc)
order=sorted(ca.keys(), key=lambda m: chac[m][0], reverse=True)
colors=[TIER_COLOR[TIER[m]] for m in order]; y=np.arange(len(order))
val=[ca[m][1] for m in order]; lo=[ca[m][2] for m in order]; hi=[ca[m][3] for m in order]
xerr=[[v-l for v,l in zip(val,lo)],[h-v for v,h in zip(val,hi)]]
fig,ax=plt.subplots(figsize=(8,4.2))
ax.barh(y,val,color=colors,xerr=xerr,error_kw=dict(ecolor="#333",capsize=3,lw=1))
ax.set_yticks(y); ax.set_yticklabels(order); ax.invert_yaxis()
ax.set_xlabel("Chapter-level Krippendorff's alpha across 6 languages (95% CI)")
ax.set_title("Cross-lingual consistency at the chapter level (same granularity as accuracy)")
ax.axvline(0.667,ls="--",color="#888",lw=1)
ax.text(0.667,len(order)-0.3,"  0.667\n  (tentative-\n  conclusion\n  threshold)",fontsize=7,color="#666",va="top")
for i,(v,h) in enumerate(zip(val,hi)): ax.text(h+0.012,i,f"{v:.3f}",va="center",fontsize=10)
ax.set_xlim(0,1.0)
plt.tight_layout(); plt.savefig("fig2_alpha.png",bbox_inches="tight"); plt.close()
print("regenerated fig2_alpha.png as CHAPTER-LEVEL alpha with 95% CI and 0.667 reference line")
