-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
models.py
307 lines (272 loc) · 8.39 KB
/
models.py
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
from dataclasses import dataclass
from datetime import datetime, timedelta, timezone
import copy
from typing import Any, Optional, Union
from enum import IntEnum
class State(IntEnum):
New = 0
Learning = 1
Review = 2
Relearning = 3
class Rating(IntEnum):
Again = 1
Hard = 2
Good = 3
Easy = 4
class ReviewLog:
rating: Rating
scheduled_days: int
elapsed_days: int
review: datetime
state: State
def __init__(
self,
rating: Rating,
scheduled_days: int,
elapsed_days: int,
review: datetime,
state: State,
) -> None:
self.rating = rating
self.scheduled_days = scheduled_days
self.elapsed_days = elapsed_days
self.review = review
self.state = state
def to_dict(self) -> dict[str, Union[int, str]]:
return_dict = {
"rating": self.rating.value,
"scheduled_days": self.scheduled_days,
"elapsed_days": self.elapsed_days,
"review": self.review.isoformat(),
"state": self.state.value,
}
return return_dict
@staticmethod
def from_dict(source_dict: dict[str, Any]) -> "ReviewLog":
rating = Rating(int(source_dict["rating"]))
scheduled_days = int(source_dict["scheduled_days"])
elapsed_days = int(source_dict["elapsed_days"])
review = datetime.fromisoformat(source_dict["review"])
state = State(int(source_dict["state"]))
return ReviewLog(
rating,
scheduled_days,
elapsed_days,
review,
state,
)
class Card:
due: datetime
stability: float
difficulty: float
elapsed_days: int
scheduled_days: int
reps: int
lapses: int
state: State
last_review: datetime
def __init__(
self,
due: Optional[datetime] = None,
stability: float = 0,
difficulty: float = 0,
elapsed_days: int = 0,
scheduled_days: int = 0,
reps: int = 0,
lapses: int = 0,
state: State = State.New,
last_review: Optional[datetime] = None,
) -> None:
if due is None:
self.due = datetime.now(timezone.utc)
else:
self.due = due
self.stability = stability
self.difficulty = difficulty
self.elapsed_days = elapsed_days
self.scheduled_days = scheduled_days
self.reps = reps
self.lapses = lapses
self.state = state
if last_review is not None:
self.last_review = last_review
def to_dict(self) -> dict[str, Any]:
return_dict = {
"due": self.due.isoformat(),
"stability": self.stability,
"difficulty": self.difficulty,
"elapsed_days": self.elapsed_days,
"scheduled_days": self.scheduled_days,
"reps": self.reps,
"lapses": self.lapses,
"state": self.state.value,
}
if hasattr(self, "last_review"):
return_dict["last_review"] = self.last_review.isoformat()
return return_dict
@staticmethod
def from_dict(source_dict: dict[str, Any]) -> "Card":
due = datetime.fromisoformat(source_dict["due"])
stability = float(source_dict["stability"])
difficulty = float(source_dict["difficulty"])
elapsed_days = int(source_dict["elapsed_days"])
scheduled_days = int(source_dict["scheduled_days"])
reps = int(source_dict["reps"])
lapses = int(source_dict["lapses"])
state = State(int(source_dict["state"]))
if "last_review" in source_dict:
last_review = datetime.fromisoformat(source_dict["last_review"])
else:
last_review = None
return Card(
due,
stability,
difficulty,
elapsed_days,
scheduled_days,
reps,
lapses,
state,
last_review,
)
def get_retrievability(self, now: datetime) -> Optional[float]:
DECAY = -0.5
FACTOR = 0.9 ** (1 / DECAY) - 1
if self.state == State.Review:
elapsed_days = max(0, (now - self.last_review).days)
return (1 + FACTOR * elapsed_days / self.stability) ** DECAY
else:
return None
@dataclass
class SchedulingInfo:
card: Card
review_log: ReviewLog
class SchedulingCards:
again: Card
hard: Card
good: Card
easy: Card
def __init__(self, card: Card) -> None:
self.again = copy.deepcopy(card)
self.hard = copy.deepcopy(card)
self.good = copy.deepcopy(card)
self.easy = copy.deepcopy(card)
def update_state(self, state: State) -> None:
if state == State.New:
self.again.state = State.Learning
self.hard.state = State.Learning
self.good.state = State.Learning
self.easy.state = State.Review
elif state == State.Learning or state == State.Relearning:
self.again.state = state
self.hard.state = state
self.good.state = State.Review
self.easy.state = State.Review
elif state == State.Review:
self.again.state = State.Relearning
self.hard.state = State.Review
self.good.state = State.Review
self.easy.state = State.Review
self.again.lapses += 1
def schedule(
self,
now: datetime,
hard_interval: int,
good_interval: int,
easy_interval: int,
) -> None:
self.again.scheduled_days = 0
self.hard.scheduled_days = hard_interval
self.good.scheduled_days = good_interval
self.easy.scheduled_days = easy_interval
self.again.due = now + timedelta(minutes=5)
if hard_interval > 0:
self.hard.due = now + timedelta(days=hard_interval)
else:
self.hard.due = now + timedelta(minutes=10)
self.good.due = now + timedelta(days=good_interval)
self.easy.due = now + timedelta(days=easy_interval)
def record_log(self, card: Card, now: datetime) -> dict[Rating, SchedulingInfo]:
return {
Rating.Again: SchedulingInfo(
self.again,
ReviewLog(
Rating.Again,
self.again.scheduled_days,
card.elapsed_days,
now,
card.state,
),
),
Rating.Hard: SchedulingInfo(
self.hard,
ReviewLog(
Rating.Hard,
self.hard.scheduled_days,
card.elapsed_days,
now,
card.state,
),
),
Rating.Good: SchedulingInfo(
self.good,
ReviewLog(
Rating.Good,
self.good.scheduled_days,
card.elapsed_days,
now,
card.state,
),
),
Rating.Easy: SchedulingInfo(
self.easy,
ReviewLog(
Rating.Easy,
self.easy.scheduled_days,
card.elapsed_days,
now,
card.state,
),
),
}
class Parameters:
request_retention: float
maximum_interval: int
w: tuple[float, ...]
def __init__(
self,
w: Optional[tuple[float, ...]] = None,
request_retention: Optional[float] = None,
maximum_interval: Optional[int] = None,
) -> None:
self.w = (
w
if w is not None
else (
0.4072,
1.1829,
3.1262,
15.4722,
7.2102,
0.5316,
1.0651,
0.0234,
1.616,
0.1544,
1.0824,
1.9813,
0.0953,
0.2975,
2.2042,
0.2407,
2.9466,
0.5034,
0.6567,
)
)
self.request_retention = (
request_retention if request_retention is not None else 0.9
)
self.maximum_interval = (
maximum_interval if maximum_interval is not None else 36500
)