"""Figures behind the P01 resolution notes on findings 515, 544, 582, 588, 589, 598 and 618 (2026-09-28).
Read-only. Uses zl.py, the f88r claim check's tokenizer."""
import sys
sys.path.insert(0, '../../../resources/tools/provenance/index.html#s-17b20ecd17b24816')
from zl import parse, words, F247_SIX, LATTICE_LABEL_LIST, is_label, is_para

pages, order = parse()


def zone_hits(pid):
    out = {}
    for zone, test in (('label', is_label), ('para', is_para)):
        ws = [(locus, w) for locus, lt, ltype, text in pages[pid]['lines'] if test(ltype) for w in words(text)]
        six_x = [(l, w) for l, w in ws if w in F247_SIX]
        six_c = [(l, w) for l, w in ws if any(t in w for t in F247_SIX)]
        eight_x = [(l, w) for l, w in ws if w in LATTICE_LABEL_LIST]
        out[zone] = (len(ws), six_x, six_c, eight_x)
    return out


for pid in ('f88r', 'f88v', 'f101v', 'f87r', 'f87v'):
    print(f'== {pid} $I={pages[pid]["vars"].get("I")} $L={pages[pid]["vars"].get("L")}')
    for zone, (n, sx, sc, ex) in zone_hits(pid).items():
        pct = lambda k: f'{k}/{n} = {100 * k / n:.1f}%' if n else f'{k}/0'
        print(f'  {zone}: words {n}; six exact {pct(len(sx))} {sx}; six contains {pct(len(sc))} {sc}; eight exact {pct(len(ex))} {ex}')

# 515: f71r nymph labels holding otal, against every zodiac-page label
lab = [(locus, lt, text) for locus, lt, ltype, text in pages['f71r']['lines'] if is_label(ltype)]
print('\n== f71r labels', len(lab), sorted({lt for _, lt, _ in lab}))
otal_lab = [(l, lt, t) for l, lt, t in lab if any('otal' in w for w in words(t))]
print('  labels holding a word that contains otal:', len(otal_lab), otal_lab)
zl = [(pid, locus, text) for pid in order if pages[pid]['vars'].get('I') == 'Z'
      for locus, lt, ltype, text in pages[pid]['lines'] if is_label(ltype)]
z_otal = [x for x in zl if any('otal' in w for w in words(x[2]))]
z_six = [x for x in zl if any(t in w for w in words(x[2]) for t in F247_SIX)]
z_o = [x for x in zl if words(x[2]) and words(x[2])[0].startswith('o')]
print(f'  zodiac pages ($I=Z): {len(zl)} labels; holding otal {len(z_otal)} = {100 * len(z_otal) / len(zl):.1f}%; '
      f'holding any of the six {len(z_six)} = {100 * len(z_six) / len(zl):.1f}%; opening with o {len(z_o)} = {100 * len(z_o) / len(zl):.1f}%')
per_page = {}
for pid, locus, text in zl:
    a = per_page.setdefault(pid, [0, 0]); a[0] += 1; a[1] += any('otal' in w for w in words(text))
print('  per zodiac page, labels holding otal / labels:', {p: f'{b}/{a}' for p, (a, b) in per_page.items()})
