-
Notifications
You must be signed in to change notification settings - Fork 32
/
org-fc-algo-sm2.el
319 lines (276 loc) · 11 KB
/
org-fc-algo-sm2.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
;;; org-fc-algo-sm2.el --- Variation of SM2 spacing algorithm -*- lexical-binding: t; -*-
;; Copyright (C) 2020-2024 Leon Rische
;; Author: Leon Rische <emacs@leonrische.me>
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;
;;
;;; Code:
(require 'cl-lib)
(require 'eieio)
(require 'eieio-base)
(require 'org-fc-core)
(require 'org-fc-awk)
(require 'org-fc-dashboard)
(defmacro org-fc-property (symbol standard doc &rest args)
(let (defcustom-args property)
(while args
(let ((keyword (pop args)))
(unless (symbolp keyword)
(error "Junk in args %S" args))
(unless args
(error "Keyword %s is missing an argument" keyword))
(let ((value (pop args)))
(cl-case keyword
(:property (setq property value))
(t
(push value defcustom-args)
(push keyword defcustom-args))))))
(unless property
(error "Missing keyword :property"))
(let ((property-symbol (intern (concat (symbol-name symbol) "-property"))))
`(progn
(defcustom
,symbol
,standard
,doc
,@defcustom-args)
(defcustom
,property-symbol
,property
,(format "Headline property for `%s'" symbol)
:type 'string
:group ,(plist-get defcustom-args :group))
(defun ,symbol ()
,(format "Getter for `%s'" symbol)
(if-let ((value (org-entry-get (point) ,property-symbol t)))
;; TODO: Switch on possible types
(read value)
;; ,(case (plist-get defcustom-args :type)
;; ('string 'value)
;; ('float '(string-to-number value))
;; ('list '(read value))
;; (t (error "Unsupported property type %s"
;; (plist-get defcustom-args :type)
,symbol))))))
;;;; Properties
(org-fc-property org-fc-algo-sm2-ease-min 1.3
"Lower bound for a cards ease."
:type 'float
:group 'org-fc
:property "FC_SM2_EASE_MIN")
(org-fc-property org-fc-algo-sm2-ease-max 5.0
"Upper bound for a cards ease."
:type 'float
:group 'org-fc
:property "FC_SM2_EASE_MAX")
(org-fc-property org-fc-algo-sm2-ease-initial 2.5
"Initial ease."
:type 'float
:group 'org-fc
:property "FC_SM2_EASE_INITIAL")
(org-fc-property org-fc-algo-sm2-fuzz-min 0.9
"Lower bound for random interval fuzz factor."
:type 'float
:group 'org-fc
:property "FC_SM2_FUZZ_MIN")
(org-fc-property org-fc-algo-sm2-fuzz-max 1.1
"Upper bound for random interval fuzz factor."
:type 'float
:group 'org-fc
:property "FC_SM2_FUZZ_MAX")
(org-fc-property org-fc-algo-sm2-changes
'((again . -0.3)
(hard . -0.15)
(good . 0.0)
(easy . 0.15))
"Changes to a cards ease depending on its rating."
:type 'list
:group 'org-fc
:property "FC_SM2_CHANGES")
(org-fc-property org-fc-algo-sm2-intervals
'(0.0 0.01 1.0 6.0)
"Hard-coded intervals for the first few card boxes.
Values are in days."
:type 'list
:group 'org-fc
:property "FC_SM2_INTERVALS")
;;;; Helper Functions
(defun org-fc-algo-sm2-fuzz (interval)
"Apply fuzz to INTERVAL.
INTERVAL is by a random factor between `org-fc-algo-sm2-fuzz-min' and
`org-fc-algo-sm2-fuzz-max'"
(let ((min (org-fc-algo-sm2-fuzz-min))
(max (org-fc-algo-sm2-fuzz-max)))
(* interval (+ min (cl-random (- max min))))))
;;;; Main Algorithm Interface
(defclass org-fc-algo-sm2 (eieio-singleton org-fc-algo) ())
(cl-defmethod org-fc-algo-headers ((_algo org-fc-algo-sm2))
'(position ease box interval due))
(cl-defmethod org-fc-algo-initial-review-data ((_algo org-fc-algo-sm2) name)
"Initial SM2 review data for position NAME."
(list
'position name
'ease (format "%.2f" (org-fc-algo-sm2-ease-initial))
'box (format "%d" 0)
'interval (format "%.2f" 0)
'due (org-fc-timestamp-in 0)))
(cl-defmethod org-fc-algo-next-review-data ((_algo org-fc-algo-sm2) current rating)
"Calculate the next parameters, given the CURRENT parameters and a RATING."
(let* ((ease (string-to-number (plist-get current 'ease)))
(box (string-to-number (plist-get current 'box)))
(interval (string-to-number (plist-get current 'interval)))
;; Configurable settings
(intervals (org-fc-algo-sm2-intervals))
(changes (org-fc-algo-sm2-changes))
;; New parameters
(next-ease
(if (< box 2)
ease
(min
(max
(+ ease (alist-get rating changes))
(org-fc-algo-sm2-ease-min))
(org-fc-algo-sm2-ease-max))))
(next-box
(cond
;; If a card is rated easy, skip the learning phase
((and (eq box 0) (eq rating 'easy)) 2)
;; If the review failed, go back to box 0
((eq rating 'again) 0)
;; Otherwise, move forward one box
(t (1+ box))))
(next-interval
(cond ((< next-box (length intervals))
(nth next-box intervals))
((and (eq org-fc-algorithm 'sm2-v2) (eq rating 'hard)) (* 1.2 interval))
(t (org-fc-algo-sm2-fuzz (* next-ease interval))))))
(list
'ease (format "%.2f" next-ease)
'box (format "%d" next-box)
'interval (format "%.2f" next-interval)
'due (org-fc-timestamp-in next-interval))))
(cl-defmethod org-fc-algo-log-review ((_algo org-fc-algo-sm2) (position org-fc-position) current rating delta)
(let* ((card (oref position card))
(file (oref card file))
(elements
(list
(org-fc-timestamp-in 0)
(oref file path)
(oref card id)
(oref position name)
(format (plist-get current 'ease))
(format (plist-get current 'box))
(format (plist-get current 'interval))
(symbol-name rating)
(format "%.2f" delta)
(symbol-name org-fc-algorithm))))
(append-to-file
(format "%s\n" (mapconcat #'identity elements "\t"))
nil
org-fc-review-history-file)))
;; NOTE: There's some duplication here as the position object already
;; contains the review data. Working on the review data parsed
;; directly from the file seems safer though.
(cl-defmethod org-fc-algo-update-review-data ((algo org-fc-algo-sm2) (position org-fc-position) rating delta)
"Update the review data of a POSITION.
Also add a new entry in the review history file. RATING is a
review rating and DELTA the time in seconds between showing and
rating the card."
(let* ((name (oref position name))
(review-data (org-fc-review-data-parse (org-fc-algo-headers algo)))
(current (org-fc-review-data-get-row review-data name)))
(unless current
(error "No review data row found for this position"))
(org-fc-algo-log-review algo position current rating delta)
(org-fc-review-data-update-row
review-data name
(org-fc-algo-next-review-data algo current rating))
(org-fc-review-data-write review-data)))
(cl-defmethod org-fc-algo-review-history ((_algo org-fc-algo-sm2) card-id position-name)
"Review history for a given CARD-ID and POSITION name.
Returns nil if there is no history file."
(when (file-exists-p org-fc-review-history-file)
(let ((output
(shell-command-to-string
(org-fc-awk--command
"awk/review_history_sm2.awk"
:input org-fc-review-history-file
:variables `(("filter_card_id" . ,(or card-id "any"))
("filter_position_name" . ,(or position-name "any")))))))
(if (string-prefix-p "(" output)
(read output)
(error "Org-fc shell error: %s" output)))))
(defun org-fc-algo-sm2-dashboard-parameters (cards)
(let* ((pos-count 0)
(avg-ease 0.0) (avg-box 0.0) (avg-interval 0.0))
(dolist (card cards)
(unless (or (oref card suspended)
(not (equal (eieio-object-class (oref card algo)) 'org-fc-algo-sm2)))
(dolist (pos (oref card positions))
(cl-incf pos-count 1)
(cl-incf avg-ease (plist-get (oref pos data) :ease))
(cl-incf avg-box (plist-get (oref pos data) :box))
(cl-incf avg-interval (plist-get (oref pos data) :interval)))))
(insert "\n")
(if (cl-plusp pos-count)
(progn
(insert (format " %6.2f avg. ease\n" (/ avg-ease pos-count)))
(insert (format " %6.2f avg. box\n" (/ avg-box pos-count)))
(insert (format " %6.2f avg. interval (days)\n\n" (/ avg-interval pos-count))))
(insert " No positions yet\n\n"))))
(defun org-fc-algo-sm2-review-stats ()
"Statistics for all card reviews.
Returns nil if there is no history file."
(when (file-exists-p org-fc-review-history-file)
(let ((output
(shell-command-to-string
(org-fc-awk--command
"awk/review_stats_sm2.awk"
:input org-fc-review-history-file
:variables `(("min_box" . ,org-fc-stats-review-min-box))))))
(if (string-prefix-p "(" output)
(read output)
(error "Org-fc shell error: %s" output)))))
(defun org-fc-algo-sm2-dashboard-review-history (_cards)
(let ((reviews-stats (org-fc-algo-sm2-review-stats)))
(insert "\n")
(if reviews-stats
(progn
(dolist (scope '((:day . "day")
(:week . "week")
(:month . "month")
(:all . "all")))
(when-let (stat (plist-get reviews-stats (car scope)))
(when (cl-plusp (plist-get stat :total))
(insert " ")
(if (and (display-graphic-p)
(memq 'svg (and (boundp 'image-types) image-types)))
(insert-image (org-fc-dashboard-bar-chart stat))
(insert (org-fc-dashboard-text-bar-chart stat)))
(insert (propertize (format " %s (%d)\n" (cdr scope) (plist-get stat :total)) 'face 'org-level-1)))))
(insert "\n"))
(insert " No reviews yet\n\n"))))
(org-fc-dashboard-add-section
(org-fc-dashboard-section
:title "SM2 Parameters"
:inserter #'org-fc-algo-sm2-dashboard-parameters))
(org-fc-dashboard-add-section
(org-fc-dashboard-section
:title "SM2 Review History (All Cards)"
:start-visible t
:inserter #'org-fc-algo-sm2-dashboard-review-history))
(org-fc-register-algo 'sm2 org-fc-algo-sm2)
;;; Footer
(provide 'org-fc-algo-sm2)
;;; org-fc-algo-sm2.el ends here