-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainer.py
executable file
·384 lines (338 loc) · 9.27 KB
/
trainer.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import curses, os, sys, getopt, random
import locale, unicodedata, json
from curses import wrapper
import db
# Disable support for terminal colors.
os.environ["TERM"] = "vt220"
locale.setlocale(locale.LC_ALL, '')
db = db.DB('data.db')
doNumberChoice = False
def indexToChoice(num):
if doNumberChoice:
return str(num)
return chr( num + (ord('a') - 1) )
def choiceToIndex(choice):
n = ord(choice)
if n >= ord('a'):
n -= ord('a') - ord('1')
return int(chr(n))
minWait = 1500
doPresetSides = False
sides = [2,3,1]
def getSides():
return ('side' + str(i) for i in sides)
logFile = open('trainer.log', 'w')
def debug(s):
#logFile.write(str(s.encode('utf-8')) + '\n')
logFile.write(str(s) + '\n')
logFile.flush()
def pretty(obj):
print(json.dumps(obj, sort_keys=True, indent=4, separators=(',', ': ')))
def getAllLessons():
return {'lessons': db.query('SELECT ROWID, * FROM lesson')}
def getCardsInLessons(lessonIDs):
if len(lessonIDs) > 0:
sql = '''
SELECT * FROM card JOIN lesson_card ON (card.ROWID == lesson_card.fk_card)
WHERE lesson_card.fk_lesson in ({0})
'''.format(','.join('?' for _ in lessonIDs))
data = db.query(sql, lessonIDs)
else:
data = db.query('''
SELECT * FROM card JOIN lesson_card ON (card.ROWID == lesson_card.fk_card)
''')
return {'cards': data}
def drawCenterXY(w, s, yoff = 0, xoff = 0, cursorReset=True):
(y, x) = w.getmaxyx()
l = len(s.encode('utf-8'))
try:
w.addstr(int(y/2 + yoff), int(xoff), s.encode('utf-8'))
except Exception as e:
debug('addstr() failed: ' + str(e))
if cursorReset:
w.move(0, 0)
def drawCenter(w, s, yoff = 0):
drawCenterXY(w, s, yoff, 4)
class Counter():
def __init__(self, pmin, pmax, start=0, prandom=True):
self.count = start
self.cmin = pmin
self.cmax = pmax
self.crandom = prandom
self.randomHistory = []
self.randomIndex = 0
if self.crandom:
self.randnext()
def randnext(self):
if self.randomIndex < len(self.randomHistory):
# Here we replay history forwards.
self.randomIndex += 1
else:
if self.randomIndex >= 100:
self.randomHistory.pop(0)
else:
self.randomIndex += 1
n = random.randint(self.cmin, self.cmax - 1)
self.randomHistory.append(n)
self.count = self.randomHistory[self.randomIndex - 1]
def randprev(self):
if self.randomIndex > 1:
self.randomIndex -= 1
self.count = self.randomHistory[self.randomIndex - 1]
def next(self):
if self.crandom:
self.randnext()
else:
self.count += 1
if self.count >= self.cmax:
self.count = self.cmin
def prev(self):
if self.crandom:
self.randprev()
else:
self.count -= 1
if self.count <= self.cmin:
self.count = self.cmax
def get(self):
return self.count
def randomCard(cards, notThisCard):
card = notThisCard
while card == notThisCard:
n = random.randint(0, len(cards) - 1)
card = cards[n]
return card
def getTimeout(string):
t = len(string) * 0.75 * 1000
return int(max(t, minWait))
def drawCard(win, card, showAll = False):
win.clear()
pri, sec, ter = getSides()
drawCenter(win, card[pri])
if showAll:
drawCenter(win, card[ter], -1)
drawCenter(win, card[sec], 1)
win.refresh()
win.timeout(getTimeout(card[pri]))
def drawChoiceQuestion(win, card, cardList):
win.clear()
pri, sec, ter = getSides()
drawCenterXY(win, card[pri], -1, 4)
answers = [randomCard(cardList, card) for i in range(3)]
answers.append(card)
random.shuffle(answers)
(y, x) = win.getmaxyx()
hw = x / 2
i = 1
for c in answers:
xoff = hw if i % 2 == 0 else 0
yoff = 0 if i <= 2 else 1
choice = indexToChoice(i)
drawCenterXY(win, '%s: %s' % (choice, c[sec]), yoff, xoff)
i += 1
win.refresh()
win.timeout(-1)
for n, c in enumerate(answers):
if c == card:
return n+1
raise Exception('Couldn\'t find card in answer list?!')
def runChoiceQuiz(win, lessonIDs, doRandom):
cards = getCardsInLessons(lessonIDs)['cards']
i = Counter(0, len(cards)-1, prandom=doRandom)
card = cards[i.get()]
pri, sec, ter = getSides()
answer = drawChoiceQuestion(win, card, cards)
while 1:
try:
key = win.getkey()
debug('key \'%s\', answer %d: %s' % (key, answer, card[sec]))
if key in ('KEY_RIGHT', ' '):
i.next()
elif key in ('KEY_LEFT'):
i.prev()
elif key in ('KEY_RESIZE'):
answer = drawChoiceQuestion(win, card, cards)
continue
elif key in ('1', '2', '3', '4', 'a', 'b', 'c', 'd'):
#if ord(key) >= ord('a'):
# key = chr( ord(key) - (ord('a') - ord('1')) )
# debug('new key: %s' % key)
#if int(key) == answer:
if choiceToIndex(key) == answer:
debug('Correct!')
i.next()
else:
debug('Wrong!')
drawCard(win, card, showAll=True)
continue
elif key in ('z'):
debug('Showing info.')
drawCard(win, card, showAll=True)
continue
else:
i.next()
except curses.error:
debug('Timer: ' + str(curses.error))
i.next()
card = cards[i.get()]
answer = drawChoiceQuestion(win, card, cards)
def drawTypingQuestion(win, card, cardList, userInput=[]):
win.clear()
pri, sec, ter = getSides()
drawCenterXY(win, card[pri], 0, 4)
s = ''.join(userInput)
drawCenterXY(win, '> ' + s, 1, cursorReset=False)
win.refresh()
win.timeout(-1)
def runTypingQuiz(win, lessonIDs, doRandom):
cards = getCardsInLessons(lessonIDs)['cards']
i = Counter(0, len(cards)-1, prandom=doRandom)
card = cards[i.get()]
pri, sec, ter = getSides()
showAnswer = False
answer = []
drawTypingQuestion(win, card, cards, answer)
while 1:
try:
char = win.get_wch()
debug('str: {} -> {}'.format(char, ord(char)))
if showAnswer:
# Goto next card.
showAnswer = False
i.next()
elif ord(char) == 127:
# Backspace
answer = answer[:-1]
elif char in ('\n'):
s_answer = ''.join(answer)
if s_answer == card[sec]:
debug('{} matches {}'.format(s_answer, card[sec]))
i.next()
card = cards[i.get()]
else:
showAnswer = True
debug('{} does not match {}'.format(s_answer, card[sec]))
answer = []
else:
answer.append(char)
except curses.error:
debug('Timer: ' + str(curses.error))
i.next()
card = cards[i.get()]
showAnswer = False
try:
if showAnswer:
drawCard(win, card, showAll=True)
else:
drawTypingQuestion(win, card, cards, answer)
except:
debug('Draw exception: ' + str(curses.error))
def runCards(win, lessonIDs, doRandom):
cards = getCardsInLessons(lessonIDs)['cards']
i = Counter(0, len(cards)-1, prandom=doRandom)
card = cards[i.get()]
drawCard(win, card)
showAll = False
while 1:
try:
key = win.getkey()
if key in ('KEY_RIGHT', ' '):
i.next()
elif key in ('KEY_LEFT'):
i.prev()
elif key in ('KEY_RESIZE'):
drawCard(win, card, showAll)
continue
elif key in ('z'):
showAll = not showAll
drawCard(win, card, showAll)
continue
else:
i.next()
#win.addstr(0, 4, key.encode('utf-8'))
#win.move(0, 0)
#win.refresh()
#key = win.getkey()
except curses.error:
i.next()
card = cards[i.get()]
drawCard(win, card, showAll)
options = [
('h','help','This help.'),
('t:','train=','Training mode.'),
('l','lessons','List of all lessons.'),
('c','cards','List all cards in lessons from -t.'),
('r','random','Randomize cards when training.'),
('q','choice-quiz','Present multiple choice quiz questions.'),
('y','typing-quiz','Present typing quiz questions.'),
('i','invert','Invert cards so you are quized on the card backs.'),
('s:','sides=','Set the primary, secondary and tertiary card sides.'),
('w:','wait=','Minimum wait time for each card in milleseconds.')]
shortOpt = "".join([opt[0] for opt in options])
longOpt = [opt[1] for opt in options]
def usage():
pad = str(len(max(longOpt, key=len)))
fmt = ' -%s, --%-'+pad+'s : %s'
print('Usage: '+sys.argv[0]+' [options]\n')
for opt in options:
print(fmt%(opt[0][0], opt[1], opt[2]))
sys.exit(2)
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], shortOpt, longOpt)
except getopt.GetoptError as err:
print(str(err))
usage()
global sides, minWait, doNumberChoice
lessonIDs = []
doLessonList = False
doCardList = False
doRandom = False
doChoiceQuiz = False
doTypingQuiz = False
for o, a in opts:
if o in ('-h', '--help'):
usage()
elif o in ('-t', '--train'):
IDranges = a.split(",")
for r in IDranges:
start, sep, stop = r.partition('-')
if stop == '':
stop = start
for i in range(int(start), int(stop) + 1):
lessonIDs.append(i)
elif o in ('-l', '--lessons'):
doLessonList = True
elif o in ('-c', '--cards'):
doCardList = True
elif o in ('-r', '--random'):
doRandom = True
elif o in ('-q', '--choice-quiz'):
doChoiceQuiz = True
elif o in ('-y', '--typing-quiz'):
doTypingQuiz = True
elif o in ('-i', '--invert'):
sides = [3,2,1]
elif o in ('-s', '--sides'):
if len(a) != 3:
raise Exception('Must specify all three sides as -s xyz')
sides = [int(c) for c in a]
elif o in ('-w', '--wait'):
minWait = int(a)
elif o in ('-n', '--number-choice'):
doNumberChoice = True
else:
assert False, 'Unhandled option: ' + str(o)
if doLessonList:
pretty(getAllLessons())
elif doCardList:
pretty(getCardsInLessons(lessonIDs))
elif doChoiceQuiz:
wrapper(runChoiceQuiz, lessonIDs, doRandom)
elif doTypingQuiz:
wrapper(runTypingQuiz, lessonIDs, doRandom)
else:
wrapper(runCards, lessonIDs, doRandom)
if __name__ == "__main__":
main()