hex/ios/TypingConfig/package/scripts/taps.py
Ryan Vogel b0f6a19eef ios: typing config package: a space tap meant for a letter is labelled as that letter
taps.py labelled the space that ended a word as meant for a space. When a tap meant for n lands on
the space bar, "on" goes in as "o" and a space, and the person finishes it as "on"; that tap was
meant for n. It is now labelled as the next letter of the finished word when the word was finished
longer from where the space went in. Before, a zone giving n's bottom edge more room looked as if
it only took taps from the space bar (right-low fixture, day 1: 7 taps lost, 0 gained); with the
right labels it gains 7 and loses none on half A (+5 words).

Verified: on the seven agent exports, only right-low (19 taps, 18 for n) and Ryan's day 1 (5 taps)
change. A fresh agent on each with this package: right-low now names the n-on-space slip and leaves
the zone out on the 15-tap floor (9 taps on half A); its config replays day 2 as before (114 -> 112
word errors, 1 made right, 0 wrong). Ryan's config applies exactly the same parts as before.
2026-11-23 12:47:08 -05:00

102 lines
4.8 KiB
Python

"""Every tap whose meaning is known, mapped onto the keyboard the person uses now.
For each word whose final spelling is known (words.py), its taps are lined up with the letters
(and spaces) it ended as, so each tap has the key it was **meant** for, not just the one it
pressed. The space that finished a word was meant as a space, unless the person then finished
the word longer from where that space went in ("o" then fixed to "on"): that tap was meant for
the next letter, and pressed the space bar instead. Each tap's landing point is
mapped onto state/phone.json's keys: it keeps its place on the keycap it landed on, scaled to
that keycap now (fingers aim at keycaps), so taps from older layouts count too.
Writes <export>/out/taps.json; letters and "space" only, no words.
python3 scripts/taps.py [EXPORT_DIR]
"""
import json, os, sys, collections
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import hexlog, words as W, geometry as G
def label(L):
m = L.get("metrics") or {}
return "%sx%s keys %s" % (L.get("width"), L.get("height"), m.get("keyHeight"))
def build(export):
words, LAY, _ = W.load(export)
target = G.load_layout(export)
tkeys = {k["id"]: k for k in target["keys"] if k["row"] >= 0}
base = G.base_regions(target)
out = []
dropped = collections.Counter()
def mapped(t):
L = LAY.get(t["layout"])
if not L:
dropped["layout not logged"] += 1; return None
s0 = t["samples"][0]; x, y = s0[1], s0[2]
under = None
for k in L["keys"]:
rects = [k["hitFrame"]] + ([k["gapHitFrame"]] if k.get("gapHitFrame") else [])
if any(G.contains(r, x, y) for r in rects):
under = k; break
if under is None or under["id"] not in tkeys:
dropped["off the keys now"] += 1; return None
of, nf = under["frame"], tkeys[under["id"]]["frame"]
nx = nf[0] + nf[2] / 2 + (x - of[0] - of[2] / 2) * nf[2] / of[2]
ny = nf[1] + nf[3] / 2 + (y - of[1] - of[3] / 2) * nf[3] / of[3]
return nx, ny, under["id"], L
def add(t, meant, w):
m = mapped(t)
if m is None: return
nx, ny, under, L = m
out.append({"meant": meant, "x": round(nx, 2), "y": round(ny, 2), "pressed": t["key0"], "under": under,
"now": G.key_at(target, base, nx, ny), "half": w["half"], "session": w["session"],
"source": label(L), "word": w["id"]})
for w in words:
if w["swiped"] or not w.get("final"):
continue
final = W.norm(w["final"])
taps = w.get("error_kept_taps") or w["taps"]
raw = w.get("raw_full") or ""
if taps and raw and len(raw) == len(taps) and all(t is not None for t in taps):
for op, i, j in W.align(raw, final):
if op in ("m", "s") and (final[j].isalpha() or final[j] == " "):
add(taps[i], "space" if final[j] == " " else final[j], w)
ct = w.get("commit_touch")
if ct and ct.get("key0") == "space" and w.get("commit_src") is not None and not w.get("merged_into"):
committed = W.norm(w.get("committed") or "")
if committed and len(final) > len(committed) and final.startswith(committed) and final[len(committed)].isalpha():
add(ct, final[len(committed)], w)
else:
add(ct, "space", w)
os.makedirs(os.path.join(export, "out"), exist_ok=True)
json.dump({"layout": target["context"], "taps": out, "dropped": dropped}, open(os.path.join(export, "out", "taps.json"), "w"))
return out, dropped
_cache = {}
def load(export, half=None):
"""The taps (built if needed), one half's or all."""
p = os.path.join(export, "out", "taps.json")
key = (p, os.path.getmtime(p) if os.path.exists(p) else None)
if key not in _cache:
_cache.clear()
_cache[key] = json.load(open(p))["taps"] if os.path.exists(p) else build(export)[0]
return [t for t in _cache[key] if half is None or t["half"] == half]
if __name__ == "__main__":
ex = hexlog.export_dir(sys.argv[1:])
taps, dropped = build(ex)
by = collections.Counter((t["half"], "space" if t["meant"] == "space" else "letter") for t in taps)
src = collections.Counter(t["source"] for t in taps)
print("%d taps with a known meaning -> %s" % (len(taps), os.path.join(ex, "out", "taps.json")))
print(" half A: %d letters, %d spaces; half B: %d letters, %d spaces" % (by[("A", "letter")], by[("A", "space")], by[("B", "letter")], by[("B", "space")]))
print(" logged on (width x height, key height):", dict(src))
print(" not mapped:", dict(dropped))
wrong = sum(1 for t in taps if t["now"] != t["meant"])
print(" pressing another key than meant on today's keys: %d (%.1f%%)" % (wrong, 100 * wrong / max(1, len(taps))))