keyscore/src/layouts.bjo

207 lines
8.2 KiB
Text
Raw Normal View History

;; Layouts: which character each key of the board types.
;;
;; - A layout is written as three rows of characters, in the board's order:
;; the top row from Q, the home row from A, the bottom row from <. Spaces
;; are skipped, so a row can be split at the hands.
;; - A row may be shorter than the board's: its last keys type nothing.
;; - Letters are written in either case. ’ is the same character as '.
;; - The shifted character of a punctuation key is typed on that key:
;; , ; . : - _ ' * < >, as on Swedish QWERTY and Svdvorak.
(import (std prelude))
(import (keyscore board))
(export Layout make-layout aligned-rows layout-key layout-chars layout-base-chars swap-keys common-chars
normalize layouts)
;; - rows: the three rows as written, for making variants.
;; - keys: the key of every character typed, shifted ones included.
(type (: Layout (Record (: name string) (: rows (Vec string)) (: keys (Map char Key)))))
;; The shifted character typed on the same key as `c`.
(: (-> char (Option char)))
(defun (shifted c)
(match c
(#\, (Some #\;))
(#\. (Some #\:))
(#\- (Some #\_))
(#\' (Some #\*))
(#\< (Some #\>))
(_ None)))
;; A character as the scorer counts it: lower case, with ’ as '.
(: (-> char char))
(defun (normalize c)
(if (char=? c (int->char 0x2019)) #\' (char-downcase c)))
;; The keys of `row` on the board, in column order.
(: (-> int (List Key)))
(defun (row-keys row)
(->list (filter (fun (k) (= (record-ref k row) row)) board)))
;; A layout named `name` from its three rows. Raises if a row is longer than
;; the board's.
(: (-> string string string string Layout))
(defun (make-layout name top home bottom)
(def keys
(loop (:for (Tuple row text) (list (Tuple 0 top) (Tuple 1 home) (Tuple 2 bottom)))
(:let chars (->list (filter (fun (c) (not (char=? c #\space))) text)))
(:let ks (row-keys row))
(:do (when (> (list-length chars) (list-length ks))
(panic! (str name ": the row " (written text) " has more keys than the board"))))
(:for (Tuple c k) (zip chars ks))
(:let base (normalize c))
(:for (Tuple ch key) (match (shifted base)
((Some s) (list (Tuple base k) (Tuple s k)))
(None (list (Tuple base k)))))
(:acc out (mapping (Tuple ch key)))))
(Layout (name name) (rows [top home bottom]) (keys keys)))
;; The key that types `c`, if the layout has one.
(: (-> Layout char (Option Key)))
(defun (layout-key l c) (map-try-ref (record-ref l keys) c))
;; Every character the layout types.
(: (-> Layout (List char)))
(defun (layout-chars l) (->list (map-keys (record-ref l keys))))
;; The characters written in the layout's rows, normalized: one per key.
(: (-> Layout (List char)))
(defun (layout-base-chars l)
(loop (:for r (record-ref l rows))
(:let row r)
(:for c row)
(:when (not (char=? c #\space)))
(:acc out (listing (normalize c)))))
;; `l` with the keys of `a` and `b` swapped, named `name`. `a` and `b` are
;; compared with the rows' characters after `normalize`.
(: (-> Layout string char char Layout))
(defun (swap-keys l name a b)
(: swap (-> char char))
(def swap (fun (c)
(def n (normalize c))
(cond ((char=? n a) b) ((char=? n b) a) (else c))))
(def rows (vec-map (fun (r) (list->string (->list (map swap r)))) (record-ref l rows)))
(make-layout name (vec-ref rows 0) (vec-ref rows 1) (vec-ref rows 2)))
;; `rows` written with the hands apart, so that the rows line up in a
;; fixed-width font:
;;
;; ÖUOF, QVLBJ
;; IEADY CTRNS
;; ÅÄ.HK Z MGWPX
;;
;; - Top and home rows: five keys, three spaces, the rest.
;; - Bottom row: five keys, the middle key between single spaces, the rest.
;; - Spaces already in `rows` are dropped first.
(: (-> (Vec string) (Vec string)))
(defun (aligned-rows rows)
(: gap (-> int int string))
(def gap (fun (row i)
(cond ((and (< row 2) (= i 5)) " ")
((and (= row 2) (or (= i 5) (= i 6))) " ")
(else ""))))
(loop (:for text rows) (:for row (range 0 3))
(:let chars (->list (filter (fun (c) (not (char=? c #\space))) text)))
(:let parts (loop (:for c chars) (:for i (range 0 (list-length chars)))
(:acc out (vecing (str (gap row i) (char->string c))))))
(:acc out (vecing (string-join parts "")))))
;; The characters every layout in `ls` types.
(: (-> (Vec Layout) (List char)))
(defun (common-chars ls)
(->list (filter (fun (c) (all? (fun (l) (some? (layout-key l c))) ls))
(layout-chars (vec-ref ls 0)))))
(: (Vec Layout))
(def layouts
[(make-layout "QWERTY"
"qwert yuiopå"
"asdfg hjklöä'"
"<zxcvb nm,.-")
(make-layout "Svdvorak"
"å,.py fgcrl'"
"aoeui dhtns-<"
"öäqjkx bmwvz")
;; The best of 8 runs of src/optimize.bjo from UENAO, 2 000 000 steps each,
;; before the same-hand and hand-run rules, and with T starting at 0.3.
(make-layout "Found1"
"YBMPW Ö.H,’Z"
"ITSDG -ANERX"
"ÄVCFK Q ÅOLUJ")
;; The best of 8 runs of src/optimize.bjo from IENAO, 2 000 000 steps each,
;; with the same-hand and hand-run rules and T starting at 0.02. 7 runs of 8
;; ended within 0.001 of it.
(make-layout "Found2"
"JUKOÄ WCHGZQ"
"IENA. FDRTSB"
"-,YÖÅ ’ VPLMX")
;; The best of 8 runs of src/optimize.bjo from IENAO, 300 000 steps each,
;; with row motion, same-finger skips and the key costs doubled.
(make-layout "Found3"
"-YKÄÖ ZBLMJQ"
"IENA, FDRTSW"
"ÅUPO. ’ GCHVX")
(make-layout "UENAO"
"Å.JÄZ QCLHB’"
"UENAO DTRSK-"
"Y,WÖI X GMVFP")
;; UENAO with I and U swapped: the best single swap of src/swaps.bjo.
(make-layout "IENAO"
"Å.JÄZ QCLHB’"
"IENAO DTRSK-"
"Y,WÖU X GMVFP")
;; Found3 changed by hand. The right pinky's stretch keys type nothing, and
;; it has no - and ': these are not scored, as not every layout types them.
(make-layout "QYKÄÖ"
"QYKÄÖ ZBLMJ"
"IENA. FDRTS"
"Å,POU W GCHVX")
;; The best of 8 runs of src/optimize.bjo from QUPÅÄ, 2 000 000 steps each,
;; with T starting at 0.3. 3 runs of 8 ended on it. The vowels are on the
;; right hand.
(make-layout "JMLBW"
"JMLBW QFOUÖ"
"STRDP KNAEI"
"XV,GC Z YH.ÄÅ")
;; The best of the same runs with the vowels on the left hand. 2 runs of 8
;; ended on it.
(make-layout "ÖUOF,"
"ÖUOF, QVLBJ"
"IEADY CTRNS"
"ÅÄ.HK Z MGWPX")
;; Two searches from QUPÅÄ with sv.txt weighing 0.75 and en.txt 0.25,
;; 8 runs of 2 000 000 steps each: the best with T starting at 0.02 (7 runs
;; of 8 ended on it), and the best with T starting at 0.3 (2 runs of 8).
(make-layout "YÖFO."
"QUPÅÄ XBLMJ"
"IENA, GDRTS"
"YÖFO. Z KHCVW")
(make-layout "JMLPB"
"JMLPB QÄHUÅ"
"STNDF .AREI"
"XVCKG W ÖO,YZ")
;; The best of 8 runs of src/optimize.bjo from QYKÄÖ, 300 000 steps each.
(make-layout "QUPÅÄ"
"QUPÅÄ XBLMJ"
"IENA. GDRTS"
"ÖYFO, Z KHCVW")
;; The best of 12 runs of src/optimize.bjo from Svdvorak, 3 000 000 steps
;; each at t-start 0.3, with Svdvorak's Å Ä Ö . , fixed.
(make-layout "Å,.YZ"
"Å,.YZ QCHPW"
"IEADK VTRNS"
"ÖÄOFU X GMLBJ")
;; ZYK,Å refined at t-start 0.02 with Swedish weighing 3 to English's 1.
(make-layout "Z,K.Å"
"Z,K.Å XCHPW"
"IEDAÄ VTRNS"
"YUFÖO Q GMLBJ")
;; The best of 12 runs of src/optimize.bjo from QUPÅÄ, 3 000 000 steps each
;; at t-start 0.3, vowels kept left, with vowels on the top row costing more.
(make-layout "ZYK,Å"
"ZYK,Å XCHPW"
"IEDAÄ VTRNS"
"ÖUFO. Q GMLBJ")])