-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslate-vocab.js
More file actions
77 lines (63 loc) · 2.5 KB
/
Copy pathtranslate-vocab.js
File metadata and controls
77 lines (63 loc) · 2.5 KB
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
const translate = require('translate-google');
const fs = require('fs');
const run = async () => {
const content = fs.readFileSync('../speaker-app/src-vocab.ts', 'utf8');
const dataStr = content.replace('export const vocabularyTopicsDummyData =', 'return');
const getFn = new Function(dataStr);
const englishTopics = getFn();
// We need 15 topics with 40 words each
const trimmedTopics = englishTopics.slice(0, 15).map(topic => ({
...topic,
words: topic.words.slice(0, 40)
}));
const langs = ['es', 'fr', 'de', 'pl'];
const output = [];
output.push(`export const getVocabularyData = (lang: string) => {`);
output.push(` if (lang === "en") return ${JSON.stringify(trimmedTopics, null, 2)};`);
for (const lang of langs) {
console.log(`Translating to ${lang}...`);
try {
const translatedTopics = JSON.parse(JSON.stringify(trimmedTopics));
const allTexts = [];
for (const t of translatedTopics) {
allTexts.push(t.title);
for (const w of t.words) {
allTexts.push(w.word);
w.examples.forEach(ex => allTexts.push(ex));
}
}
// Batch translate because Google might block large requests
const batchSize = 100;
let translatedTexts = [];
for (let i = 0; i < allTexts.length; i += batchSize) {
console.log(`Translating batch ${i} to ${i + batchSize}`);
const batch = allTexts.slice(i, i + batchSize);
const res = await translate(batch, {to: lang});
translatedTexts = translatedTexts.concat(res);
// Wait 1 second
await new Promise(r => setTimeout(r, 1000));
}
let textIdx = 0;
for (const t of translatedTopics) {
t.title = translatedTexts[textIdx++];
t.id = t.id.replace('en', lang); // replace en with es etc
for (const w of t.words) {
w.id = w.id.replace('en', lang);
w.word = translatedTexts[textIdx++];
for (let k = 0; k < w.examples.length; k++) {
w.examples[k] = translatedTexts[textIdx++];
}
w.transcription = `/${w.word}/`; // simple fallback transcription
}
}
output.push(` if (lang === "${lang}") return ${JSON.stringify(translatedTopics, null, 2)};`);
} catch(e) {
console.log('Error in', lang, e);
}
}
output.push(` return [];`);
output.push(`};`);
fs.writeFileSync('../speaker-app/constants/vocab.ts', output.join('\\n'));
console.log('Done writing vocab.ts');
};
run();