# Recount the tool's hook lines independently: words (same rule: whitespace tokens with a letter or digit), seconds at a pace.
import json, re, statistics as st, sys
d = json.load(open(sys.argv[1]))
WPM = 155
def words(t): return len([w for w in re.split(r'\s+', t) if re.search(r'[A-Za-z0-9]', w)])
def secs(n, wpm=WPM): return n / wpm * 60
# control: 155 words at 155 wpm must be 60.0 s; 31 words at 155 wpm = 12.0 s
assert abs(secs(155) - 60.0) < 1e-9 and abs(secs(31) - 12.0) < 1e-9, 'control failed'
print('control ok: 155 words = %.1f s, 31 words = %.1f s' % (secs(155), secs(31)))
mism = 0
for r in d:
    w = words(r['text'])
    r['w'] = w
    if str(w) != r['info'] or abs(secs(w) - float(r['sec'])) > 0.005: mism += 1; print('MISMATCH', r)
print('rows', len(d), 'tool-vs-recount mismatches', mism)
kinds = ['Question']*4+['Mistake']*4+['Number']*4+['Result first']*4+['Contrarian']*4+['Story']*4+['Curiosity']*6
for r in d: r['kind'] = kinds[r['i']]
def line(label, rows):
    ws = [r['w'] for r in rows]; ss = [secs(w) for w in ws]
    print('%-14s n=%2d words min/med/max %d/%g/%d  sec min/med/max %.2f/%.2f/%.2f  >3.0s:%d  >4.5s(tool flag):%d  toolflag:%d' % (label, len(rows), min(ws), st.median(ws), max(ws), min(ss), st.median(ss), max(ss), sum(s > 3.0 for s in ss), sum(s > 4.5 for s in ss), sum(r['long'] for r in rows)))
line('ALL 90', d)
for f in ['default', 'meal-prep', 'video-editing']: line(f, [r for r in d if r['fill'] == f])
for k in dict.fromkeys(kinds): line(k, [r for r in d if r['kind'] == k])
# bare template length (placeholders counted as one word each) vs filled
print('longest 3 and shortest 3 lines:')
for r in sorted(d, key=lambda r: r['w'])[:3] + sorted(d, key=lambda r: r['w'])[-3:]: print(' ', r['w'], '%.2f s' % secs(r['w']), r['fill'], r['text'])
# pace sensitivity for the all-90 median words
mw = st.median([r['w'] for r in d])
for wpm in (120, 155, 190): print('median %g words at %d wpm = %.2f s' % (mw, wpm, secs(mw, wpm)))
# words allowed per budget
for t in (2, 3, 4, 5):
    print('budget %d s: %s' % (t, ', '.join('%d wpm -> %d words' % (w, int(t * w / 60)) for w in (120, 155, 190))))
json.dump(d, open('hooks-analysed.json', 'w'))
