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
|
##############################################################################
#
# card.py <Peter.Bienstman@UGent.be>
#
##############################################################################
import md5, time
from mnemosyne.libmnemosyne.plugin_manager import get_database, get_scheduler
##############################################################################
#
# Card
#
# Note that we store a card_type_id, as opposed to a card_type, because
# otherwise we can't use pickled databases, as the card_types themselves are
# not stored in the database. It is also closer the SQL implementation.
#
# 'fact_view' indicate different cards generated from the same fact data,
# like reverse cards. TODO: do we need a mapping from this integer to a
# description?
#
# For UI simplicity reasons, the user is not allowed to deleted one of a set
# of related cards, only to deactivate them. This is a second, orthogonal
# selection mechanism to see if cards are active, together with not being
# in a deactivated category.
#
##############################################################################
class Card(object):
##########################################################################
#
# __init__
#
##########################################################################
def __init__(self, grade, fact, fact_view, cat_names, id=None):
self.fact = fact
self.fact_view = fact_view
self.active = True
db = get_database()
self.cat = []
for cat_name in cat_names:
self.cat.append(db.get_or_create_category_with_name(cat_name))
self.reset_learning_data() # TODO: see where this is used and merge.
# The initial grading is seen as the first repetition.
self.grade = grade
self.acq_reps = 1
self.acq_reps_since_lapse = 1
self.last_rep = start_date.days_since_start()
self.easiness = db.average_easiness()
if id is not None:
self.new_id()
else:
self.id = id
sch = get_scheduler()
new_interval = sch.calculate_initial_interval(grade)
new_interval += sch.calculate_interval_noise(new_interval)
self.next_rep = start_date.days_since_start() + new_interval
##########################################################################
#
# question
#
##########################################################################
def question(self):
card_type = get_card_type_by_id(self.card_type_id)
q = card_type.generate_q(self.fact, self.fact_view)
#q = preprocess(q) # TODO: update to plugin scheme
return q
##########################################################################
#
# answer
#
##########################################################################
def answer(self):
card_type = get_card_type_by_id(self.card_type_id)
a = card_type.generate_a(self.fact, self.fact_view)
#a = preprocess(a) # TODO: update to plugin scheme
return a
##########################################################################
#
# reset_learning_data
#
##########################################################################
def reset_learning_data(self):
self.grade = 0
self.easiness = 2.5
self.acq_reps = 0
self.ret_reps = 0
self.lapses = 0
self.acq_reps_since_lapse = 0
self.ret_reps_since_lapse = 0
# TODO: store as float for minute level scheduling.
self.last_rep = 0 # In days since beginning.
self.next_rep = 0 #
##########################################################################
#
# new_id
#
##########################################################################
def new_id(self):
digest = md5.new(self.q.encode("utf-8") + self.a.encode("utf-8") + \
time.ctime()).hexdigest()
self.id = digest[0:8]
##########################################################################
#
# interval
#
##########################################################################
def interval(self):
return self.next_rep - self.last_rep
##########################################################################
#
# sort_key: needed?
#
##########################################################################
def sort_key(self):
return self.next_rep
##########################################################################
#
# sort_key_newest: needed?
#
##########################################################################
def sort_key_newest(self):
return self.acq_reps + self.ret_reps
##########################################################################
#
# is_active
#
##########################################################################
def is_active(self):
if self.active == False:
return False
if self.fact.is_active() == False:
return False
for c in self.cat:
if c.active == False:
return False
return True
# TODO: see which of these we still need and if they could be moved to
# database
##########################################################################
#
# is_new
#
##########################################################################
def is_new(self):
return (self.acq_reps == 0) and (self.ret_reps == 0)
##########################################################################
#
# is_overdue
#
##########################################################################
def is_overdue(self):
return (self.grade >= 2) and (self.is_in_active_category()) and \
(days_since_start > self.next_rep)
##########################################################################
#
# days_since_last_rep
#
##########################################################################
def days_since_last_rep(self):
return days_since_start - self.last_rep
##########################################################################
#
# days_until_next_rep
#
##########################################################################
def days_until_next_rep(self):
return self.next_rep - days_since_start
##########################################################################
#
# qualifies_for_learn_ahead
#
##########################################################################
def qualifies_for_learn_ahead(self):
return (self.grade >= 2) and (self.is_in_active_category()) and \
(get_days_since_start() < self.next_rep)
##########################################################################
#
# change_category
#
##########################################################################
def change_category(self, new_cat_name):
old_cat = self.cat
self.cat = get_category_by_name(new_cat_name)
remove_category_if_unused(old_cat)
|