keyscore/src/swaps.bjo
Linus Björnstam 53a0002907 keyscore: a scorer, optimizer and GUI for keyboard layouts
Scores layouts for ISO Swedish with the angle mod on Swedish and
English text, by Michael Capewell's rules plus row motion, triples,
pinky load and finger distance.

- src/main.bjo: the report. src/optimize.bjo: simulated annealing.
- src/gui.bjo: a bjogui window for tuning the numbers, with help on
  every number, presets and saved layouts.
- corpus/: the texts, and fetch.py for the Wikipedia ones.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 11:14:39 +01:00

67 lines
2.7 KiB
Text

;; Tries swapping one key of a layout with each of its other keys.
;;
;; bjo run src/swaps.bjo [char] [layout]
;;
;; - char: the character to move. i unless given.
;; - layout: a layout's name in (keyscore layouts). The last one unless given.
;; - Scores each swap on the texts of (keyscore texts) and prints them best
;; first, by the weighted mean, the layout as it is among them. Lower is
;; better.
(import (std prelude))
(import (std fmt))
(import (keyscore layouts))
(import (keyscore params))
(import (keyscore score))
(import (keyscore texts))
;; The numbers scored with: params.txt when there is one.
(: Params)
(def params (current-params))
;; A layout's score on each text.
(: (-> (Vec Counts) Layout (Vec double)))
(defun (totals counts l) (vec-map (fun (c) (record-ref (score-layout params l c) total)) counts))
(: (-> (Vec double) double))
(defun (mean xs) (weighted-mean (record-ref params corpora) xs))
(: (-> double Block))
(defun (fixed x) (numeric x #:precision 2))
;; The layout named `name`. Raises if there is none.
(: (-> string Layout))
(defun (layout-named name)
(: named? (-> Layout bool))
(def named? (fun (l) (= (record-ref l name) name)))
(match (find named? layouts)
((Some l) l)
(None (panic! (str "no layout is named " name)))))
(defun (main args)
(def moved (if (> (length args) 0) (normalize (option-ref (first (ref args 0)))) #\i))
(def base (if (> (length args) 1)
(layout-named (ref args 1))
(vec-ref layouts (- (vec-length layouts) 1))))
(def counts (count-files layouts (vec-map (fun (c) (record-ref c path)) (record-ref params corpora))))
(def variants
(cons (Tuple "as it is" base)
(->list (map (fun (c)
(Tuple (str (char->string moved) " <-> " (char->string c))
(swap-keys base "" moved c)))
(filter (fun (c) (not (char=? c moved))) (layout-base-chars base))))))
(def scored
(vec-sort-by (fun (a b) (compare (mean (snd a)) (mean (snd b))))
(->vec (map (fun (v) (Tuple (fst v) (totals counts (snd v)))) variants))))
(show [(record-ref base name) ", with " (char->string moved) " swapped with each other key." nl])
(show (tabular/rows
(->vec (map (fun (s)
(: cells (Vec Block))
(def cells (vec-append (vec-append [(BStr (fst s))] (vec-map fixed (snd s)))
[(fixed (mean (snd s)))]))
cells)
scored))
#:titles ["swap" "sv" "en" "bjolang" "mean"]
#:sep " " #:align [AlignLeft AlignRight AlignRight AlignRight AlignRight]))
0)