-
Notifications
You must be signed in to change notification settings - Fork 0
/
learn_answers
executable file
·204 lines (161 loc) · 6.37 KB
/
learn_answers
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
#!/usr/bin/env python3
import argparse
import json
import curses
import random
def main():
parser = argparse.ArgumentParser(description='Learn answers.')
parser.add_argument('-f', '--file',
type=argparse.FileType('r'),
help='Input file.')
parser.add_argument('-r', '--random',
action='store_true',
help='Choose random question.')
parser.add_argument('-l', '--loop',
action='store_true',
help='Ask questions forever. Ctrl+c to exit')
args = parser.parse_args()
# args = parser.parse_args(['-f', 'test.json'])
f = args.file
data = json.load(f)
f.close()
win = curses.initscr()
nlines, ncols = win.getmaxyx()
skip_question = 0
restart_question = 1
pass_answer = 2
no_pass_answer = 3
def get_word():
word = ''
while True:
ch = win.getkey()
if ch == ' ' or ch == '\n' or ch == '\x7f':
return word, ch
if ch.isprintable():
word += ch
def analyze_answer(i_answer, answers, note):
pre_ans = ''
if len(answers) > 1:
pre_ans = str(i_answer+1) + ". "
win.addstr(pre_ans)
answer = answers[i_answer]
answer_words = answer.split()
in_word, state = get_word()
if state == '\n':
if len(in_word) == 0:
return skip_question
elif len(answer_words) > 1:
return no_pass_answer
pass_i_answer = None
total_chrs = 0
last_x = 0
for i in range(len(answer_words)):
if in_word.endswith('.') and not answer_words[i].endswith('.'):
in_word = in_word[:-1]
if not in_word.endswith(('.', ',')) and answer_words[i].endswith(('.', ',')):
in_word += answer_words[i][-1]
if in_word != answer_words[i]:
win.addstr('\n')
while True: # practice answer
win.addstr(' ' * len(pre_ans))
if pass_i_answer is None:
for j in range(len(answer_words)):
if j == i:
win.addstr(
answer_words[j], curses.A_STANDOUT)
else:
win.addstr(
answer_words[j], curses.A_UNDERLINE)
if j != len(answer_words) - 1:
win.addstr(' ', curses.A_UNDERLINE)
win.addstr('\n')
if note:
win.addstr(note + '\n\n', curses.A_DIM)
pass_i_answer = False
else:
win.addstr(' ' * (total_chrs % ncols))
win.addstr(
answer_words[i] + '\n', curses.A_STANDOUT)
win.addstr(pre_ans)
win.addstr(answer[len(pre_ans) + total_chrs - last_x: total_chrs])
last_y, last_x = win.getyx()
if last_y > nlines - 3:
return restart_question
in_word, state = get_word()
if state == '\n':
return restart_question
if in_word == answer_words[i]:
break
else:
win.addstr('\n')
total_chrs += len(answer_words[i])
if i < len(answer_words) - 1:
total_chrs += 1
if i == len(answer_words) - 1:
if pass_i_answer is None:
return pass_answer
else:
return no_pass_answer
last_y, last_x = win.getyx()
if last_y > nlines - 3:
return restart_question
in_word, state = get_word()
if state == '\n' and i < len(answer_words) - 2:
return restart_question
while True:
if args.random and len(data) > 1:
last_q = data[-1]['question']
while True:
random.shuffle(data)
if data[0]['question'] != last_q:
break
for q, question in enumerate(data):
answers = question.get('answer')
if answers is None:
answers = [answer.get('answer') for answer in question.get('answers')]
else:
answers = [answers]
options = question.get('options')
if options is not None:
options = [option.get('option') for option in options]
answers = [options[answer-1] for answer in answers]
random.shuffle(options) # random order options
answers = [option for option in options if option in answers]
note = question.get('note')
complement = question.get('complement')
i_answer = 0
while True:
win.clear()
if not args.random:
win.addstr(str(q+1) + '. ', curses.A_BOLD) # add num question
win.addstr(question.get('question') + '\n', curses.A_BOLD)
if options is not None:
for option in options:
win.addstr(' • ' + option + '\n')
win.addstr('\n')
if i_answer == 0:
pass_answers = True
answer_result = analyze_answer(i_answer, answers, note)
if answer_result == no_pass_answer:
pass_answers = False
elif answer_result == pass_answer:
if i_answer == len(answers) - 1:
if pass_answers:
break
else:
i_answer = 0
else:
i_answer += 1
elif answer_result == restart_question:
i_answer = 0
elif answer_result == skip_question:
break
if not args.loop:
break
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
pass
print('\nTraining finished')
curses.endwin()