-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
fsrs4anki_scheduler_qt5.js
382 lines (374 loc) · 20.1 KB
/
fsrs4anki_scheduler_qt5.js
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
// FSRS4Anki v5.2.0 Scheduler Qt5
set_version();
// The latest version will be released on https://github.com/open-spaced-repetition/fsrs4anki/releases/latest
// Configuration Start
const deckParams = [
{
// Default parameters of FSRS4Anki for global
"deckName": "global config for FSRS4Anki",
"w": [0.40255, 1.18385, 3.173, 15.69105, 7.1949, 0.5345, 1.4604, 0.0046, 1.54575, 0.1192, 1.01925,
1.9395, 0.11, 0.29605, 2.2698, 0.2315, 2.9898, 0.51655, 0.6621],
// The above parameters can be optimized via FSRS4Anki optimizer.
// For details about the parameters, please see: https://github.com/open-spaced-repetition/fsrs4anki/wiki/The-Algorithm
// User's custom parameters for global
"requestRetention": 0.9, // recommended setting: 0.75 ~ 0.95
"maximumInterval": 36500,
// FSRS only modifies the long-term scheduling. So (re)learning steps in deck options work as usual.
// I recommend setting steps shorter than 1 day.
},
{
// Example 1: User's custom parameters for this deck and its sub-decks.
"deckName": "MainDeck1",
"w": [0.40255, 1.18385, 3.173, 15.69105, 7.1949, 0.5345, 1.4604, 0.0046, 1.54575, 0.1192, 1.01925,
1.9395, 0.11, 0.29605, 2.2698, 0.2315, 2.9898, 0.51655, 0.6621],
"requestRetention": 0.9,
"maximumInterval": 36500,
},
{
// Example 2: User's custom parameters for this deck and its sub-decks.
// Don't omit any keys.
"deckName": "MainDeck2::SubDeck::SubSubDeck",
"w": [0.40255, 1.18385, 3.173, 15.69105, 7.1949, 0.5345, 1.4604, 0.0046, 1.54575, 0.1192, 1.01925,
1.9395, 0.11, 0.29605, 2.2698, 0.2315, 2.9898, 0.51655, 0.6621],
"requestRetention": 0.9,
"maximumInterval": 36500,
}
];
// To turn off FSRS in specific decks, fill them into the skip_decks list below.
// Please don't remove it even if you don't need it.
const skip_decks = ["MainDeck3", "MainDeck4::SubDeck"];
// "Fuzz" is a small random delay applied to new intervals to prevent cards from
// sticking together and always coming up for review on the same day
const enable_fuzz = true;
// FSRS supports displaying memory states of cards.
// Enable it for debugging if you encounter something wrong.
const display_memory_state = false;
// Configuration End
debugger;
// display if FSRS is enabled
if (display_memory_state) {
const prev = document.getElementById('FSRS_status');
if (prev) {
prev.remove();
}
var fsrs_status = document.createElement('span');
fsrs_status.innerHTML = "<br>FSRS enabled";
fsrs_status.id = "FSRS_status";
fsrs_status.style.cssText = "font-size:12px;opacity:0.5;font-family:monospace;text-align:left;line-height:1em;";
document.body.appendChild(fsrs_status);
document.getElementById("qa").style.cssText += "min-height:50vh;";
}
let params = {};
// get the name of the card's deck
if (deck_name = get_deckname()) {
if (display_memory_state) {
fsrs_status.innerHTML += "<br>Deck name: " + deck_name;
}
for (const i of skip_decks) {
if (deck_name.startsWith(i)) {
fsrs_status.innerHTML = fsrs_status.innerHTML.replace("FSRS enabled", "FSRS disabled");
return;
}
}
// Arrange the deckParams of sub-decks in front of their parent decks.
deckParams.sort(function (a, b) {
return -a.deckName.localeCompare(b.deckName);
});
for (let i = 0; i < deckParams.length; i++) {
if (deck_name.startsWith(deckParams[i]["deckName"])) {
params = deckParams[i];
break;
}
}
} else {
if (display_memory_state) {
fsrs_status.innerHTML += "<br>Deck name not found";
}
}
if (Object.keys(params).length === 0) {
params = deckParams.find(deck => deck.deckName === "global config for FSRS4Anki");
}
const w = params["w"];
const requestRetention = params["requestRetention"];
const maximumInterval = params["maximumInterval"];
const DECAY = -0.5;
const FACTOR = 0.9 ** (1 / DECAY) - 1;
// global fuzz factor for all ratings.
const fuzz_factor = set_fuzz_factor();
const ratings = {
"again": 1,
"hard": 2,
"good": 3,
"easy": 4
};
// For new cards
if (is_new()) {
var _states$good$normal, _states$easy$normal;
init_states();
const good_interval = next_interval(customData.good.s);
const easy_interval = Math.max(next_interval(customData.easy.s), good_interval + 1);
if ((_states$good$normal = states.good.normal) !== null && _states$good$normal !== void 0 && _states$good$normal.review) {
states.good.normal.review.scheduledDays = good_interval;
}
if ((_states$easy$normal = states.easy.normal) !== null && _states$easy$normal !== void 0 && _states$easy$normal.review) {
states.easy.normal.review.scheduledDays = easy_interval;
}
// For learning/relearning cards
} else if (is_learning()) {
var _states$good$normal2, _states$easy$normal2;
// Init states if the card didn't contain customData
if (is_empty()) {
init_states();
}
const last_d = customData.again.d;
const last_s = customData.again.s;
customData.again.d = next_difficulty(last_d, "again");
customData.again.s = next_short_term_stability(last_s, ratings["again"]);
customData.hard.d = next_difficulty(last_d, "hard");
customData.hard.s = next_short_term_stability(last_s, ratings["hard"]);
customData.good.d = next_difficulty(last_d, "good");
customData.good.s = next_short_term_stability(last_s, ratings["good"]);
customData.easy.d = next_difficulty(last_d, "easy");
customData.easy.s = next_short_term_stability(last_s, ratings["easy"]);
const good_interval = next_interval(customData.good.s);
const easy_interval = Math.max(next_interval(customData.easy.s), good_interval + 1);
if ((_states$good$normal2 = states.good.normal) !== null && _states$good$normal2 !== void 0 && _states$good$normal2.review) {
states.good.normal.review.scheduledDays = good_interval;
}
if ((_states$easy$normal2 = states.easy.normal) !== null && _states$easy$normal2 !== void 0 && _states$easy$normal2.review) {
states.easy.normal.review.scheduledDays = easy_interval;
}
// For review cards
} else if (is_review()) {
var _states$current$norma, _states$hard$normal, _states$good$normal3, _states$easy$normal3;
// Convert the interval and factor to stability and difficulty if the card didn't contain customData
if (is_empty()) {
convert_states();
}
const interval = (_states$current$norma = states.current.normal) !== null && _states$current$norma !== void 0 && _states$current$norma.review.elapsedDays ? states.current.normal.review.elapsedDays : states.current.filtered.rescheduling.originalState.review.elapsedDays;
const last_d = customData.again.d;
const last_s = customData.again.s;
const retrievability = forgetting_curve(interval, last_s);
if (display_memory_state) {
fsrs_status.innerHTML += "<br>D: " + last_d + "<br>S: " + last_s + "<br>R: " + (retrievability * 100).toFixed(2) + "%";
}
customData.again.d = next_difficulty(last_d, "again");
customData.again.s = next_forget_stability(last_d, last_s, retrievability);
customData.hard.d = next_difficulty(last_d, "hard");
customData.hard.s = next_recall_stability(last_d, last_s, retrievability, "hard");
customData.good.d = next_difficulty(last_d, "good");
customData.good.s = next_recall_stability(last_d, last_s, retrievability, "good");
customData.easy.d = next_difficulty(last_d, "easy");
customData.easy.s = next_recall_stability(last_d, last_s, retrievability, "easy");
let hard_interval = next_interval(customData.hard.s);
let good_interval = next_interval(customData.good.s);
let easy_interval = next_interval(customData.easy.s);
hard_interval = Math.min(hard_interval, good_interval);
good_interval = Math.max(good_interval, hard_interval + 1);
easy_interval = Math.max(easy_interval, good_interval + 1);
if ((_states$hard$normal = states.hard.normal) !== null && _states$hard$normal !== void 0 && _states$hard$normal.review) {
states.hard.normal.review.scheduledDays = hard_interval;
}
if ((_states$good$normal3 = states.good.normal) !== null && _states$good$normal3 !== void 0 && _states$good$normal3.review) {
states.good.normal.review.scheduledDays = good_interval;
}
if ((_states$easy$normal3 = states.easy.normal) !== null && _states$easy$normal3 !== void 0 && _states$easy$normal3.review) {
states.easy.normal.review.scheduledDays = easy_interval;
}
}
function constrain_difficulty(difficulty) {
return Math.min(Math.max(+difficulty.toFixed(2), 1), 10);
}
function apply_fuzz(ivl) {
if (!enable_fuzz || ivl < 2.5) return ivl;
ivl = Math.round(ivl);
let min_ivl = Math.max(2, Math.round(ivl * 0.95 - 1));
let max_ivl = Math.round(ivl * 1.05 + 1);
if (is_review()) {
var _states$current$norma2;
const scheduledDays = (_states$current$norma2 = states.current.normal) !== null && _states$current$norma2 !== void 0 && _states$current$norma2.review.scheduledDays ? states.current.normal.review.scheduledDays : states.current.filtered.rescheduling.originalState.review.scheduledDays;
if (ivl > scheduledDays) {
min_ivl = Math.max(min_ivl, scheduledDays + 1);
}
}
return Math.floor(fuzz_factor * (max_ivl - min_ivl + 1) + min_ivl);
}
function forgetting_curve(elpased_days, stability) {
return Math.pow(1 + FACTOR * elpased_days / stability, DECAY);
}
function next_interval(stability) {
const new_interval = apply_fuzz(stability / FACTOR * (Math.pow(requestRetention, 1 / DECAY) - 1));
return Math.min(Math.max(Math.round(new_interval), 1), maximumInterval);
}
function linear_damping(delta_d, old_d) {
return delta_d * (10 - old_d) / 9;
}
function next_difficulty(d, rating) {
let delta_d = - w[6] * (ratings[rating] - 3);
let next_d = d + linear_damping(delta_d, d);
return constrain_difficulty(mean_reversion(init_difficulty("easy"), next_d));
}
function mean_reversion(init, current) {
return w[7] * init + (1 - w[7]) * current;
}
function next_recall_stability(d, s, r, rating) {
let hardPenalty = rating === "hard" ? w[15] : 1;
let easyBonus = rating === "easy" ? w[16] : 1;
return +(s * (1 + Math.exp(w[8]) * (11 - d) * Math.pow(s, -w[9]) * (Math.exp((1 - r) * w[10]) - 1) * hardPenalty * easyBonus)).toFixed(2);
}
function next_forget_stability(d, s, r) {
return +Math.min(w[11] * Math.pow(d, -w[12]) * (Math.pow(s + 1, w[13]) - 1) * Math.exp((1 - r) * w[14]), s).toFixed(2);
}
function next_short_term_stability(s, rating) {
return +(s * Math.exp(w[17] * (rating - 3 + w[18]))).toFixed(2);
}
function init_states() {
customData.again.d = init_difficulty("again");
customData.again.s = init_stability("again");
customData.hard.d = init_difficulty("hard");
customData.hard.s = init_stability("hard");
customData.good.d = init_difficulty("good");
customData.good.s = init_stability("good");
customData.easy.d = init_difficulty("easy");
customData.easy.s = init_stability("easy");
}
function init_difficulty(rating) {
return +constrain_difficulty(w[4] - Math.exp(w[5] * (ratings[rating] - 1)) + 1).toFixed(2);
}
function init_stability(rating) {
return +Math.max(w[ratings[rating] - 1], 0.1).toFixed(2);
}
function convert_states() {
const scheduledDays = states.current.normal ? states.current.normal.review.scheduledDays : states.current.filtered.rescheduling.originalState.review.scheduledDays;
const easeFactor = states.current.normal ? states.current.normal.review.easeFactor : states.current.filtered.rescheduling.originalState.review.easeFactor;
const old_s = +Math.max(scheduledDays, 0.1).toFixed(2);
const old_d = constrain_difficulty(11 - (easeFactor - 1) / (Math.exp(w[8]) * Math.pow(old_s, -w[9]) * (Math.exp(0.1 * w[10]) - 1)));
customData.again.d = old_d;
customData.again.s = old_s;
customData.hard.d = old_d;
customData.hard.s = old_s;
customData.good.d = old_d;
customData.good.s = old_s;
customData.easy.d = old_d;
customData.easy.s = old_s;
}
function is_new() {
var _states$current$norma3, _states$current$filte;
if (((_states$current$norma3 = states.current.normal) === null || _states$current$norma3 === void 0 ? void 0 : _states$current$norma3.new) !== undefined) {
var _states$current$norma4;
if (((_states$current$norma4 = states.current.normal) === null || _states$current$norma4 === void 0 ? void 0 : _states$current$norma4.new) !== null) {
return true;
}
}
if (((_states$current$filte = states.current.filtered) === null || _states$current$filte === void 0 || (_states$current$filte = _states$current$filte.rescheduling) === null || _states$current$filte === void 0 ? void 0 : _states$current$filte.originalState) !== undefined) {
var _states$current$filte2;
if (Object.hasOwn((_states$current$filte2 = states.current.filtered) === null || _states$current$filte2 === void 0 || (_states$current$filte2 = _states$current$filte2.rescheduling) === null || _states$current$filte2 === void 0 ? void 0 : _states$current$filte2.originalState, 'new')) {
return true;
}
}
return false;
}
function is_learning() {
var _states$current$norma5, _states$current$filte3, _states$current$norma7, _states$current$filte5;
if (((_states$current$norma5 = states.current.normal) === null || _states$current$norma5 === void 0 ? void 0 : _states$current$norma5.learning) !== undefined) {
var _states$current$norma6;
if (((_states$current$norma6 = states.current.normal) === null || _states$current$norma6 === void 0 ? void 0 : _states$current$norma6.learning) !== null) {
return true;
}
}
if (((_states$current$filte3 = states.current.filtered) === null || _states$current$filte3 === void 0 || (_states$current$filte3 = _states$current$filte3.rescheduling) === null || _states$current$filte3 === void 0 ? void 0 : _states$current$filte3.originalState) !== undefined) {
var _states$current$filte4;
if (Object.hasOwn((_states$current$filte4 = states.current.filtered) === null || _states$current$filte4 === void 0 || (_states$current$filte4 = _states$current$filte4.rescheduling) === null || _states$current$filte4 === void 0 ? void 0 : _states$current$filte4.originalState, 'learning')) {
return true;
}
}
if (((_states$current$norma7 = states.current.normal) === null || _states$current$norma7 === void 0 ? void 0 : _states$current$norma7.relearning) !== undefined) {
var _states$current$norma8;
if (((_states$current$norma8 = states.current.normal) === null || _states$current$norma8 === void 0 ? void 0 : _states$current$norma8.relearning) !== null) {
return true;
}
}
if (((_states$current$filte5 = states.current.filtered) === null || _states$current$filte5 === void 0 || (_states$current$filte5 = _states$current$filte5.rescheduling) === null || _states$current$filte5 === void 0 ? void 0 : _states$current$filte5.originalState) !== undefined) {
var _states$current$filte6;
if (Object.hasOwn((_states$current$filte6 = states.current.filtered) === null || _states$current$filte6 === void 0 || (_states$current$filte6 = _states$current$filte6.rescheduling) === null || _states$current$filte6 === void 0 ? void 0 : _states$current$filte6.originalState, 'relearning')) {
return true;
}
}
return false;
}
function is_review() {
var _states$current$norma9, _states$current$filte7;
if (((_states$current$norma9 = states.current.normal) === null || _states$current$norma9 === void 0 ? void 0 : _states$current$norma9.review) !== undefined) {
var _states$current$norma10;
if (((_states$current$norma10 = states.current.normal) === null || _states$current$norma10 === void 0 ? void 0 : _states$current$norma10.review) !== null) {
return true;
}
}
if (((_states$current$filte7 = states.current.filtered) === null || _states$current$filte7 === void 0 || (_states$current$filte7 = _states$current$filte7.rescheduling) === null || _states$current$filte7 === void 0 ? void 0 : _states$current$filte7.originalState) !== undefined) {
var _states$current$filte8;
if (Object.hasOwn((_states$current$filte8 = states.current.filtered) === null || _states$current$filte8 === void 0 || (_states$current$filte8 = _states$current$filte8.rescheduling) === null || _states$current$filte8 === void 0 ? void 0 : _states$current$filte8.originalState, 'review')) {
return true;
}
}
return false;
}
function is_empty() {
return !customData.again.d | !customData.again.s | !customData.hard.d | !customData.hard.s | !customData.good.d | !customData.good.s | !customData.easy.d | !customData.easy.s;
}
function set_version() {
const version = "v5.2.0";
customData.again.v = version;
customData.hard.v = version;
customData.good.v = version;
customData.easy.v = version;
}
function get_deckname() {
if (typeof ctx !== 'undefined' && ctx.deckName) {
return ctx.deckName;
} else if (document.getElementById("deck") !== null && document.getElementById("deck").getAttribute("deck_name")) {
return document.getElementById("deck").getAttribute("deck_name");
} else {
return null;
}
}
function get_seed() {
if (!customData.again.seed | !customData.hard.seed | !customData.good.seed | !customData.easy.seed) {
if (typeof ctx !== 'undefined' && ctx.seed) {
return ctx.seed;
} else {
return document.getElementById("qa").innerText;
}
} else {
return customData.good.seed;
}
}
function set_fuzz_factor() {
// Note: Originally copied from seedrandom.js package (https://github.com/davidbau/seedrandom)
!function(f,a,c){var s,l=256,p="random",d=c.pow(l,6),g=c.pow(2,52),y=2*g,h=l-1;function n(n,t,r){function e(){for(var n=u.g(6),t=d,r=0;n<g;)n=(n+r)*l,t*=l,r=u.g(1);for(;y<=n;)n/=2,t/=2,r>>>=1;return(n+r)/t}var o=[],i=j(function n(t,r){var e,o=[],i=typeof t;if(r&&"object"==i)for(e in t)try{o.push(n(t[e],r-1))}catch(n){}return o.length?o:"string"==i?t:t+"\0"}((t=1==t?{entropy:!0}:t||{}).entropy?[n,S(a)]:null==n?function(){try{var n;return s&&(n=s.randomBytes)?n=n(l):(n=new Uint8Array(l),(f.crypto||f.msCrypto).getRandomValues(n)),S(n)}catch(n){var t=f.navigator,r=t&&t.plugins;return[+new Date,f,r,f.screen,S(a)]}}():n,3),o),u=new m(o);return e.int32=function(){return 0|u.g(4)},e.quick=function(){return u.g(4)/4294967296},e.double=e,j(S(u.S),a),(t.pass||r||function(n,t,r,e){return e&&(e.S&&v(e,u),n.state=function(){return v(u,{})}),r?(c[p]=n,t):n})(e,i,"global"in t?t.global:this==c,t.state)}function m(n){var t,r=n.length,u=this,e=0,o=u.i=u.j=0,i=u.S=[];for(r||(n=[r++]);e<l;)i[e]=e++;for(e=0;e<l;e++)i[e]=i[o=h&o+n[e%r]+(t=i[e])],i[o]=t;(u.g=function(n){for(var t,r=0,e=u.i,o=u.j,i=u.S;n--;)t=i[e=h&e+1],r=r*l+i[h&(i[e]=i[o=h&o+t])+(i[o]=t)];return u.i=e,u.j=o,r})(l)}function v(n,t){return t.i=n.i,t.j=n.j,t.S=n.S.slice(),t}function j(n,t){for(var r,e=n+"",o=0;o<e.length;)t[h&o]=h&(r^=19*t[h&o])+e.charCodeAt(o++);return S(t)}function S(n){return String.fromCharCode.apply(0,n)}if(j(c.random(),a),"object"==typeof module&&module.exports){module.exports=n;try{s=require("crypto")}catch(n){}}else"function"==typeof define&&define.amd?define(function(){return n}):c["seed"+p]=n}("undefined"!=typeof self?self:this,[],Math);
// MIT License
// Copyright 2019 David Bau.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
let seed = get_seed();
const generator = new Math.seedrandom(seed);
const fuzz_factor = generator();
seed = Math.round(fuzz_factor * 10000);
customData.again.seed = (seed + 1) % 10000;
customData.hard.seed = (seed + 2) % 10000;
customData.good.seed = (seed + 3) % 10000;
customData.easy.seed = (seed + 4) % 10000;
return fuzz_factor;
}