/
german-quiz.js
249 lines (220 loc) · 6.66 KB
/
german-quiz.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
const colors = require('colors/safe');
const columnify = require('columnify');
const conjugator = require('../lib/conjugator');
const verbs = require('../lib/verbs').verbs;
const Tenses = require('../lib/data/tenses');
const pronouns = require('../lib/data/pronouns');
var app = require('commander');
app
.option('-t, --type [type]', 'filter verb type strong or weak')
.option('-w, --weird [n]', 'filter for weirdness n/n+/n-')
.option('-i, --include-tense [tenses]', 'choose tenses (präsens,präteritum)')
.option('-l, --limit [limit]', 'limit the number of questions')
.option('--top [topn]', 'filter by topn verbs')
.parse(process.argv);
var infinitives = Object.keys(verbs);
var filters = [];
if (app.type) {
switch (app.type) {
case 'strong':
filters.push(function(verb){
return verb.strong;
});
break;
case 'weak':
filters.push(function(verb){
return !verb.strong;
});
break;
default:
throw new Error('invalid type, try weak or strong')
}
}
if (app.weird) {
var m = app.weird.match(/^([0-9]+)(\+|\-)?$/);
if (!m) throw new Error('invalid wierd spec ' + app.weird);
var num = parseInt(m[1], 10);
var type = m[2];
switch (type) {
case '+':
filters.push(function(verb){
return verb.irregularity >= num;
});
break;
case '-':
filters.push(function(verb){
return verb.irregularity <= num;
});
break;
default:
filters.push(function(verb){
return verb.irregularity == num;
});
break;
}
}
if (app.top) {
if (!/^[0-9]+$/.test(app.top)) throw new Error('top must be a number');
var n = parseInt(app.top, 10);
if (n < 1 || n > 100) throw new Error('top must be between 1 and 100');
var top100verbs = require('../lib/data/top100verbs');
filters.push(function(verb){
var rank = top100verbs.indexOf(verb.infinitive) + 1; // rank is 1 indexed
return rank && rank <= n;
});
}
infinitives = infinitives.filter(function(infinitive){
var verb = verbs[infinitive];
if (verb.tags.indexOf('test') !== -1) return false;
for (var i = 0; i < filters.length; i++) {
var filter = filters[i];
if (!filter(verb)) {
return false;
}
}
return true;
});
if (infinitives.length === 0) {
throw new Error('you filtered everything!');
}
var tenses = [];
if (app.includeTense) {
tenses = tenses.concat(app.includeTense.split(','));
} else {
tenses.push('präsens');
}
var positiveWords = [
'Super!', 'Sehr gut!', 'Richtig!', 'Richtig!', 'Toll!',
'Hammergeil!', 'Halleluja!', 'Perfekt!', 'Du machst das sehr gut!',
'Deine Freunde werden beeindruckt sein!', 'Feine Sache!', 'Darauf stoßen wir an!',
'Total toll!', 'Total super!', 'Ganz schön clever!'
];
var negativeWords = [
'Falsch!', 'Gaaaaaaaanz falsch!', 'Wer das liest is doof!', 'Macht nichts. Nochmal!',
'Nicht ganz...', 'Wenn das deine Mutter wüsste!', 'Ja ja, deine Mudder!',
'Iiih. Das ist doch kein Deutsch!'
];
var combinations = [];
infinitives.forEach(function(infinitive){
var verb = verbs[infinitive];
tenses.forEach(function(tense){
var conjugations = conjugator(verb, tense);
pronouns.forEach(function(pronoun){
var conjugation = conjugations[pronoun];
combinations.push({
verb: verb,
infinitive: infinitive,
tense: tense,
pronoun: pronoun,
conjugation: conjugation
});
});
});
});
shuffle(combinations);
if (app.limit) {
combinations.splice(0, combinations.length - app.limit);
}
console.log('' + combinations.length, 'problems to complete');
var nextCombination = createRandomPool(combinations);
var nextPositiveWord = createRandomPool(positiveWords);
var nextNegativeWord = createRandomPool(negativeWords);
var errors = [];
function askQuestion() {
var combination = nextCombination();
var infinitive = combination.infinitive;
var verb = combination.verb;
var tense = combination.tense;
var pronoun = combination.pronoun;
var conjugation = combination.conjugation;
ask('Conjugate ' + colors.bold(tenseName(tense)) + ' / ' + colors.bold(infinitive) +
' : ' + pronounName(pronoun) + ' ', function(answer) {
if (answer === conjugation) {
console.log(colors.green(nextPositiveWord() + ' ' + colors.bold('✓')));
} else {
errors.push(combination);
nextCombination.push(combination);
console.log(colors.red(nextNegativeWord() + ' ' + colors.bold('✕')), colors.grey('Die richtige Antwort ist:', conjugation));
printFullConjugation(verb);
}
if (nextCombination.isEmpty()) {
var resultText;
if (errors.length === 0) {
resultText = colors.bold('Perfekt score!');
} else if (errors.length === 1) {
resultText = colors.red('1 error');
} else {
resultText = colors.red(errors.length + ' errors');
}
console.log(colors.green('Completed ' + combinations.length + ' problems'), resultText);
} else {
askQuestion();
}
});
}
askQuestion();
function pickRandomFrom(ary) {
return ary[getRandomInt(0, ary.length)];
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
function ask(question, callback) {
var rl = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
rl.question(question, function(answer) {
rl.close();
callback(answer);
});
}
function createRandomPool(items) {
var picks = [];
function next(){
if (picks.length === 0) {
picks = items.slice();
shuffle(picks);
}
return picks.pop();
};
next.push = function(item){
var idx = getRandomInt(0, picks.length)
picks.splice(idx, 0, item);
};
next.isEmpty = function(){
return picks.length === 0;
};
return next;
}
function tenseName(tense) {
var info = Tenses.info[tense];
return [info.mood, info.tense].join(' ');
}
function pronounName(pronoun) {
return pronoun === 'es' ? 'er/sie/es' : pronoun;
}
function printFullConjugation(verb) {
var results = conjugator(verb, Tenses.simple);
var data = [];
var partizip = conjugator.partizip(verb);
if (partizip) {
data.push(['partizip', partizip]);
}
var hilfsverb = conjugator.hilfsverb(verb);
if (hilfsverb) {
data.push(['hilfsverb', hilfsverb.infinitive]);
}
Object.keys(results).forEach(function(tense){
var words = results[tense];
data.push([tenseName(tense), words.ich, words.du, words.es, words.wir, words.ihr]);
});
console.log(columnify(data, {
showHeaders: false,
columnSplitter: " "
}));
}
// http://stackoverflow.com/a/6274381
function shuffle(o){
for (var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
}