731 lines
35 KiB
Text
731 lines
35 KiB
Text
|
|
;; A window for tuning the scorer's numbers and seeing what they do.
|
|||
|
|
;;
|
|||
|
|
;; bjo run src/gui.bjo
|
|||
|
|
;;
|
|||
|
|
;; Run it from the package's folder: the texts are read from corpus/.
|
|||
|
|
;;
|
|||
|
|
;; - Left: every layout's score, best first, rescored as soon as a number
|
|||
|
|
;; changes. A click chooses a layout for the Layout and Search tabs.
|
|||
|
|
;; - Rules: the numbers of (keyscore params), by group. Key costs: each key's
|
|||
|
|
;; cost, on the board.
|
|||
|
|
;; - Layout: the chosen layout on the board, and the parts of its score.
|
|||
|
|
;; - Search: runs (keyscore search) from the chosen layout with the numbers as
|
|||
|
|
;; they are. "Keep" adds a result to the list, "Copy" puts it on the
|
|||
|
|
;; clipboard as a make-layout form.
|
|||
|
|
;; - Save writes the numbers to params.txt, which the other programs read.
|
|||
|
|
;; Reload reads it again; Defaults puts back `default-params`, unsaved.
|
|||
|
|
;; - Presets, on the Rules tab: named sets of numbers in presets/. See
|
|||
|
|
;; (keyscore saved).
|
|||
|
|
;; - Layouts can be saved by name, from the Layout tab or from a search
|
|||
|
|
;; result, to saved-layouts.txt. The saved layouts are listed at start.
|
|||
|
|
;; - An "i" mark shows what a number or an option does.
|
|||
|
|
|
|||
|
|
(import (std prelude))
|
|||
|
|
(import (only (std fmt) numeric render->string))
|
|||
|
|
(import (except (bjogui view) Key))
|
|||
|
|
(import (bjogui app))
|
|||
|
|
(import (bjogui draw))
|
|||
|
|
(import (keyscore board))
|
|||
|
|
(import (keyscore layouts))
|
|||
|
|
(import (keyscore params))
|
|||
|
|
(import (keyscore saved))
|
|||
|
|
(import (keyscore score))
|
|||
|
|
(import (keyscore search))
|
|||
|
|
(import (keyscore texts))
|
|||
|
|
|
|||
|
|
(export Model Msg initial gui-update view subscriptions ranking)
|
|||
|
|
|
|||
|
|
;; --- the model ------------------------------------------------------------------
|
|||
|
|
|
|||
|
|
;; - params: the numbers scored with.
|
|||
|
|
;; - counts: the counts of the texts of `params`, empty until they are read.
|
|||
|
|
;; - layouts: (keyscore layouts), then the saved layouts and the search
|
|||
|
|
;; results kept.
|
|||
|
|
;; - scores: [layout][text], for `generation`: each change of the numbers
|
|||
|
|
;; starts a new one, and a score of an older one is dropped.
|
|||
|
|
;; - chosen: the layout the Layout and Search tabs show, by index in `layouts`.
|
|||
|
|
;; - options, searching, results: the Search tab.
|
|||
|
|
;; - saved: the names of the layouts in `layouts-file`.
|
|||
|
|
;; - layout-name: the name a layout is saved as.
|
|||
|
|
;; - presets, preset-name: the presets, and the name one is saved as.
|
|||
|
|
(type (: Model (Record (: params Params)
|
|||
|
|
(: counts (Vec Counts))
|
|||
|
|
(: layouts (Vec Layout))
|
|||
|
|
(: scores (Vec (Vec Score)))
|
|||
|
|
(: generation int)
|
|||
|
|
(: chosen int)
|
|||
|
|
(: tab int)
|
|||
|
|
(: options Options)
|
|||
|
|
(: searching bool)
|
|||
|
|
(: results (Vec Found))
|
|||
|
|
(: kept int)
|
|||
|
|
(: saved (Vec string))
|
|||
|
|
(: layout-name string)
|
|||
|
|
(: presets (Vec string))
|
|||
|
|
(: preset-name string)
|
|||
|
|
(: status string))))
|
|||
|
|
|
|||
|
|
(type (: Msg (Union (: Counted (Vec Counts))
|
|||
|
|
(: Scored int (Vec (Vec Score)))
|
|||
|
|
(: SetParam int double)
|
|||
|
|
(: Choose int)
|
|||
|
|
(: Tab int)
|
|||
|
|
(: SetRuns double)
|
|||
|
|
(: SetSteps double)
|
|||
|
|
(: SetTStart double)
|
|||
|
|
(: SetVowels bool)
|
|||
|
|
(: SetFixed string)
|
|||
|
|
Search
|
|||
|
|
(: Searched (Vec Found))
|
|||
|
|
(: Failed string)
|
|||
|
|
(: Keep int)
|
|||
|
|
(: Copy int)
|
|||
|
|
Save
|
|||
|
|
Saved
|
|||
|
|
Reload
|
|||
|
|
(: Loaded Params)
|
|||
|
|
Defaults
|
|||
|
|
(: SetPresetName string)
|
|||
|
|
SavePreset
|
|||
|
|
(: LoadPreset string)
|
|||
|
|
(: PresetLoaded string Params)
|
|||
|
|
(: DeletePreset string)
|
|||
|
|
(: Listed string (Vec string))
|
|||
|
|
(: SetLayoutName string)
|
|||
|
|
SaveLayout
|
|||
|
|
(: SaveResult int)
|
|||
|
|
RemoveLayout
|
|||
|
|
(: Wrote string)
|
|||
|
|
Noop)))
|
|||
|
|
|
|||
|
|
;; The layouts of `layouts-file` when the program starts.
|
|||
|
|
(: (Vec Layout))
|
|||
|
|
(def saved-at-start (read-saved-layouts))
|
|||
|
|
|
|||
|
|
(: (-> Layout string))
|
|||
|
|
(defun (lname l) (record-ref l name))
|
|||
|
|
|
|||
|
|
|
|||
|
|
(: Model)
|
|||
|
|
(def initial
|
|||
|
|
(Model (params (current-params))
|
|||
|
|
(counts [])
|
|||
|
|
(layouts (vec-append layouts saved-at-start))
|
|||
|
|
(scores [])
|
|||
|
|
(generation 0)
|
|||
|
|
(chosen (- (vec-length layouts) 1))
|
|||
|
|
(tab 0)
|
|||
|
|
(options (Options (runs 12) (steps 1000000) (t-start 0.3) (vowels-left #f) (fixed "")))
|
|||
|
|
(searching #f)
|
|||
|
|
(results [])
|
|||
|
|
(kept 0)
|
|||
|
|
(saved (vec-map lname saved-at-start))
|
|||
|
|
(layout-name "")
|
|||
|
|
(presets (preset-names))
|
|||
|
|
(preset-name "")
|
|||
|
|
(status "Reading the texts…")))
|
|||
|
|
|
|||
|
|
;; --- scoring --------------------------------------------------------------------
|
|||
|
|
|
|||
|
|
;; The score of each of `ls` on each of `counts`.
|
|||
|
|
(: (-> Params (Vec Layout) (Vec Counts) (Vec (Vec Score))))
|
|||
|
|
(defun (score-all p ls counts)
|
|||
|
|
(vec-map (fun (l) (vec-map (fun (c) (score-layout p l c)) counts)) ls))
|
|||
|
|
|
|||
|
|
;; The mean of `xs` weighted by the texts' weights, or 0 when none weighs
|
|||
|
|
;; anything.
|
|||
|
|
(: (-> Params (Vec double) double))
|
|||
|
|
(defun (mean p xs)
|
|||
|
|
(def cs (record-ref p corpora))
|
|||
|
|
(if (> (vec-fold (fun (c t) (+ t (corpus-weight c))) 0.0 cs) 0.0) (weighted-mean cs xs) 0.0))
|
|||
|
|
|
|||
|
|
;; `f` of each text's score of the layout `i`, as the weighted mean.
|
|||
|
|
(: (-> Model int (-> Score double) double))
|
|||
|
|
(defun (mean-of m i f) (mean (.params m) (vec-map f (vec-ref (.scores m) i))))
|
|||
|
|
|
|||
|
|
;; The indices of the layouts, best first. Empty until they are scored.
|
|||
|
|
(: (-> Model (Vec int)))
|
|||
|
|
(defun (ranking m)
|
|||
|
|
(if (= (vec-length (.scores m)) (vec-length (.layouts m)))
|
|||
|
|
(vec-sort-by (fun (a b) (compare (mean-of m a (fun (s) (.total s))) (mean-of m b (fun (s) (.total s)))))
|
|||
|
|
(->vec (range 0 (vec-length (.layouts m)))))
|
|||
|
|
[]))
|
|||
|
|
|
|||
|
|
;; A message for the result of `blocking` work: `(f x)` for its value, or
|
|||
|
|
;; `(Failed what)` when it raised.
|
|||
|
|
(: (-> (-> %a Msg) string (-> (Result System.Exception %a) Msg)))
|
|||
|
|
(defun (on-ok f what)
|
|||
|
|
(fun (r) (match r ((Ok x) (f x)) ((Err _) (Failed what)))))
|
|||
|
|
|
|||
|
|
;; The model with `p`, and the command that scores the layouts with it.
|
|||
|
|
(: (-> Model Params (Tuple Model (List (Event Msg)))))
|
|||
|
|
(defun (rescore m p)
|
|||
|
|
(def g (+ (.generation m) 1))
|
|||
|
|
(def ls (.layouts m))
|
|||
|
|
(def counts (.counts m))
|
|||
|
|
(if (= (vec-length counts) 0)
|
|||
|
|
(next (record-set m (params p) (generation g)))
|
|||
|
|
(next (record-set m (params p) (generation g))
|
|||
|
|
(wrap (blocking (fun () (score-all p ls counts)))
|
|||
|
|
(on-ok (fun (s) (Scored g s)) "Scoring failed.")))))
|
|||
|
|
|
|||
|
|
;; `step` with the command `c` as well.
|
|||
|
|
(: (-> (Tuple Model (List (Event Msg))) (Event Msg) (Tuple Model (List (Event Msg)))))
|
|||
|
|
(defun (and-then step c)
|
|||
|
|
(def (Tuple m cs) step)
|
|||
|
|
(Tuple m (cons c cs)))
|
|||
|
|
|
|||
|
|
;; The index of the layout named `n` in `ls`, if there is one.
|
|||
|
|
(: (-> (Vec Layout) string (Option int)))
|
|||
|
|
(defun (index-named ls n)
|
|||
|
|
(find (fun (i) (= (lname (vec-ref ls i)) n)) (range 0 (vec-length ls))))
|
|||
|
|
|
|||
|
|
;; The command that writes the saved layouts of `ls` to `layouts-file`.
|
|||
|
|
(: (-> (Vec Layout) (Vec string) string (Event Msg)))
|
|||
|
|
(defun (write-layouts ls saved done)
|
|||
|
|
(def built (vec-length layouts))
|
|||
|
|
(def to-write (loop (:for l ls) (:for i (range 0 (vec-length ls)))
|
|||
|
|
(:when (and (>= i built) (vec-contains? saved (lname l))))
|
|||
|
|
(:acc out (vecing l))))
|
|||
|
|
(wrap (blocking (fun () (write-saved-layouts to-write) #t))
|
|||
|
|
(on-ok (fun (_) (Wrote done)) (str "Could not write " layouts-file "."))))
|
|||
|
|
|
|||
|
|
(: string)
|
|||
|
|
(def bad-name "A name cannot be empty, start or end with a space, start with a dot, or hold / \\ or a tab.")
|
|||
|
|
|
|||
|
|
;; `m` with `l` saved as `name`, and the commands that score the layouts and
|
|||
|
|
;; write the file.
|
|||
|
|
;;
|
|||
|
|
;; - replacing: the index of a layout that `l` takes the place of, such as a
|
|||
|
|
;; kept result or a saved layout given a new name. A built-in layout is
|
|||
|
|
;; never replaced.
|
|||
|
|
;; - Another layout that is not built in and has the same name is replaced.
|
|||
|
|
;; - choose?: whether the saved layout becomes the chosen one. If not, the
|
|||
|
|
;; chosen layout stays chosen, unless it is replaced.
|
|||
|
|
;; - Rejects an invalid name, and the name of a built-in layout.
|
|||
|
|
(: (-> Model Layout (Option int) string bool (Tuple Model (List (Event Msg)))))
|
|||
|
|
(defun (save-layout m l replacing name choose?)
|
|||
|
|
(def ls (.layouts m))
|
|||
|
|
(def built (vec-length layouts))
|
|||
|
|
(cond
|
|||
|
|
((not (valid-name? name)) (next (record-set m (status bad-name))))
|
|||
|
|
((some? (index-named layouts name))
|
|||
|
|
(next (record-set m (status (str name " is a built-in layout. Choose another name.")))))
|
|||
|
|
(else
|
|||
|
|
(def replaced (match replacing
|
|||
|
|
((Some i) (if (>= i built) (Some i) None))
|
|||
|
|
(None None)))
|
|||
|
|
(def old (match replaced ((Some i) (Some (lname (vec-ref ls i)))) (None None)))
|
|||
|
|
(def others (loop (:for x ls) (:for i (range 0 (vec-length ls)))
|
|||
|
|
(:when (or (< i built)
|
|||
|
|
(and (not (= (Some i) replaced)) (not (= (lname x) name)))))
|
|||
|
|
(:acc out (vecing x))))
|
|||
|
|
(def new-ls (vec-append others [(record-set l (name name))]))
|
|||
|
|
(def last (- (vec-length new-ls) 1))
|
|||
|
|
(def before (lname (vec-ref ls (.chosen m))))
|
|||
|
|
(def chosen (if choose? last (option-ref-or (index-named new-ls before) last)))
|
|||
|
|
(def saved (vec-append (vec-filter (fun (n) (not (or (= n name) (= (Some n) old)))) (.saved m)) [name]))
|
|||
|
|
(and-then (rescore (record-set m (layouts new-ls) (saved saved) (chosen chosen) (scores [])
|
|||
|
|
(layout-name name))
|
|||
|
|
(.params m))
|
|||
|
|
(write-layouts new-ls saved (str "Saved the layout " name " to " layouts-file "."))))))
|
|||
|
|
|
|||
|
|
;; Whether `l` types a vowel on the right hand.
|
|||
|
|
(: (-> Layout bool))
|
|||
|
|
(defun (vowel-right? l)
|
|||
|
|
(any? (fun (c) (match (layout-key l c)
|
|||
|
|
((Some k) (not (record-ref k left?)))
|
|||
|
|
(None #f)))
|
|||
|
|
(->list "aeiouyåäö")))
|
|||
|
|
|
|||
|
|
;; --- update ---------------------------------------------------------------------
|
|||
|
|
|
|||
|
|
(: gui-update (-> Msg Model (Tuple Model (List (Event Msg)))))
|
|||
|
|
(defun (gui-update msg m)
|
|||
|
|
(def o (.options m))
|
|||
|
|
(match msg
|
|||
|
|
((Counted cs) (rescore (record-set m (counts cs) (status "")) (.params m)))
|
|||
|
|
((Scored g s) (next (if (= g (.generation m)) (record-set m (scores s)) m)))
|
|||
|
|
((SetParam i x) (rescore m ((record-ref (vec-ref param-specs i) set) (.params m) x)))
|
|||
|
|
((Choose pos)
|
|||
|
|
(def r (ranking m))
|
|||
|
|
(if (and (>= pos 0) (< pos (vec-length r)))
|
|||
|
|
(let ((i (vec-ref r pos)))
|
|||
|
|
(next (record-set m (chosen i)
|
|||
|
|
(layout-name (if (< i (vec-length layouts)) (.layout-name m)
|
|||
|
|
(lname (vec-ref (.layouts m) i)))))))
|
|||
|
|
(next m)))
|
|||
|
|
((Tab t) (next (record-set m (tab t))))
|
|||
|
|
((SetRuns x) (next (record-set m (options (record-set o (runs (cast int x)))))))
|
|||
|
|
((SetSteps x) (next (record-set m (options (record-set o (steps (cast int x)))))))
|
|||
|
|
((SetTStart x) (next (record-set m (options (record-set o (t-start x))))))
|
|||
|
|
((SetVowels b) (next (record-set m (options (record-set o (vowels-left b))))))
|
|||
|
|
((SetFixed s) (next (record-set m (options (record-set o (fixed s))))))
|
|||
|
|
(Search
|
|||
|
|
(def l (vec-ref (.layouts m) (.chosen m)))
|
|||
|
|
(def p (.params m))
|
|||
|
|
(def counts (.counts m))
|
|||
|
|
(cond ((= (vec-length counts) 0) (next m))
|
|||
|
|
((= (mean p (vec-map (fun (c) 1.0) counts)) 0.0)
|
|||
|
|
(next (record-set m (status "No text weighs anything."))))
|
|||
|
|
((and (.vowels-left o) (vowel-right? l))
|
|||
|
|
(next (record-set m (status (str (record-ref l name) " has a vowel on the right hand.")))))
|
|||
|
|
(else
|
|||
|
|
(next (record-set m (searching #t) (results [])
|
|||
|
|
(status (str "Searching from " (record-ref l name) "…")))
|
|||
|
|
(wrap (blocking (fun () (search (problem-of p l counts) o)))
|
|||
|
|
(on-ok Searched "The search failed."))))))
|
|||
|
|
((Searched found)
|
|||
|
|
(next (record-set m (searching #f) (results (vec-sort-by (fun (a b) (compare (.score a) (.score b))) found))
|
|||
|
|
(status ""))))
|
|||
|
|
((Failed what) (next (record-set m (searching #f) (status what))))
|
|||
|
|
((Keep i)
|
|||
|
|
(def rows (.rows (vec-ref (.results m) i)))
|
|||
|
|
(def n (+ (.kept m) 1))
|
|||
|
|
(def l (make-layout (str "found " (displayed n)) (vec-ref rows 0) (vec-ref rows 1) (vec-ref rows 2)))
|
|||
|
|
(rescore (record-set m (layouts (vec-append (.layouts m) [l])) (kept n) (scores [])) (.params m)))
|
|||
|
|
((Copy i)
|
|||
|
|
(def rows (.rows (vec-ref (.results m) i)))
|
|||
|
|
(next m (copy-text (str "(make-layout \"found\"\n \"" (vec-ref rows 0) "\"\n \""
|
|||
|
|
(vec-ref rows 1) "\"\n \"" (vec-ref rows 2) "\")"))))
|
|||
|
|
(Save
|
|||
|
|
(def p (.params m))
|
|||
|
|
(next m (wrap (blocking (fun () (save-params p params-file) #t))
|
|||
|
|
(on-ok (fun (_) Saved) (str "Could not write " params-file ".")))))
|
|||
|
|
(Saved (next (record-set m (status (str "Saved to " params-file ".")))))
|
|||
|
|
(Reload (next m (wrap (blocking current-params) (on-ok Loaded (str "Could not read " params-file ".")))))
|
|||
|
|
((Loaded p) (rescore (record-set m (status (if (file-exists? params-file)
|
|||
|
|
(str "Read " params-file ".")
|
|||
|
|
(str "No " params-file ": the defaults."))))
|
|||
|
|
p))
|
|||
|
|
(Defaults (rescore (record-set m (status "The defaults, not saved.")) default-params))
|
|||
|
|
((SetPresetName s) (next (record-set m (preset-name s))))
|
|||
|
|
(SavePreset
|
|||
|
|
(def name (.preset-name m))
|
|||
|
|
(def p (.params m))
|
|||
|
|
(if (valid-name? name)
|
|||
|
|
(next m (wrap (blocking (fun () (save-preset name p)))
|
|||
|
|
(on-ok (fun (names) (Listed (str "Saved the preset " name ".") names))
|
|||
|
|
(str "Could not save the preset " name "."))))
|
|||
|
|
(next (record-set m (status bad-name)))))
|
|||
|
|
((LoadPreset name)
|
|||
|
|
(next m (wrap (blocking (fun () (load-preset name)))
|
|||
|
|
(on-ok (fun (p) (PresetLoaded name p)) (str "Could not read the preset " name ".")))))
|
|||
|
|
((PresetLoaded name p)
|
|||
|
|
(rescore (record-set m (preset-name name)
|
|||
|
|
(status (str "Loaded the preset " name ". Save writes it to " params-file ".")))
|
|||
|
|
p))
|
|||
|
|
((DeletePreset name)
|
|||
|
|
(next m (wrap (blocking (fun () (delete-preset name)))
|
|||
|
|
(on-ok (fun (names) (Listed (str "Deleted the preset " name ".") names))
|
|||
|
|
(str "Could not delete the preset " name ".")))))
|
|||
|
|
((Listed what names) (next (record-set m (presets names) (status what))))
|
|||
|
|
((SetLayoutName s) (next (record-set m (layout-name s))))
|
|||
|
|
(SaveLayout
|
|||
|
|
(def i (.chosen m))
|
|||
|
|
(save-layout m (vec-ref (.layouts m) i) (Some i) (.layout-name m) #t))
|
|||
|
|
((SaveResult i)
|
|||
|
|
(def rows (.rows (vec-ref (.results m) i)))
|
|||
|
|
(save-layout m (make-layout "" (vec-ref rows 0) (vec-ref rows 1) (vec-ref rows 2)) None (.layout-name m) #f))
|
|||
|
|
(RemoveLayout
|
|||
|
|
(def i (.chosen m))
|
|||
|
|
(def ls (.layouts m))
|
|||
|
|
(def built (vec-length layouts))
|
|||
|
|
(if (< i built)
|
|||
|
|
(next (record-set m (status "A built-in layout cannot be removed.")))
|
|||
|
|
(let ((name (lname (vec-ref ls i))))
|
|||
|
|
(def new-ls (loop (:for x ls) (:for j (range 0 (vec-length ls)))
|
|||
|
|
(:when (not (= j i)))
|
|||
|
|
(:acc out (vecing x))))
|
|||
|
|
(def saved (vec-filter (fun (n) (not (= n name))) (.saved m)))
|
|||
|
|
(def step (rescore (record-set m (layouts new-ls) (saved saved) (chosen (- built 1)) (scores [])
|
|||
|
|
(status (str "Removed " name ".")))
|
|||
|
|
(.params m)))
|
|||
|
|
(if (vec-contains? (.saved m) name)
|
|||
|
|
(and-then step (write-layouts new-ls saved (str "Removed " name " from " layouts-file ".")))
|
|||
|
|
step))))
|
|||
|
|
((Wrote what) (next (record-set m (status what))))
|
|||
|
|
(Noop (next m))))
|
|||
|
|
|
|||
|
|
(: subscriptions (-> Model (List (Sub Msg))))
|
|||
|
|
(defun (subscriptions m)
|
|||
|
|
(if (= (vec-length (.counts m)) 0)
|
|||
|
|
(let ((paths (vec-map corpus-path (record-ref (.params m) corpora))))
|
|||
|
|
(list (task "count" (wrap (blocking (fun () (count-files layouts paths)))
|
|||
|
|
(on-ok Counted "Could not read the texts.")))))
|
|||
|
|
'()))
|
|||
|
|
|
|||
|
|
;; --- the board --------------------------------------------------------------------
|
|||
|
|
|
|||
|
|
(: double)
|
|||
|
|
(def key-size 38.0)
|
|||
|
|
|
|||
|
|
;; The colour of a finger's keys: pinky, ring, middle, index.
|
|||
|
|
(: (-> Key string))
|
|||
|
|
(defun (finger-colour k)
|
|||
|
|
(match (record-ref k finger)
|
|||
|
|
(0 "#f4c7c3")
|
|||
|
|
(1 "#fce8b2")
|
|||
|
|
(2 "#b7e1cd")
|
|||
|
|
(_ "#c6dafc")))
|
|||
|
|
|
|||
|
|
;; Where `k` is drawn, in pixels: its position on the board, the right hand
|
|||
|
|
;; half a key further right.
|
|||
|
|
(: (-> Key (Tuple double double)))
|
|||
|
|
(defun (key-place k)
|
|||
|
|
(def (Tuple x y) (key-position k))
|
|||
|
|
(def gap (if (record-ref k left?) 0.0 0.5))
|
|||
|
|
(Tuple (* key-size (+ x 0.5 gap)) (* key-size y)))
|
|||
|
|
|
|||
|
|
(: double)
|
|||
|
|
(def board-width (* key-size 13.0))
|
|||
|
|
|
|||
|
|
(: double)
|
|||
|
|
(def board-height (* key-size 3.0))
|
|||
|
|
|
|||
|
|
;; The board, with a label on each key of `board`, in its order. A stretch
|
|||
|
|
;; key has a darker edge.
|
|||
|
|
(: keyboard (Component (Vec string) Unit Unit))
|
|||
|
|
(def keyboard
|
|||
|
|
(component unit
|
|||
|
|
(fun (pen w h s labels)
|
|||
|
|
(loop (:for k board) (:for label labels)
|
|||
|
|
(:let (Tuple x y) (key-place k))
|
|||
|
|
(:let size (- key-size 4.0))
|
|||
|
|
(:do (fill-rect pen x y size size (finger-colour k) #:radius 5.0)
|
|||
|
|
(when (record-ref k stretch?)
|
|||
|
|
(stroke-rect pen x y size size "#888888" #:width 1.5 #:radius 5.0))
|
|||
|
|
(draw-text pen label (+ x (/ size 2.0)) (+ y 7.0) "#202124"
|
|||
|
|
#:size 17.0 #:bold #t #:align "centre"))))))
|
|||
|
|
|
|||
|
|
;; The character `l` puts on each key of `board`, upper case, or "".
|
|||
|
|
(: (-> Layout (Vec string)))
|
|||
|
|
(defun (labels-of l)
|
|||
|
|
(def on-key (loop (:for c (layout-base-chars l))
|
|||
|
|
(:let k (option-ref (layout-key l c)))
|
|||
|
|
(:acc out (mapping (Tuple (record-ref k index) c)))))
|
|||
|
|
(vec-map (fun (k) (match (map-try-ref on-key (record-ref k index))
|
|||
|
|
((Some c) (if (char=? c #\') "’" (char->string (char-upcase c))))
|
|||
|
|
(None "")))
|
|||
|
|
board))
|
|||
|
|
|
|||
|
|
(: (-> (Vec string) (View Msg)))
|
|||
|
|
(defun (board-view labels)
|
|||
|
|
(drawn keyboard labels (fun (_) Noop) #:width board-width #:height board-height))
|
|||
|
|
|
|||
|
|
;; --- formatting ---------------------------------------------------------------------
|
|||
|
|
|
|||
|
|
(: (-> double string))
|
|||
|
|
(defun (fixed x) (render->string (numeric x #:precision 2)))
|
|||
|
|
|
|||
|
|
(: (-> double string))
|
|||
|
|
(defun (fixed3 x) (render->string (numeric x #:precision 3)))
|
|||
|
|
|
|||
|
|
(: (-> double string))
|
|||
|
|
(defun (pct x) (str (render->string (numeric (* 100.0 x) #:precision 1)) "%"))
|
|||
|
|
|
|||
|
|
;; A text's name in a heading: its file name with no .txt.
|
|||
|
|
(: (-> string string))
|
|||
|
|
(defun (file-label path)
|
|||
|
|
(def name (option-ref-or (path-filename path) path))
|
|||
|
|
(if (string-ends-with? name ".txt") (string-drop-right name 4) name))
|
|||
|
|
|
|||
|
|
;; The number `s` holds, if it holds one.
|
|||
|
|
(: (-> string (Option double)))
|
|||
|
|
(defun (number-in s)
|
|||
|
|
(result->option (try (string->double s) #:catch (System.Exception))))
|
|||
|
|
|
|||
|
|
;; A grid of rows of texts, the first column left and the others right.
|
|||
|
|
(: (-> string (Vec (Vec string)) (View Msg)))
|
|||
|
|
(defun (table columns rows)
|
|||
|
|
(apply grid #:columns columns
|
|||
|
|
#:rows (string-join (vec-map (fun (_) "Auto") rows) ",")
|
|||
|
|
#:spacing 4.0
|
|||
|
|
(vec->array (loop (:for r rows) (:for i (range 0 (vec-length rows)))
|
|||
|
|
(:let cells r)
|
|||
|
|
(:for cell cells) (:for j (range 0 (vec-length cells)))
|
|||
|
|
(:acc out (vecing (at i j (text cell))))))))
|
|||
|
|
|
|||
|
|
;; --- the views ---------------------------------------------------------------------
|
|||
|
|
|
|||
|
|
;; A blue circle with an "i", 16 pixels across.
|
|||
|
|
(: info-mark (Component Unit Unit Unit))
|
|||
|
|
(def info-mark
|
|||
|
|
(component unit
|
|||
|
|
(fun (pen w h s _)
|
|||
|
|
(fill-circle pen 8.0 8.0 8.0 "#1a73e8")
|
|||
|
|
(draw-text pen "i" 8.0 0.5 "#ffffff" #:size 12.0 #:bold #t #:align "centre"))))
|
|||
|
|
|
|||
|
|
;; A small "i" mark that shows `help` when the pointer rests on it.
|
|||
|
|
(: (-> string (View Msg)))
|
|||
|
|
(defun (info help)
|
|||
|
|
(tooltip (drawn info-mark unit (fun (_) Noop) #:width 16.0 #:height 16.0) help))
|
|||
|
|
|
|||
|
|
;; `v` with an "i" mark after it, and `help` shown on both. Where a row has an
|
|||
|
|
;; input, the mark goes after the input instead.
|
|||
|
|
(: (-> (View Msg) string (View Msg)))
|
|||
|
|
(defun (with-info v help)
|
|||
|
|
(row #:spacing 6.0 (tooltip v help) (info help)))
|
|||
|
|
|
|||
|
|
;; The help of `s`, and its default.
|
|||
|
|
(: (-> Spec string))
|
|||
|
|
(defun (spec-help s)
|
|||
|
|
(str (record-ref s help) "\n\nDefault: " (displayed ((record-ref s get) default-params)) "."))
|
|||
|
|
|
|||
|
|
(: (-> Model (View Msg)))
|
|||
|
|
(defun (scores-view m)
|
|||
|
|
(def cs (record-ref (.params m) corpora))
|
|||
|
|
(def r (ranking m))
|
|||
|
|
(def columns (string-join (vec-append ["90" "60"] (vec-map (fun (_) "60") cs)) ","))
|
|||
|
|
(: line (-> (Vec string) (View Msg)))
|
|||
|
|
(def line (fun (cells)
|
|||
|
|
(apply grid #:columns columns
|
|||
|
|
(vec->array (loop (:for c cells) (:for j (range 0 (vec-length cells)))
|
|||
|
|
(:acc out (vecing (at 0 j (text c)))))))))
|
|||
|
|
(def items
|
|||
|
|
(vec-map (fun (i)
|
|||
|
|
(def l (vec-ref (.layouts m) i))
|
|||
|
|
(keyed (record-ref l name)
|
|||
|
|
(line (vec-append [(record-ref l name) (fixed (mean-of m i (fun (s) (.total s))))]
|
|||
|
|
(vec-map (fun (s) (fixed (.total s))) (vec-ref (.scores m) i))))))
|
|||
|
|
r))
|
|||
|
|
(def selected (match (find (fun (p) (= (vec-ref r p) (.chosen m))) (range 0 (vec-length r)))
|
|||
|
|
((Some p) p)
|
|||
|
|
(None -1)))
|
|||
|
|
(column #:spacing 6.0 #:margin 8.0
|
|||
|
|
(text "Score per key press, lower is better" #:size 15.0)
|
|||
|
|
(line (vec-append ["layout" "mean"] (vec-map (fun (c) (file-label (corpus-path c))) cs)))
|
|||
|
|
(list-box items selected Choose #:width 340.0)))
|
|||
|
|
|
|||
|
|
;; The numbers of one group, each with its input.
|
|||
|
|
(: (-> Model string (View Msg)))
|
|||
|
|
(defun (group-view m group)
|
|||
|
|
(def p (.params m))
|
|||
|
|
(def specs (loop (:for s param-specs) (:for i (range 0 (vec-length param-specs)))
|
|||
|
|
(:when (= (record-ref s group) group))
|
|||
|
|
(:acc out (vecing (Tuple i s)))))
|
|||
|
|
(border
|
|||
|
|
(column #:spacing 6.0
|
|||
|
|
(text group #:size 15.0)
|
|||
|
|
(apply grid #:columns "Auto,130,Auto" #:rows (string-join (vec-map (fun (_) "Auto") specs) ",") #:spacing 6.0
|
|||
|
|
(vec->array
|
|||
|
|
(loop (:for (Tuple i s) specs) (:for row (range 0 (vec-length specs)))
|
|||
|
|
(:let input (number-input ((record-ref s get) p) (fun (x) (SetParam i x))
|
|||
|
|
#:minimum (record-ref s minimum)
|
|||
|
|
#:maximum (record-ref s maximum)
|
|||
|
|
#:step (record-ref s step)
|
|||
|
|
#:width 130.0))
|
|||
|
|
(:let help (spec-help s))
|
|||
|
|
(:for cell (list (at row 0 (tooltip (text (record-ref s label)) help))
|
|||
|
|
(at row 1 (tooltip input help))
|
|||
|
|
(at row 2 (info help))))
|
|||
|
|
(:acc out (vecing cell))))))
|
|||
|
|
#:padding 10.0 #:radius 6.0 #:colour "#9e9e9e"))
|
|||
|
|
|
|||
|
|
;; The presets: a name to save the numbers as, and each preset with Load and
|
|||
|
|
;; Delete.
|
|||
|
|
(: (-> Model (View Msg)))
|
|||
|
|
(defun (presets-view m)
|
|||
|
|
(def items
|
|||
|
|
(if (= (vec-length (.presets m)) 0)
|
|||
|
|
[(text "No presets yet.")]
|
|||
|
|
(vec-map (fun (n)
|
|||
|
|
(keyed n (row #:spacing 6.0
|
|||
|
|
(text n #:width 160.0)
|
|||
|
|
(tooltip (button "Load" (LoadPreset n)) (str "Use the numbers of " n "."))
|
|||
|
|
(tooltip (button "Delete" (DeletePreset n)) (str "Delete the file of " n ".")))))
|
|||
|
|
(.presets m))))
|
|||
|
|
(border
|
|||
|
|
(column #:spacing 6.0
|
|||
|
|
(text "Presets" #:size 15.0)
|
|||
|
|
(row #:spacing 6.0
|
|||
|
|
(text-input (.preset-name m) SetPresetName #:placeholder "name" #:width 160.0)
|
|||
|
|
(tooltip (button "Save preset" SavePreset)
|
|||
|
|
"Saves the numbers as they are under this name. A preset of the same name is replaced.")
|
|||
|
|
(info (str "A preset is a named set of all the numbers, key costs and text weights included. "
|
|||
|
|
"Presets are kept in the " presets-folder " folder. Load uses a preset's numbers; "
|
|||
|
|
"Save at the top writes them to " params-file " for the other programs.")))
|
|||
|
|
(apply column #:spacing 4.0 (vec->array items)))
|
|||
|
|
#:padding 10.0 #:radius 6.0 #:colour "#9e9e9e"))
|
|||
|
|
|
|||
|
|
(: (-> Model (View Msg)))
|
|||
|
|
(defun (rules-view m)
|
|||
|
|
(def groups ["Texts" "Keys" "Pairs" "Row motion" "Triples" "Pinky load" "Finger distance"])
|
|||
|
|
(scroll (apply flow #:spacing 10.0 #:margin 8.0
|
|||
|
|
(vec->array (vec-append [(presets-view m)] (vec-map (fun (g) (group-view m g)) groups))))))
|
|||
|
|
|
|||
|
|
;; The key costs, each input where its key is on the board.
|
|||
|
|
(: (-> Model (View Msg)))
|
|||
|
|
(defun (key-costs-view m)
|
|||
|
|
(def p (.params m))
|
|||
|
|
(def width 56.0)
|
|||
|
|
(: cell (-> Key (View Msg)))
|
|||
|
|
(def cell (fun (k)
|
|||
|
|
(def i (option-ref (find (fun (j) (= (record-ref (vec-ref param-specs j) name)
|
|||
|
|
(str "key." (record-ref k name))))
|
|||
|
|
(range 0 (vec-length param-specs)))))
|
|||
|
|
(def s (vec-ref param-specs i))
|
|||
|
|
(tooltip (column #:spacing 2.0
|
|||
|
|
(text (record-ref s label))
|
|||
|
|
(text-input (displayed ((record-ref s get) p))
|
|||
|
|
(fun (t) (match (number-in t) ((Some x) (SetParam i x)) (None Noop)))
|
|||
|
|
#:width (- width 4.0)))
|
|||
|
|
(spec-help s))))
|
|||
|
|
(: board-row (-> int (View Msg)))
|
|||
|
|
(def board-row (fun (r)
|
|||
|
|
(: on-row? (-> Key bool))
|
|||
|
|
(def on-row? (fun (k) (= (record-ref k row) r)))
|
|||
|
|
(def ks (->vec (filter on-row? board)))
|
|||
|
|
(def (Tuple x0 _) (key-position (vec-ref ks 0)))
|
|||
|
|
;; The first key of the right hand, with a gap before it.
|
|||
|
|
(: first-right? (-> int bool))
|
|||
|
|
(def first-right? (fun (j) (and (> j 0) (not (record-ref (vec-ref ks j) left?))
|
|||
|
|
(record-ref (vec-ref ks (- j 1)) left?))))
|
|||
|
|
(apply row #:spacing 4.0
|
|||
|
|
(vec->array
|
|||
|
|
(vec-append [(text "" #:width (* (+ x0 0.5) width))]
|
|||
|
|
(loop (:for k ks) (:for j (range 0 (vec-length ks)))
|
|||
|
|
(:acc out (vecing (if (first-right? j)
|
|||
|
|
(row (text "" #:width (/ width 2.0)) (cell k))
|
|||
|
|
(cell k))))))))))
|
|||
|
|
(column #:spacing 10.0 #:margin 8.0
|
|||
|
|
(with-info (text (str "Each press costs its key's cost times the key weight, "
|
|||
|
|
(displayed (record-ref p key-weight)) ". Set the key weight under Rules."))
|
|||
|
|
"Point at a key to see its default cost. A key of the right pinky past P and Ö, and the centre columns, are stretch keys: the Stretch key number is added to pairs with them.")
|
|||
|
|
(board-row 0) (board-row 1) (board-row 2)))
|
|||
|
|
|
|||
|
|
;; What the chosen layout is: built in, saved, or kept and not saved.
|
|||
|
|
(: (-> Model string))
|
|||
|
|
(defun (kind-of-chosen m)
|
|||
|
|
(def i (.chosen m))
|
|||
|
|
(cond ((< i (vec-length layouts)) "built in")
|
|||
|
|
((vec-contains? (.saved m) (lname (vec-ref (.layouts m) i))) (str "saved in " layouts-file))
|
|||
|
|
(else "kept, not saved")))
|
|||
|
|
|
|||
|
|
;; The name input and the Save layout and Remove buttons.
|
|||
|
|
(: (-> Model (View Msg)))
|
|||
|
|
(defun (save-layout-view m)
|
|||
|
|
(def built? (< (.chosen m) (vec-length layouts)))
|
|||
|
|
(row #:spacing 6.0
|
|||
|
|
(text "Save as")
|
|||
|
|
(text-input (.layout-name m) SetLayoutName #:placeholder "name" #:width 160.0)
|
|||
|
|
(tooltip (button "Save layout" SaveLayout)
|
|||
|
|
(str "Saves the chosen layout under this name in " layouts-file ". A built-in layout is copied. "
|
|||
|
|
"A kept or saved layout gets the new name. A saved layout of the same name is replaced."))
|
|||
|
|
(tooltip (button "Remove" RemoveLayout #:enabled (not built?))
|
|||
|
|
(str "Removes the chosen layout from the list, and from " layouts-file " if it is saved there."))
|
|||
|
|
(info (str "Saved layouts are kept in " layouts-file ", one a line, and are listed at start."))))
|
|||
|
|
|
|||
|
|
;; The chosen layout on the board, and the parts of its score.
|
|||
|
|
(: (-> Model (View Msg)))
|
|||
|
|
(defun (layout-view m)
|
|||
|
|
(def i (.chosen m))
|
|||
|
|
(def l (vec-ref (.layouts m) i))
|
|||
|
|
(def heading (row #:spacing 10.0 (text (record-ref l name) #:size 18.0) (text (str "(" (kind-of-chosen m) ")"))))
|
|||
|
|
(if (< i (vec-length (.scores m)))
|
|||
|
|
(let ((cs (record-ref (.params m) corpora))
|
|||
|
|
(scores (vec-ref (.scores m) i)))
|
|||
|
|
(: part (-> string (-> Score double) (-> double string) (Vec string)))
|
|||
|
|
(def part (fun (label f show)
|
|||
|
|
(vec-append (vec-append [label] (vec-map (fun (s) (show (f s))) scores))
|
|||
|
|
[(show (mean-of m i f))])))
|
|||
|
|
(def rule-rows (loop (:for r rules) (:for j (range 0 (vec-length rules)))
|
|||
|
|
(:acc out (vecing (part (record-ref r name) (fun (s) (vec-ref (.rules s) j)) fixed)))))
|
|||
|
|
(def rows
|
|||
|
|
(vec-append
|
|||
|
|
(vec-append [(vec-append (vec-append ["part"] (vec-map (fun (c) (file-label (corpus-path c))) cs)) ["mean"])
|
|||
|
|
(part "key costs" (fun (s) (.position s)) fixed)
|
|||
|
|
(part "top-row vowels" (fun (s) (.top-vowels s)) fixed)]
|
|||
|
|
rule-rows)
|
|||
|
|
[(part "hand runs" (fun (s) (.runs s)) fixed)
|
|||
|
|
(part "same-finger skips" (fun (s) (.skips s)) fixed)
|
|||
|
|
(part "two-finger runs" (fun (s) (.two-fingers s)) fixed)
|
|||
|
|
(part "pinky load" (fun (s) (.pinkies s)) fixed)
|
|||
|
|
(part "distance cost" (fun (s) (* (record-ref (.params m) distance) (.distance s))) fixed)
|
|||
|
|
(part "total" (fun (s) (.total s)) fixed3)
|
|||
|
|
(part "finger distance (key widths)" (fun (s) (.distance s)) fixed)
|
|||
|
|
(part "home row" (fun (s) (.home s)) pct)
|
|||
|
|
(part "hand alternation" (fun (s) (.alternation s)) pct)
|
|||
|
|
(part "same finger" (fun (s) (.same-finger s)) pct)
|
|||
|
|
(part "triples in hand runs" (fun (s) (.run-share s)) pct)
|
|||
|
|
(part "same-finger skip triples" (fun (s) (.skip-share s)) pct)
|
|||
|
|
(part "two-finger run triples" (fun (s) (.two-finger-share s)) pct)
|
|||
|
|
(part "left hand" (fun (s) (.left-hand s)) pct)
|
|||
|
|
(part "left pinky" (fun (s) (.left-pinky s)) pct)
|
|||
|
|
(part "right pinky" (fun (s) (.right-pinky s)) pct)]))
|
|||
|
|
(scroll
|
|||
|
|
(column #:spacing 10.0 #:margin 8.0
|
|||
|
|
heading
|
|||
|
|
(save-layout-view m)
|
|||
|
|
(board-view (labels-of l))
|
|||
|
|
(table (string-join (vec-append ["Auto"] (vec-map (fun (_) "70") (vec-ref rows 0))) ",") rows))))
|
|||
|
|
(column #:spacing 10.0 #:margin 8.0 heading (save-layout-view m) (board-view (labels-of l)))))
|
|||
|
|
|
|||
|
|
(: (-> Model (View Msg)))
|
|||
|
|
(defun (search-view m)
|
|||
|
|
(def o (.options m))
|
|||
|
|
(def l (vec-ref (.layouts m) (.chosen m)))
|
|||
|
|
(def results
|
|||
|
|
(loop (:for f (.results m)) (:for i (range 0 (vec-length (.results m))))
|
|||
|
|
(:acc out (vecing (keyed (str "result " (displayed i))
|
|||
|
|
(row #:spacing 10.0
|
|||
|
|
(text (fixed3 (.score f)) #:width 50.0)
|
|||
|
|
(board-view (labels-of (make-layout "" (vec-ref (.rows f) 0) (vec-ref (.rows f) 1)
|
|||
|
|
(vec-ref (.rows f) 2))))
|
|||
|
|
(column #:spacing 6.0
|
|||
|
|
(tooltip (button "Keep" (Keep i) #:width 70.0)
|
|||
|
|
"Adds the result to the list on the left as \"found N\". It is not saved.")
|
|||
|
|
(tooltip (button "Save" (SaveResult i) #:width 70.0)
|
|||
|
|
(str "Adds the result to the list and saves it in " layouts-file
|
|||
|
|
" under the name in Save as."))
|
|||
|
|
(tooltip (button "Copy" (Copy i) #:width 70.0)
|
|||
|
|
"Puts the result on the clipboard as a make-layout form for src/layouts.bjo."))))))))
|
|||
|
|
(scroll
|
|||
|
|
(column #:spacing 10.0 #:margin 8.0
|
|||
|
|
(text (str "From " (record-ref l name) ", the layout chosen on the left, with the numbers as they are.")
|
|||
|
|
#:wrap #t)
|
|||
|
|
(grid #:columns "Auto,160,Auto" #:rows "Auto,Auto,Auto,Auto,Auto" #:spacing 6.0
|
|||
|
|
(at 0 0 (text "Runs, in parallel"))
|
|||
|
|
(at 0 1 (number-input (cast double (.runs o)) SetRuns #:minimum 1.0 #:maximum 64.0 #:width 160.0))
|
|||
|
|
(at 0 2 (info "How many searches run at the same time. Each starts from the chosen layout with its own random swaps. More runs find good layouts more reliably."))
|
|||
|
|
(at 1 0 (text "Swaps per run"))
|
|||
|
|
(at 1 1 (number-input (cast double (.steps o)) SetSteps #:minimum 1000.0 #:maximum 20000000.0
|
|||
|
|
#:step 100000.0 #:width 160.0))
|
|||
|
|
(at 1 2 (info "How many key swaps each run tries. More swaps take longer and search more thoroughly."))
|
|||
|
|
(at 2 0 (text "Start temperature"))
|
|||
|
|
(at 2 1 (number-input (.t-start o) SetTStart #:minimum 0.002 #:maximum 5.0 #:step 0.01 #:width 160.0))
|
|||
|
|
(at 2 2 (info "How readily a run accepts a swap that makes the score worse, at the start. It falls to almost 0 at the end. High (0.3 or more): the search goes far from the start layout. Low (0.02): it refines the start layout."))
|
|||
|
|
(at 3 0 (text "Fixed characters"))
|
|||
|
|
(at 3 1 (text-input (.fixed o) SetFixed #:placeholder "åäö.," #:width 160.0))
|
|||
|
|
(at 3 2 (info "Characters whose keys do not move, written as in a layout, such as åäö.,"))
|
|||
|
|
(at 4 0 (checkbox "Vowels on the left hand" (.vowels-left o) SetVowels) #:column-span 2)
|
|||
|
|
(at 4 2 (info "Keeps every vowel (a e i o u y å ä ö) on the left hand. The chosen layout must have all its vowels on the left.")))
|
|||
|
|
(row #:spacing 6.0
|
|||
|
|
(text "Save as")
|
|||
|
|
(text-input (.layout-name m) SetLayoutName #:placeholder "name" #:width 160.0)
|
|||
|
|
(info "The name that Save on a result saves it under."))
|
|||
|
|
(button (if (.searching m) "Searching…" "Search") Search #:enabled (not (.searching m)) #:width 120.0)
|
|||
|
|
(apply column #:spacing 10.0 (vec->array results)))))
|
|||
|
|
|
|||
|
|
(: view (-> Model (View Msg)))
|
|||
|
|
(defun (view m)
|
|||
|
|
(dock
|
|||
|
|
(at-top (row #:spacing 8.0 #:margin 8.0
|
|||
|
|
(tooltip (button "Save" Save)
|
|||
|
|
(str "Writes the numbers to " params-file ". keyscore and optimize read that file."))
|
|||
|
|
(tooltip (button "Reload" Reload) (str "Reads the numbers from " params-file " again."))
|
|||
|
|
(tooltip (button "Defaults" Defaults) "Puts back the built-in numbers. Nothing is saved.")
|
|||
|
|
(text (.status m) #:margin 6.0)))
|
|||
|
|
(at-left (scores-view m))
|
|||
|
|
(tabs (.tab m) Tab #:margin 8.0
|
|||
|
|
(tab "Rules" (rules-view m))
|
|||
|
|
(tab "Key costs" (key-costs-view m))
|
|||
|
|
(tab "Layout" (layout-view m))
|
|||
|
|
(tab "Search" (search-view m)))))
|
|||
|
|
|
|||
|
|
(defun (main)
|
|||
|
|
(run-app initial gui-update view
|
|||
|
|
#:subscriptions subscriptions
|
|||
|
|
#:title "keyscore" #:width 1500.0 #:height 900.0)
|
|||
|
|
0)
|