"""Dictionary-position test: a one-part code's numbers track alphabetical rank.
For an uncertain group, compare its fractional position between its known
neighbours with each candidate word's fractional position in a word list."""
import bisect, json, sys
D=[]
for line in open('work/codebook.txt'):
    g,w=line.split(None,1); D.append((int(g),w.strip().lower()))
words=sorted(set(l.split()[0].lower() for l in open('../skydrop-scroll-puzzle/data/words60k.txt') if l.strip() and l.split()[0].isalpha()))
rank=lambda w: bisect.bisect_left(words,w)
TESTS={38078:['recent','received','receive','reconnaissance','reliable'],
       47425:['telephone','telegraph','teletype'],
       19275:['gasoline','gas','gasolene'],
       13864:['do','doubtful','double'],
       1453:['all','allied','allow'],
       21921:['his','him','hostile'],
       34719:['plan','planning','plane'],
       1284:['air','aircraft','airplane'],
       50920:['us','urgent','usa'],
       25278:['just','jointly','july']}
out={}
for g,cands in TESTS.items():
    i=[k for k,(c,_) in enumerate(D) if c==g][0]
    (g0,w0),(g2,w2)=D[i-1],D[i+1]
    f=(g-g0)/(g2-g0); r0,r2=rank(w0),rank(w2)
    rows=[]
    for c in cands:
        fc=(rank(c)-r0)/(r2-r0); rows.append((c,round(fc,3),round(abs(fc-f),3)))
    rows.sort(key=lambda t:t[2])
    out[g]={'between':[w0,w2],'frac':round(f,3),'cands':rows}
    print(f'{g:05d} between {w0}..{w2} frac {f:.3f}: '+', '.join(f'{c} {fc} (|d|={d})' for c,fc,d in rows))
json.dump(out,open('work/slot_test.json','w'),indent=1)
