-
Notifications
You must be signed in to change notification settings - Fork 0
/
learn.lua
executable file
·364 lines (273 loc) · 7.36 KB
/
learn.lua
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
#!/usr/bin/env lua
local Serialize = dofile("serialize.lua")
local dict = {
lang = {},
data = {},
}
-------------------------------------------
-- Constants ------------------------------
-------------------------------------------
local success_max
local freq_decrement_base
-------------------------------------------
local freq_init
-------------------------------------------
local LoadConstants = function()
success_max = dict.success_max or 3
freq_decrement_base = dict.freq_decrement_base or 2
freq_init = math.ceil(math.ceil(freq_decrement_base) ^ success_max)
end
local query_mt = {
__index = function(t,k)
t[k] = {[0] = {0, 0}}
return t[k]
end
}
local Trim = function(l)
l = string.match(l, "%s*(.-)%s*$")
l = string.gsub (l, "(%p)", "%1 ")
l = string.gsub (l, "%s+", " ")
l = string.gsub (l, " (%p)", "%1")
return l
end
local AppendDict = function(new_dict)
if new_dict.lang then
for i,new_lang in ipairs(new_dict.lang) do
if not dict.lang[new_lang] then
table.insert(dict.lang,new_lang)
dict.lang[new_lang] = #dict.lang
end
end
end
if new_dict.data then
for i,new_data in ipairs(new_dict.data) do
local reordered_data = {}
for j,x in ipairs(new_data) do
local new_table = type(x) == "table" and x or {x}
for k,v in ipairs(new_table) do
new_table[k] = Trim(v)
end
reordered_data [dict.lang[new_dict.lang[j]]] = new_table
end
table.insert(dict.data, reordered_data)
end
end
if new_dict.pairs then
if not dict.query then dict.query = {} end
for i,j in ipairs(new_dict.pairs) do
local langQ = dict.lang[j[1]]
local langA = dict.lang[j[2]]
if not dict.query[langQ] then dict.query[langQ] = {} end
if not dict.query[langQ][langA] then dict.query[langQ][langA] = setmetatable({}, query_mt) end
end
end
if new_dict.success_max then
dict.success_max = math.min(math.max(math.floor(new_dict.success_max), 1), 9)
end
if new_dict.freq_decrement_base then
dict.freq_decrement_base = math.min(math.max(new_dict.freq_decrement_base, 1), 10)
end
end
local CollectVocabularies = function()
dict.vocabulary = {}
for i = 1,#dict.lang do
dict.vocabulary[i] = {}
end
for i,record in ipairs(dict.data) do
for lang,words in pairs(record) do
local vocabulary = dict.vocabulary[lang]
for j,word in ipairs(words) do
if not vocabulary[word] then
table.insert(vocabulary,word)
vocabulary[word] = {}
end
table.insert(vocabulary[word],i)
end
end
end
end
local AnswerFor = function(q)
return q and dict.query[q[1]][q[2]][q[3]]
end
local TraverseQueryWith = function(f)
for langQ=1,#dict.lang do
if dict.query[langQ] then
for langA=1,#dict.lang do
if dict.query[langQ][langA] then
for i,wordQ in ipairs(dict.vocabulary[langQ]) do
local question = {langQ, langA, i}
if f(question, AnswerFor(question), wordQ) then
return
end
end
end
end
end
end
end
local MaxFreq = function(ans)
local m = 0
for i,word in ipairs(ans) do
m = math.max(m, ans[word][1])
end
return m
end
local Refill = function(wt, wf_init)
for i,word in ipairs(wt) do
local w = wt[word]
w[1] = wf_init
w[2] = 0
end
end
local ChangeFreq = function(t, v)
dict.freq_total = dict.freq_total - t[1] + v
t[1] = v
end
local RestoreFreq = function(answer)
if answer then
ChangeFreq(answer[0], MaxFreq(answer))
end
end
local FillQuery = function()
dict.freq_total = 0
LoadConstants()
TraverseQueryWith(
function(question, answer, wordQ)
for j,data_line in ipairs(dict.vocabulary[question[1]][wordQ]) do
if dict.data[data_line][question[2]] then
for k,wordA in ipairs(dict.data[data_line][question[2]]) do
if not answer[wordA] then
table.insert(answer, wordA)
answer[wordA] = {}
end
end
end
end
Refill(answer, freq_init)
RestoreFreq(answer)
end
)
end
local correct
local SelectQuestion = function()
if dict.freq_total == 0 then
RestoreFreq(correct)
end
if dict.freq_total == 0 then
io.write("\nDone!\n\n")
return false
end
io.write("\n\nPress <ENTER> to continue ( any symbols to interrupt ) : ")
if io.read() ~= "" then
return false
end
local dice = math.random(dict.freq_total)
local freq = 0
TraverseQueryWith(
function(question, answer)
freq = freq + answer[0][1]
if freq >= dice then
RestoreFreq(correct)
dict.question = question
correct = answer
return true
end
end
)
return true
end
local InputAnswer = function()
os.execute("clear")
io.write(dict.lang[dict.question[1]], " : ")
io.write(dict.vocabulary[dict.question[1]][dict.question[3]], "\n\n")
local answer = {}
for i=1,#correct do
io.write(dict.lang[dict.question[2]], " : ")
local next_answer = io.read()
if next_answer == "" then
break
end
table.insert(answer,Trim(next_answer))
end
return answer
end
local Reward = function(w)
if w[2] < success_max then
w[1] = math.floor(w[1] / freq_decrement_base)
w[2] = w[2] + 1
end
if w[2] >= success_max then
w[1] = 0
end
end
local CheckAnswer = function(answer)
ChangeFreq(correct[0], 0)
correct[0][2] = correct[0][2] + 1
local good = {}
local bad = {}
for i,word in ipairs(answer) do
table.insert(correct[word] and good or bad, word)
end
for i,word in ipairs(good) do
Reward(correct[word])
end
local indent = string.rep(" ",utf8.len(dict.lang[dict.question[2]]) + 3)
if #bad > 0 or #good == 0 then
Refill(correct, math.max(dict.freq_total, freq_init))
io.write("\n\nError!\n\n")
for i,word in ipairs(bad) do
io.write(indent, word, "\n")
end
end
io.write("\n\nCorrect :\n\n")
for i,word in ipairs(correct) do
io.write(indent, word, " ")
io.write(string.rep("+", correct[word][2]))
io.write(string.rep("-", success_max - correct[word][2]), "\n")
end
end
local ShowStats = function()
local questions_total = 0
local hist = {}
TraverseQueryWith(
function(question, answer)
questions_total = questions_total + answer[0][2]
for j,wordA in ipairs(answer) do
local k = answer[wordA][2]
hist[k] = hist[k] and hist[k] + 1 or 1
end
end
)
io.write("\nQuestions total = ", questions_total, "\n")
for i=0,success_max do
io.write("\n",i," ")
if hist[i] then
io.write(string.rep(">", math.floor(math.log(hist[i])/math.log(2))+1))
io.write( hist[i] > 2 and hist[i] or "")
end
end
io.write("\n\nMemory usage = ", math.ceil(collectgarbage("count")), " kB\n\n")
end
io.write("Learn 0.3 Copyright (C) 2021 Andrey Dobrovolsky\n")
local saved_name = "learn.save"
if #arg == 0 then
dict = dofile(saved_name)
else
for i,name in ipairs(arg) do
AppendDict(dofile(name))
end
if dict.query then
CollectVocabularies()
FillQuery()
end
end
if dict.query then
LoadConstants()
correct = AnswerFor(dict.question)
math.randomseed(os.time())
while SelectQuestion() do
CheckAnswer(InputAnswer())
end
ShowStats()
end
io.output(saved_name) io.write("return ") Serialize(dict) io.write("\n")