-
Notifications
You must be signed in to change notification settings - Fork 0
/
spaced.sh
executable file
·446 lines (372 loc) · 11.6 KB
/
spaced.sh
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#!/bin/bash
set -e
# This should be set outside of this script
# SM2DIR="$HOME/code/sm-2-cli"
CFG="$SM2DIR/settings.json"
TMPFILE="$SM2DIR/settings.tmp"
CARDS=[]
USING_DEBUG_DATE=0
# TODO:
# - catch kill signal and clean up
# - also clean up on setup
# if date string passed in:
if [[ $# -gt 0 ]]
then
# Got an argument
USING_DEBUG_DATE=1
echo "------DEBUG-----"
echo "Date: $1"
echo "------DEBUG-----"
# Use the first argument as the debug 'last_run' date
jq --arg LAST "$(date -d "$1" +%s)" '.debug_last_run = ($LAST | tonumber | todate)' $CFG > $TMPFILE
mv $TMPFILE $CFG
fi
setup () {
clear -x
# Loop through all cards in /cards directory, add any that we haven't seen yet
# to the settings.
shopt -s nullglob
for f in $SM2DIR/cards/*
do
filename=$(basename $f)
if [[ `jq --arg FILENAME "$filename" 'any(.files[].filename; .== $FILENAME)' $CFG` = true ]]
then
continue
# Already in the config file, do nothing
else
# Add this entry to the list with default settings
jq --arg FILENAME "$filename" '.files += [{"filename": $FILENAME, "n": 0, "EF": 2.5, "I": 0, "last_seen":"1921-06-28T00:00:00Z"}]' $CFG > $TMPFILE
mv $TMPFILE $CFG
echo "$filename added to config"
fi
done
#let DAYS_SINCE_RUN=($(date +%s -d -)-$(date +%s -d "2021-06-27"))/86400
# No. See below:
# use jq for dates, since its apparently easier than mixing with UNIX's 'date'
jq '.today = (now | todate)' $CFG > $TMPFILE
mv $TMPFILE $CFG
#### Welcome
printf "\n###########\n"
printf "\n### Welcome to spaced-repetition active recall with SM-2\n"
if [[ USING_DEBUG_DATE -eq 1 ]]
then
DAYS_SINCE_RUN=`jq '((.today | fromdate) - (.debug_last_run | fromdate))/86400 | floor' $CFG`
printf "\nDays since last review: $DAYS_SINCE_RUN"
printf "\n(Last run on $(cat $CFG | jq '.debug_last_run | fromdate | strftime("%Y-%m-%d")'))"
else
DAYS_SINCE_RUN=`jq '((.today | fromdate) - (.last_run | fromdate))/86400 | floor' $CFG`
printf "\nDays since last review: $DAYS_SINCE_RUN"
printf "\n(Last run on $(cat $CFG | jq '.last_run | fromdate | strftime("%Y-%m-%d")'))"
fi
# TODO: Remove any cards from settings.json that _are not_ in /cards
# Or are broken symlinks. (and report these)
}
i_less_than_x_days_ago () {
AGE=$1
# find cards with I <= AGE in days
CARDS=`jq -r --arg AGE "$AGE" '.files[] | select(.I <= ($AGE | tonumber)).filename' $CFG`
}
update_last_seen () {
FILE=$1
jq --arg FILENAME "$FILE" '(.files[] | select(.filename == $FILENAME)) |= . + { last_seen: (now | todate) }' $CFG > $TMPFILE
mv $TMPFILE $CFG
}
update_q_only () {
FILE=$1
q=$2
jq --arg FILENAME "$FILE" --arg Q "$q" '(.files[] | select(.filename == $FILENAME)) |= . + { last_q: ($Q | tonumber) }' $CFG > $TMPFILE
mv $TMPFILE $CFG
}
update_sm2 () {
FILE=$1
q=$2
n=$(jq --arg FILENAME "$FILE" '.files[] | select(.filename == $FILENAME).n' $CFG)
EF=$(jq --arg FILENAME "$FILE" '.files[] | select(.filename == $FILENAME).EF' $CFG)
I=$(jq --arg FILENAME "$FILE" '.files[] | select(.filename == $FILENAME).I' $CFG)
# SM-2 algorithm
if [[ $q -ge 3 ]]
then
if [[ $n -eq 0 ]]
then
NEW_I=1
elif [[ $n -eq 1 ]]
then
NEW_I=6
else
NEW_I=$(echo - | awk -v I="$I" -v EF="$EF" '{print (I * EF);}')
fi
# Determine if new EF has to be forced to 1.3
NEW_EF=$(echo - | awk -v q="$q" -v EF="$EF" '{print (EF + (0.1 - (5 - q) * (0.08 + (5 - q) * 0.02)));}')
if awk -v EF="$NEW_EF" 'BEGIN {exit !(EF < 1.3)}'; then
NEW_EF=1.3
else
NEW_EF=$NEW_EF
fi
let NEW_N=($n+1)
else
NEW_N=0
NEW_I=1
NEW_EF=$EF
fi
# Set new parameters
jq --arg FILENAME "$FILE" --arg NEW_N "$NEW_N" --arg NEW_EF "$NEW_EF" --arg NEW_I "$NEW_I" --arg Q "$q" '(.files[] | select(.filename == $FILENAME)) |= . + { n: ($NEW_N | tonumber), EF: ($NEW_EF | tonumber), I: ($NEW_I | tonumber), last_q: ($Q | tonumber) }' $CFG > $TMPFILE
mv $TMPFILE $CFG
}
run_cards() {
# Fill up CARDS array:
i_less_than_x_days_ago "$DAYS_SINCE_RUN"
# Count today's cards for display
FOR_TODAY=`jq -r --arg AGE "$DAYS_SINCE_RUN" '[ .files[] | select(.I <= ($AGE | tonumber)) ] | length' $CFG`
printf "\n"
printf "\n You have $TOTAL_CARDS cards in your collection.\n"
if [[ FOR_TODAY -eq 0 ]]
then
printf "\n ...But none to show today.\n"
printf " Try again tomorrow!\n"
printf "\n"
exit 0
else
printf "\n We'll show you $FOR_TODAY of them today\n"
printf "\n"
printf "\nPress any key to start.\n"
read -n 1 advance
fi
for FILE in $CARDS
do
clear -x
ask_card $FILE
n=$(jq --arg FILENAME "$FILE" '.files[] | select(.filename == $FILENAME).n' $CFG)
I=$(jq --arg FILENAME "$FILE" '.files[] | select(.filename == $FILENAME).I' $CFG)
clear -x
printf "\n###########\n"
printf "### That was: '$FILE'\n"
printf "\n You've recalled this card $n times in a row."
printf "\n We will show it to you again in $I days."
printf "\n\nPress any key for the next card.\n"
read -n 1 advance
done # CARDS for loop
clear -x
printf "\nYou've completed the main review\n"
printf "Press any key to continue\n"
read -n 1 continue
}
ask_card () { # $1 FILE, $2 RE_REVIEW
FILE=$1
RE_REVIEW=$2
while true; do # y/n loop
# Check if card has a divider - if so it's a flash card
if grep -qE '([-]){3,}' $SM2DIR/cards/$FILE; then
printf "\n###########\n"
printf "\n### This is a flash card:\n"
printf "\n"
printf "\n"
# show only the top half first:
sed -E '/([-]){3,}/Q' $SM2DIR/cards/$FILE
printf "\n"
printf "\n###########\n"
printf "### Press any key to reveal the whole card.\n"
read -n 1 advance
printf "\n"
# show bottom half only
# sed -E '1,/([-]){3,}/ d' $SM2DIR/cards/$FILE
clear -x
else
printf "\n### This is not flash card:\n"
printf "\n"
fi
printf "\n"
cat "$SM2DIR/cards/$FILE"
printf "\n"
printf "\n"
printf "\n###########\n"
printf "### That was: '$FILE'\n"
printf "\n- Press y if you recalled it correctly\n"
printf "\n- Press n if you if you did not\n"
printf "\n- (Press q to quit at any time)\n"
printf "\n> "
read -n 1 decision
case $decision in
[yY])
clear -x
printf "\n###########\n"
printf "### That was: '$FILE'\n"
printf "\n Great, you recalled it.\n"
printf "\n"
printf "\nHow easy was it? (1-3)?\n"
printf "\n1: Significant difficulty recalling.\n"
printf "\n2: Some hesitation.\n"
printf "\n3: Perfect recall.\n"
printf "\n> "
while true; do # 1-3 loop
read -n 1 q
case $q in
[123])
let q=($q+2)
if [[ $RE_REVIEW -eq 1 ]]
then
update_q_only $FILE $q
else
update_sm2 $FILE $q
update_last_seen $FILE
fi
break # 1-3 loop break
;;
*)
echo 'Please choose 1-3' >&2
;;
esac
done # 1-3 loop
break # y/n loop break
;;
[nN])
echo "Sub 4: $SUB_4_CARDS"
# TODO: add to sub-4 array
SUB_4_CARDS+=($FILE)
echo "Sub 4: $SUB_4_CARDS"
clear -x
printf "\n###########\n"
printf "### That was: '$FILE'\n"
printf "\n You did not recall it.\n"
printf "\n"
printf "\nHow did it feel? (1-3)?\n"
printf "\n1: Total blackout/failure to recall.\n"
printf "\n2: Felt familiar after seeing answer.\n"
printf "\n3: Seemed easy to remember after seeing answer.\n"
printf "\n> "
while true; do # 1-3 loop
read -n 1 q
case $q in
[123])
# Seems to be an issue with subtracting 1 from 1....
if [[ "$q" -eq 1 ]]
then
q=0
else
let q=($q-1)
fi
if [[ $RE_REVIEW -eq 1 ]]
then
update_q_only $FILE $q
else
update_sm2 $FILE $q
update_last_seen $FILE
fi
break # 1-3 loop break
;;
*)
echo 'Please choose 1-3' >&2
;;
esac
done # 1-3 loop
break # y/n loop break
;;
q*)
printf "\nCleaning up and quitting...\n"
# TODO
break # y/n/q loop break
;;
*)
echo 'Invalid input' >&2
;;
esac
done
}
re_run_sub_fours () {
# If any cards have their last_q as < 4,
# re-ask them until they're all >= 4
clear -x
SUB_4_COUNT=`jq -r '[ .files[] | select(.last_q < (4 | tonumber)) ] | length' $CFG`
printf "\n###########\n"
printf "### Now you'll review $SUB_4_COUNT cards that were\n"
printf " giving you a hard time this session.\n"
printf " You'll see them repeatedly until your recall is good.\n"
printf "\n"
printf "\nPress any key to continue\n"
read -n 1 continue
# SUB_4_Qs=`jq -r '[ .files[] | select(.last_q < (4 | tonumber)) ] | length' $CFG`
while [[ `jq -r '[ .files[] | select(.last_q < (4 | tonumber)) ] | length' $CFG` -gt 0 ]]; do
SUB_4=`jq -r --arg AGE "$AGE" '.files[] | select(.last_q < (4 | tonumber)).filename' $CFG`
for FILE in $SUB_4
do
clear -x
SUB_4_COUNT=`jq -r '[ .files[] | select(.last_q < (4 | tonumber)) ] | length' $CFG`
printf "\n($SUB_4_COUNT review cards left) \n"
ask_card $FILE 1
done
done
clear -x
printf "\n###########\n"
printf "### Reinforcement complete!\n"
printf "\n"
printf "\nPress any key to continue\n"
read -n 1 continue
}
exit_if_no_cards () {
TOTAL_CARDS=`cat $CFG | jq '.files | length'`
if [[ "$TOTAL_CARDS" -lt 1 ]]; then
printf "\n\nWe have no cards. Add or symlink to /cards\n"
exit 1
fi
}
# 0: "Total blackout", complete failure to recall the information.
# 1: Incorrect response, but upon seeing the correct answer it felt familiar.
# 2: Incorrect response, but upon seeing the correct answer it seemed easy to remember.
# 3: Correct response, but required significant difficulty to recall.
# 4: Correct response, after some hesitation.
# 5: Correct response with perfect recall.
#algorithm SM-2 is
# input: user grade q
# repetition number n
# easiness factor EF
# interval I
# output: updated values of n, EF, and I
#
# if q ≥ 3 (correct response) then
# if n = 0 then
# I ← 1
# else if n = 1 then
# I ← 6
# else
# I ← ⌈I × EF⌉
# end if
# EF ← EF + (0.1 − (5 − q) × (0.08 + (5 − q) × 0.02))
# if EF < 1.3 then
# EF ← 1.3
# end if
# increment n
# else (incorrect response)
# n ← 0
# I ← 1
# end if
#
# return (n, EF, I)
cleanup () {
clear -x
printf "\n###########\n"
printf "\n### Cleaning up...\n"
# move 'today' to 'last_run' field
jq '.last_run = .today' $CFG > $TMPFILE
mv $TMPFILE $CFG
# remove 'today' field
jq 'del(.today)' $CFG > $TMPFILE
mv $TMPFILE $CFG
# If we were debugging, remove the debug field
if [[ USING_DEBUG_DATE -eq 1 ]]
then
jq 'del(.debug_last_run)' $CFG > $TMPFILE
mv $TMPFILE $CFG
fi
printf "\n All done.\n"
printf "\n See you tomorrow!\n"
printf "\n"
}
setup
exit_if_no_cards
run_cards
SUB_4_COUNT=`jq -r '[ .files[] | select(.last_q < (4 | tonumber)) ] | length' $CFG`
if [[ $SUB_4_COUNT -gt 0 ]]
then
re_run_sub_fours
fi
cleanup
exit 0