-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertToAnki.js
111 lines (94 loc) · 2.88 KB
/
convertToAnki.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
const fs = require('fs');
const Q_AND_A_DIR = 'manually-cleaned';
const DIST_DIR = 'dist';
const getCatalogue = (name) => {
const solutions = fs.readFileSync(`./${Q_AND_A_DIR}/${name}-solutions.md`, 'utf8')
.trim()
.split('');
const questions = fs.readFileSync(`./${Q_AND_A_DIR}/${name}-questions.md`, 'utf8')
.trim()
.split('\n\n')
.map((question, i) => {
return {
question: question.split('\n')[0],
answers: question.split('\n').slice(1),
correctAnswer: solutions[i],
};
});
console.log(`Parsed solutions and questions for: ${name}`);
console.log(`--> Solutions count: ${solutions.length}`);
console.log(`--> Questions count: ${questions.length}`);
return questions;
};
const saveCataloguesAsJSON = (catalogues) => {
catalogues.forEach(catalogue =>
fs.writeFileSync(
`./${DIST_DIR}/${catalogue.name}-output.json`,
JSON.stringify(catalogue.questions, null, 2)
)
);
};
/*
* Anki supports HTML
* => We can leverage that, to make the questions look nicer
*/
const saveForAnki = (catalogues) => {
catalogues.forEach(catalogue => {
const questions = catalogue.questions.map(question => {
const q = '<h1>' + question.question + '</h1>';
const possibleAnswers = question.answers
.map(answer => '<p>' + answer + '</p>')
.join('');
const front = '<div>' + q + possibleAnswers + '</div>';
const back = question.correctAnswer;
return [front.replace(';', ',.'), back].join(';');
});
fs.writeFileSync(
`./${DIST_DIR}/${catalogue.name}-output-anki.csv`,
questions.join('\n')
);
})
};
const saveCustomCSV = (catalogues) => {
catalogues.forEach(catalogue => {
const questions = catalogue.questions.map(question => {
const q = question.question;
const [ a1, a2, a3, a4 ] = question.answers
.map(a => a.replace(/,/g, '\\,'))
.map(a => a.replace(/ß/g, 'ss'))
.map(a => a.replace(/ä/g, 'ae'))
.map(a => a.replace(/Ä/g, 'AE'))
.map(a => a.replace(/ö/g, 'oe'))
.map(a => a.replace(/ü/g, 'ue'))
.map(a => a.replace(/Ü/g, 'UE'));
let correctAnswer;
switch(question.correctAnswer) {
case 'A': correctAnswer = '1'; break;
case 'B': correctAnswer = '2'; break;
case 'C': correctAnswer = '3'; break;
case 'D': correctAnswer = '4'; break;
default: break;
}
return [q, a1, a2, a3, a4, null, null, null, null, null, null, correctAnswer].join(',');
});
fs.writeFileSync(
`./${catalogue.name}-output-custom.csv`,
questions.join('\n')
);
})
};
const catalogues = [
'flugpraxis',
'luftrecht',
'meteorologie',
'technik',
].map(name => (
{
name,
questions: getCatalogue(name)
}
));
// console.log(catalogues);
// saveCustomCSV(catalogues);
// saveCataloguesAsJSON(catalogues);
saveForAnki(catalogues);