org-learn.el (6080B)
1 ;;; org-learn.el --- Implements SuperMemo's incremental learning algorithm 2 3 ;; Copyright (C) 2009-2021 Free Software Foundation, Inc. 4 5 ;; Author: John Wiegley <johnw at gnu dot org> 6 ;; Keywords: outlines, hypermedia, calendar, wp 7 ;; Homepage: https://git.sr.ht/~bzg/org-contrib 8 ;; Version: 6.32trans 9 ;; 10 ;; This file is not part of GNU Emacs. 11 ;; 12 ;; This program is free software: you can redistribute it and/or modify 13 ;; it under the terms of the GNU General Public License as published by 14 ;; the Free Software Foundation, either version 3 of the License, or 15 ;; (at your option) any later version. 16 17 ;; This program is distributed in the hope that it will be useful, 18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 ;; GNU General Public License for more details. 21 22 ;; You should have received a copy of the GNU General Public License 23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. 24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 25 ;; 26 ;;; Commentary: 27 28 ;; The file implements the learning algorithm described at 29 ;; https://supermemo.com/english/ol/sm5.htm, which is a system for reading 30 ;; material according to "spaced repetition". See 31 ;; https://en.wikipedia.org/wiki/Spaced_repetition for more details. 32 ;; 33 ;; To use, turn on state logging and schedule some piece of information you 34 ;; want to read. Then in the agenda buffer type 35 36 (require 'org) 37 (eval-when-compile 38 (require 'cl)) 39 40 (defgroup org-learn nil 41 "Options concerning the learning code in Org-mode." 42 :tag "Org Learn" 43 :group 'org-progress) 44 45 (defcustom org-learn-always-reschedule nil 46 "If non-nil, always reschedule items, even if retention was \"perfect\"." 47 :type 'boolean 48 :group 'org-learn) 49 50 (defcustom org-learn-fraction 0.5 51 "Controls the rate at which EF is increased or decreased. 52 Must be a number between 0 and 1 (the greater it is the faster 53 the changes of the OF matrix)." 54 :type 'float 55 :group 'org-learn) 56 57 (defun initial-optimal-factor (n ef) 58 (if (= 1 n) 59 4 60 ef)) 61 62 (defun get-optimal-factor (n ef of-matrix) 63 (let ((factors (assoc n of-matrix))) 64 (or (and factors 65 (let ((ef-of (assoc ef (cdr factors)))) 66 (and ef-of (cdr ef-of)))) 67 (initial-optimal-factor n ef)))) 68 69 (defun set-optimal-factor (n ef of-matrix of) 70 (let ((factors (assoc n of-matrix))) 71 (if factors 72 (let ((ef-of (assoc ef (cdr factors)))) 73 (if ef-of 74 (setcdr ef-of of) 75 (push (cons ef of) (cdr factors)))) 76 (push (cons n (list (cons ef of))) of-matrix))) 77 of-matrix) 78 79 (defun inter-repetition-interval (n ef &optional of-matrix) 80 (let ((of (get-optimal-factor n ef of-matrix))) 81 (if (= 1 n) 82 of 83 (* of (inter-repetition-interval (1- n) ef of-matrix))))) 84 85 (defun modify-e-factor (ef quality) 86 (if (< ef 1.3) 87 1.3 88 (+ ef (- 0.1 (* (- 5 quality) (+ 0.08 (* (- 5 quality) 0.02))))))) 89 90 (defun modify-of (of q fraction) 91 (let ((temp (* of (+ 0.72 (* q 0.07))))) 92 (+ (* (- 1 fraction) of) (* fraction temp)))) 93 94 (defun calculate-new-optimal-factor (interval-used quality used-of 95 old-of fraction) 96 "This implements the SM-5 learning algorithm in Lisp. 97 INTERVAL-USED is the last interval used for the item in question. 98 QUALITY is the quality of the repetition response. 99 USED-OF is the optimal factor used in calculation of the last 100 interval used for the item in question. 101 OLD-OF is the previous value of the OF entry corresponding to the 102 relevant repetition number and the E-Factor of the item. 103 FRACTION is a number belonging to the range (0,1) determining the 104 rate of modifications (the greater it is the faster the changes 105 of the OF matrix). 106 107 Returns the newly calculated value of the considered entry of the 108 OF matrix." 109 (let (;; the value proposed for the modifier in case of q=5 110 (mod5 (/ (1+ interval-used) interval-used)) 111 ;; the value proposed for the modifier in case of q=2 112 (mod2 (/ (1- interval-used) interval-used)) 113 ;; the number determining how many times the OF value will 114 ;; increase or decrease 115 modifier) 116 (if (< mod5 1.05) 117 (setq mod5 1.05)) 118 (if (< mod2 0.75) 119 (setq mod5 0.75)) 120 (if (> quality 4) 121 (setq modifier (1+ (* (- mod5 1) (- quality 4)))) 122 (setq modifier (- 1 (* (/ (- 1 mod2) 2) (- 4 quality))))) 123 (if (< modifier 0.05) 124 (setq modifier 0.05)) 125 (setq new-of (* used-of modifier)) 126 (if (> quality 4) 127 (if (< new-of old-of) 128 (setq new-of old-of))) 129 (if (< quality 4) 130 (if (> new-of old-of) 131 (setq new-of old-of))) 132 (setq new-of (+ (* new-of fraction) (* old-of (- 1 fraction)))) 133 (if (< new-of 1.2) 134 (setq new-of 1.2) 135 new-of))) 136 137 (defvar initial-repetition-state '(-1 1 2.5 nil)) 138 139 (defun determine-next-interval (n ef quality of-matrix) 140 (assert (> n 0)) 141 (assert (and (>= quality 0) (<= quality 5))) 142 (if (< quality 3) 143 (list (inter-repetition-interval n ef) (1+ n) ef nil) 144 (let ((next-ef (modify-e-factor ef quality))) 145 (setq of-matrix 146 (set-optimal-factor n next-ef of-matrix 147 (modify-of (get-optimal-factor n ef of-matrix) 148 quality org-learn-fraction)) 149 ef next-ef) 150 ;; For a zero-based quality of 4 or 5, don't repeat 151 (if (and (>= quality 4) 152 (not org-learn-always-reschedule)) 153 (list 0 (1+ n) ef of-matrix) 154 (list (inter-repetition-interval n ef of-matrix) (1+ n) 155 ef of-matrix))))) 156 157 (defun org-smart-reschedule (quality) 158 (interactive "nHow well did you remember the information (on a scale of 0-5)? ") 159 (let* ((learn-str (org-entry-get (point) "LEARN_DATA")) 160 (learn-data (or (and learn-str 161 (read learn-str)) 162 (copy-list initial-repetition-state))) 163 closed-dates) 164 (setq learn-data 165 (determine-next-interval (nth 1 learn-data) 166 (nth 2 learn-data) 167 quality 168 (nth 3 learn-data))) 169 (org-entry-put (point) "LEARN_DATA" (prin1-to-string learn-data)) 170 (if (= 0 (nth 0 learn-data)) 171 (org-schedule t) 172 (org-schedule nil (time-add (current-time) 173 (days-to-time (nth 0 learn-data))))))) 174 175 (provide 'org-learn) 176 177 ;;; org-learn.el ends here