# My own read-only analysis of the bundle's published results/R1.json (no bundle code run)
import json, math
r=json.load(open('../ar1/results/R1.json'))
counted=set("SAD GRIEF ANGRY ANGER ANXIETY SHAME HATE ENVY PITY REGRET GUILTY HAPPY LOVE PEACE HOPE PROUD SURPRISED BRAVE FORGET REMEMBER UNDERSTAND BELIEVE DOUBT WANT DIFFICULT EASY".split())
cls=r['classes']; pool=r['pool']
q=pool['share_with_any_hit']
print('pool share of concepts with any hit:',q,' mean hits/concept:',pool['mean_hits_per_concept'])
joint=1
for c,v in cls.items():
    p0=(1-q)**v['anchors']; 
    if c!='load': joint*=p0
    print(f"{c}: anchors={v['anchors']} hits={v['hits']} null_mean={v['null_mean']} approx P(0 hits|null)={p0:.3f} p_one_sided={v['p_one_sided']}")
print('approx P(all four non-benchmark classes have 0 hits | null) =',round(joint,3))
# evaluated pairs and near misses
ev=0; tot=0; near=[]; thin=0; colex_any=[]
for a,t in r['pairs'].items():
    for tg,d in t.items():
        tot+=1
        if d.get('families_with_both',0)>=30: ev+=1
        if d.get('families_with_both',0)<60: thin+=1
        if d.get('families_colexifying',0)>=1 and not d.get('hit'):
            colex_any.append((a,tg,d['families_colexifying'],d['percentile'],d['families_with_both']))
print('pairs listed',tot,'with >=30 families',ev,'with <60 families',thin)
colex_any.sort(key=lambda x:(-x[2],-x[3]))
print('non-hit pairs with >=1 colexifying family (top 25):')
for x in colex_any[:25]: print('  ',x)
print('n non-hit pairs with >=2 families:',sum(1 for x in colex_any if x[2]>=2))
print('n with >=2 families and percentile>=0.9:',[x for x in colex_any if x[2]>=2 and x[3]>=0.9])
