aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArun Isaac2022-06-18 15:25:35 +0530
committerArun Isaac2022-06-18 15:25:35 +0530
commit5edc8a43602296e30b61fbaaa09b1b27024b4190 (patch)
tree718934b69bc2a41de8c133bea24377b70d7078ad
parent817a42b6b913b162adf5cc37a8ca720bc09d6a07 (diff)
downloadthogai-5edc8a43602296e30b61fbaaa09b1b27024b4190.tar.gz
thogai-5edc8a43602296e30b61fbaaa09b1b27024b4190.tar.lz
thogai-5edc8a43602296e30b61fbaaa09b1b27024b4190.zip
Implement reverse lookup of dictionary.
* thogai.el (thogai-reverse-dictionary): New variable. (thogai-load-dictionaries): Initialize reverse dictionary. (thogai-filter-map, thogai-reverse-lookup): New functions.
-rw-r--r--thogai.el39
1 files changed, 37 insertions, 2 deletions
diff --git a/thogai.el b/thogai.el
index d9511ad..daa9372 100644
--- a/thogai.el
+++ b/thogai.el
@@ -81,6 +81,15 @@ not present in `thogai-dictionary-files'. For example,
This variable is loaded from files listed in
`thogai-dictionary-files' on running `thogai-load-dictionaries'.")
+(defvar thogai-reverse-dictionary
+ (make-hash-table :test 'equal)
+ "Reverse steno dictionary mapping translations to strokes.
+
+This variable is an exact inverse of the `thogai-dictionary' hash
+table. It is built when files listed in
+`thogai-dictionary-files' are loaded using
+`thogai-load-dictionaries'.")
+
(defvar thogai-stroke-ring-size
1000
"Size of `thogai-stroke-ring'.
@@ -125,13 +134,18 @@ and the variable is reset to nil.")
(defun thogai-load-dictionaries ()
"Load dictionary files in THOGAI-DICTIONARY-FILES."
(interactive)
- ;; Reset hash table.
+ ;; Reset forward and reverse dictionaries.
(clrhash thogai-dictionary)
+ (clrhash thogai-reverse-dictionary)
(let ((thogai-dictionary-longest-key 0))
(dolist (dictionary-file thogai-dictionary-files)
(maphash (lambda (key value)
- ;; Load key, value pair into hash table.
+ ;; Load keys and values into forward and reverse
+ ;; dictionaries.
(puthash key value thogai-dictionary)
+ (puthash value
+ (cons key (gethash value thogai-reverse-dictionary))
+ thogai-reverse-dictionary)
;; Update length of longest key.
(setq thogai-dictionary-longest-key
(max thogai-dictionary-longest-key
@@ -156,6 +170,27 @@ higher precedence. If STROKE is in neither, nil is returned."
(or (map-elt thogai-user-dictionary-alist stroke)
(map-elt thogai-dictionary stroke)))
+(defun thogai-filter-map (function sequence)
+ "Map FUNCTION over SEQUENCE and return only non-nil results.
+
+Map FUNCTION over SEQUENCE using `seq-map', but return a list of
+only the non-nil results."
+ (seq-filter 'identity
+ (seq-map function sequence)))
+
+(defun thogai-reverse-lookup (translation)
+ "Lookup TRANSLATION in reverse dictionary.
+
+A list of all strokes producing TRANSLATION is returned.
+TRANSLATION is looked up in `thogai-user-dictionary-alist' and
+`thogai-reverse-dictionary'."
+ (append
+ (thogai-filter-map (pcase-lambda (`(,key . ,value))
+ (and (equal value translation)
+ key))
+ thogai-user-dictionary-alist)
+ (gethash translation thogai-reverse-dictionary)))
+
(defun thogai-longest-match (state)
"Return the longest sublist of STATE that maps to a stroke.