From 43ebca1233c040a73dfdc5d048eb71c912883e10 Mon Sep 17 00:00:00 2001 From: Thanos Apollo Date: Wed, 27 Dec 2023 10:03:26 +0200 Subject: Rewrite gnsois-cloze-extract-cards --- gnosis.el | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/gnosis.el b/gnosis.el index 7bfb9cc..0ef0cef 100644 --- a/gnosis.el +++ b/gnosis.el @@ -248,22 +248,6 @@ choice in the `CHOICES' list. Each note must correspond to one `DECK'. (history-add-new-input nil)) ;; Disable history (completing-read "Answer: " choices))) -(defun gnosis-cloze-extract-answers (string) - "Extract items from a STRING, and group them based on the cx tag." - (let ((res '()) - (pos 0)) - (while (string-match "{\\{1,2\\}\\(c[0-9]+\\)::\\([^}]*\\)}\\{1,2\\}" string pos) - (let* ((tag (match-string 1 string)) - (text (match-string 2 string)) - (pair (assoc tag res))) - (if pair - (setcdr pair (append (cdr pair) (list text))) - (push (list tag text) res))) - (setf pos (match-end 0))) - ;; Reverse the final result and each sublist to maintain original order - ;; As our push/assoc approach prepends elements, not appends them - ;; (mapcar (lambda (x) (cons (car x) (cdr x))) (nreverse res)) ;; check for cl-rest - (mapcar (lambda (x) (cdr x)) (nreverse res)))) (defun gnosis-cloze-replace-words (string words new) "In STRING replace WORDS with NEW." (cl-assert (listp words)) @@ -272,6 +256,29 @@ choice in the `CHOICES' list. Each note must correspond to one `DECK'. do (setf string (replace-regexp-in-string (concat "\\<" word "\\>") ;; use word boundary indentifiers new string))) string) + +(defun gnosis-cloze-extract-answers (str) + "Extract cloze answers for STR. + +Return a list of cloze answers for STR, organized by cX-tag. + +Valid cloze formats include: +\"This is an {c1:example}\" +\"This is an {c1::example}\" +\"This is an {{c1:example}}\" +\"This is an {{c1::example}}\"" + (let ((result-alist '()) + (start 0)) + (while (string-match "{\\{1,2\\}c\\([0-9]+\\)::?\\(.*?\\)}\\{1,2\\}" str start) + (let* ((tag (match-string 1 str)) + (content (match-string 2 str))) + (if (assoc tag result-alist) + (push content (cdr (assoc tag result-alist))) + (push (cons tag (list content)) result-alist)) + (setf start (match-end 0)))) + (mapcar (lambda (tag-group) (nreverse (cdr tag-group))) + (nreverse result-alist)))) + ;; Review ;;;;;;;;;; (defun gnosis-review--algorithm (id success) -- cgit v1.2.3