forked from juanchel/conjugator
-
Notifications
You must be signed in to change notification settings - Fork 4
/
conjugate.js
527 lines (455 loc) · 12.6 KB
/
conjugate.js
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
'use strict';
var score = 0;
var time = 0;
var mult = 1;
var _timeMax = 30;
var timeMax = _timeMax;
var correct = '';
var quiz_term = '';
var termType = '';
var skipped = false;
var scored = false;
$(document).ready(function() {
// Stop the user from pressing enter in the text area
$('textarea').bind('keypress', function(e) {
if ((e.keyCode || e.which) == 13) {
$(this).parents('form').submit();
skipQuestion();
return false;
}
});
// When the play button is clicked
$('#play').add("#optplay").click(function() {
nextQuestion();
$('#start-screen').animate({top: '-1000px'}, 800);
$('#main').show();
$('#option-menu').hide();
$('#main').animate({'margin-top': '20px'}, 800);
$('#title-text').animate({'width': '350px', 'font-size': '20pt', 'height': '40px', 'bottom': '5px', 'margin-bottom': '0px'}, 800);
$('#title').animate({'height': '50px'}, 800);
$('#ribbon img').animate({'height': '50px', 'width': '50px'}, 800);
});
$('#options').click(function() {
$('#start-screen').animate({top: '-1000px'}, 800);
$('#option-menu').show();
$('#option-menu').animate({'margin-top': '20px'}, 800);
$('#title-text').animate({'width': '350px', 'font-size': '20pt', 'height': '40px', 'bottom': '5px', 'margin-bottom': '0px'}, 800);
$('#title').animate({'height': '50px'}, 800);
$('#ribbon img').animate({'height': '50px', 'width': '50px'}, 800);
});
function debugTerm(e)
{
var wi = $(e.target).parents('.wellitem:first');
if(wi.data('done'))
{
wi.data('done', false).find('.debug-out').remove();
return;
}
var d = wi.data();
var term = d.term;
var typeMods = d.type;
var w = $('<div/>')
.addClass('debug-out');
w.append("<hr />");
typeMods.forEach(function(mod)
{
debugMod(term, mod, w);
});
wi.append(w).data('done', true);
};
function debugMod(term, mod, w, premods)
{
var q = Question({"word": term});
if(premods)
{
premods.forEach(function(m)
{
q.modify([m], true);
});
}
else
{
premods = [];
}
var newmods = premods.filter(listCopy);
newmods.push(mod);
q.modify([mod], true);
var desc = $.unique($.merge([], q.modList).filter(filterFalse));
if(desc.length)
{
w.append(q.word + " - " + desc.join(', '))
.append("<br />");
}
};
$('#well').on('click', '.debug', debugTerm);
var genOpts = function(id, opts)
{
opts.forEach(function(opt)
{
opt[1] = "opt-" + opt[1];
opt.unshift($("#"+id));
genFullOption.apply(this, opt)
});
}
genOpts('adjective-options', [
['い adjectives', 'iadj']
]);
genOpts('conjugation-options', [
ModTypes.FORMAL,
ModTypes.INFORMAL,
ModTypes.PAST,
ModTypes.NEGATIVE,
ModTypes.TE,
ModTypes.IMPERATIVE,
ModTypes.VOLITIONAL,
ModTypes.WANTING,
ModTypes.PROGRESSIVE,
ModTypes.PASSIVE,
ModTypes.POTENTIAL,
ModTypes.CAUSATIVE,
ModTypes.CONDITIONAL_BA,
ModTypes.CONDITIONAL_TARA
]);
genOpts('verb-options',[
['To be (いる, ある)', 'to_be'],
['Ichidan (-いる, -える)', 'ichidan'],
['Irregular (する, 来る)', 'irregular'],
['Godan', 'godan']
]);
genOpts('kanji-options',[
['Show and Accept Kanji', 'kanji'],
['Show Furigana', 'furigana'],
]);
$("#option-menu input:checkbox")
.change(function()
{
location.hash = configString();
})
.each(function(i)
{
$(this).data("cfg", Math.pow(2, i));
});
setConfig(location.hash.replace(/^\#/, ''));
});
function Question(term) {
if(!(this instanceof Question))
return new Question(term);
this.word = term.word;
this.kanji = '';
if ($("#opt-kanji:checked").length == 1)
this.kanji = term.kanji;
this.base = term.kanji || term.word;
this.modList = [];
}
Question.prototype.modify = function(modSet, skipNext) {
if (!modSet.length) return;
// Pick and apply a random mod
var modifier = fetchRandom(modSet);
this.word = modifier.modFunc(this.word);
if(this.kanji)
this.kanji = modifier.modFunc(this.kanji);
this.modList.push.apply(this.modList, modifier.desc);
}
// Fetches a random element of an array
function fetchRandom(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
// Skips a question and shows the correct answer
function skipQuestion() {
if (skipped || scored) {
nextQuestion();
} else {
skipped = true;
scored = false;
time = -1;
mult = 1;
$('#mult').text(mult);
$('#answer').addClass('flash-red');
$('#time-bar').css('background', '#e74c3c');
addWell($('#answer').val()||'', correct, quiz_term, false)
$('#answer').val(correct[0]);
setTimeout(function(){
$('#answer').removeClass('flash-red');
}, 300);
}
}
// Check if the answer is correct every time a character is typed
function submitAnswer() {
if(skipped || scored) return;
var ans = $('#answer').val().replace(/\s/g, '');
if (correct.indexOf(ans) > -1 && !skipped) {
$('#answer').addClass('flash');
setTimeout(function(){
$('#answer').removeClass('flash');
}, 300);
if (time > 0) {
score += Math.ceil(time * mult / timeMax);
mult += 1;
timeMax *= 0.95;
} else {
mult = 1;
timeMax = _timeMax;
}
addWell(ans, correct, quiz_term, true)
$('#score').text(score);
$('#mult').text(mult);
scored = true;
}
}
// Sets time remaining bar to the percentage passed in
function setTimeBar(percent) {
$('#time-bar').css('background-image', 'linear-gradient(left, #3498db ' + percent + '%, #ecf0f1 ' + percent + '%)');
$('#time-bar').css('background-image', '-o-linear-gradient(left, #3498db ' + percent + '%, #ecf0f1 ' + percent + '%)');
$('#time-bar').css('background-image', '-moz-linear-gradient(left, #3498db ' + percent + '%, #ecf0f1 ' + percent + '%)');
$('#time-bar').css('background-image', '-webkit-linear-gradient(left, #3498db ' + percent + '%, #ecf0f1 ' + percent + '%)');
$('#time-bar').css('background-image', '-ms-linear-gradient(left, #3498db ' + percent + '%, #ecf0f1 ' + percent + '%)');
}
// Generate a new question
function nextQuestion() {
console.clear();
scored = false;
skipped = false;
setTimeBar(100);
time = 100 * timeMax;
var wordset = pickType(),
type = wordset[0],
terms = wordset[1],
pos = wordset[2];
var term = terms[Math.floor(Math.random() * terms.length)];
termType = type;
$('#part').text(pos)
var question = new Question(term);
question.modify(type);
correct = ([question.word, question.kanji]).filter(filterFalse);
quiz_term = term.word;
console.log(correct.join(", "));
$('#question-word').html(term.render());
$('#meaning').html(term.def);
$('#mods .mod').remove();
$('#answer').val('');
$('#well').data('mods', question.modList.map(listCopy));
fadeInMods(question.modList);
}
// Function for animating the mods falling in
function fadeInMods(modList) {
var $space = $('<div/>', {class: 'space'});
$space.text('.')
var $toAdd = $('<div/>', {class: 'mod', style: 'display:none'});
$toAdd.text(modList.shift());
$space.insertBefore('#mod-clear');
$toAdd.insertBefore('#mod-clear');
$('.space').animate({width: '0px'}, 300);
$('.mod').fadeIn(300);
if (modList.length > 0) {
setTimeout(function() {
$('.space').remove();
fadeInMods(modList);
}, 300);
}
}
// Picks a type of word to make the next question about
// This function returns the object dictionary so it can be passed around easily
var sets = null
function pickType() {
var sum = 0;
if(sets == null)
{
sets = [];
if($("#opt-ichidan:checked").length)
sets.push([ICHIDAN, ichidan, '[ichidan] v.']);
if($("#opt-godan:checked").length)
sets.push([GODAN, godan, '[godan] v.']);
if($("#opt-irregular:checked").length)
{
sets.push([IRREGULAR_SURU, irregular_suru, '[irregular] v.']);
sets.push([IRREGULAR_KURU, irregular_kuru, '[irregular] v.']);
}
if($("#opt-iadj:checked").length)
sets.push([II_ADJECTIVE, ii_adjective, '[i] adj.']);
// keep last
if($("#opt-to_be:checked").length || !sets.length)
{
sets.push([TO_BE_IRU, to_be_iru, '[to be] v.']);
sets.push([TO_BE_ARU, to_be_aru, '[to be] v.']);
}
// remove config-disabled modifiers
filterSets(sets);
}
if(sets.length == 1)
return sets[0];
sets.forEach(function(s)
{
sum += s[1].length;
});
var rando = ~~(Math.random() * sum);
var i=0
do {
if(rando < sets[i][1].length)
return sets[i]
rando -= sets[i][1].length
i++;
} while (i < sets.length);
}
// Returns the word without the last kana
function trimLast(word) {
return word.substring(0, word.length - 1);
}
function snipLast(word) {
return word.substr(-1);
}
// Timer function called 100 times per second
function interval() {
if (skipped || scored)
return
time--;
setTimeBar(time/timeMax);
}
// Adds an label to the options menu
function genLabel(desc) {
var $label = $('<div/>', {class: 'option-label'});
$label.text(desc);
return $label;
}
// Adds an option to the options menu
function genOption(desc) {
var $option = $('<div/>', {class: 'check-box'});
var $input = $('<input/>', {type: 'checkbox', value: '0', id: desc, name: ''});
$input.prop('checked', true);
var $label = $('<label/>', {for: desc});
$option.append($input);
$option.append($label);
return $option;
}
function genFullOption(target, label, opt) {
target.append(genLabel(label));
target.append(genOption(opt));
}
var t = setInterval(interval, 10);
function addWell(actual, expected, rootword, isCorrect)
{
var mods = $('#well')
.data('mods')
.filter(filterFalse)
.join(", ");
var def = $("#meaning").text();
if(!def)
return;
var w = $('<div/>')
.addClass('wellitem')
.data({
type: termType,
term: rootword
});
w.append(
$("<span/>")
.addClass("well-right")
.addClass("mods")
.append(def + " — ")
.append(mods + " ")
.append(
$("<span/>")
.addClass('debug')
.text('≟')
.attr({
title: "Click to view conjugations."
})
)
);
var expected_link = $("<a/>")
.html($.unique(expected).join('<br />'))
.addClass("answers")
.attr({
href: "http://jisho.org/search/" + encodeURIComponent(rootword),
target: "jisho",
title: "Jisho - " + rootword + " - click Show Inflections to review conjugations."
});
var wellLeft = $("<div />")
.addClass("well-left")
.append(expected_link);
if(isCorrect)
{
w.addClass('correct')
.append(wellLeft);
}
else
{
if(actual.replace(/\s/g,'')) {
wellLeft.prepend(
$('<span/>')
.addClass("response")
.addClass('striken')
.html(actual + "<br />")
);
}
w.addClass('skipped')
.append(wellLeft);
}
w.append(
$('<div/>').addClass('clear')
);
$('#well').prepend(w);
}
function configString()
{
return $("#option-menu input:checkbox:checked")
.map(function(){
return $(this).data('cfg')
})
.toArray()
.reduce(function(a,b)
{
return a + b;
}).toString(36);
}
function setConfig(str)
{
str=""+str;
if(!str.length) return;
var bits = parseInt(""+str, 36, 10);
$("#option-menu input:checkbox")
.each(function(){
var bval = +$(this).data('cfg');
$(this).prop("checked", !!(bits & bval));
});
}
function checkConfig(opts)
{
var i, id;
for(i=0; i < opts.length; i++)
{
if(opts[i] == 'base')
continue;
id = '#opt-' + opts[i];
if($(id).filter(":checked").length == 0)
{
return false;
}
}
return true;
}
function filterSets(sets)
{
var i=0, mods, terms;
for(i; i < sets.length; i++)
{
sets[i][0] = sets[i][0].filter(filterMod);
}
return sets;
};
function filterMod(mod)
{
var i, flags = mod.flag;
if(!checkConfig(flags))
return false;
return true;
};
// l.filter(filterFalse)
function filterFalse(it)
{
return !!it;
};
// l.map(listCopy)
function listCopy(i)
{
return i;
}