-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWordReferenceUtils.js
More file actions
74 lines (68 loc) · 2.03 KB
/
WordReferenceUtils.js
File metadata and controls
74 lines (68 loc) · 2.03 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
const wordReferenceApi = require("wordreference-api");
const fs = require("fs");
const {
LANGUAGE_FROM,
LANGUAGE_TO,
NO_RESULTS_FILE,
OUTPUT_FILE
} = require("./constants");
class WordReferenceUtils {
static createBatches(data) {
let batches = [];
while (data.length > 0) {
batches.push(data.splice(0, 15));
}
return batches;
}
static determineIfFinishedProcessing(currentBatch, batches) {
if (currentBatch >= batches.length - 1) {
console.log("Done translating! Data available in output.csv");
return true;
}
return false;
}
static formatTranslations(translations) {
return translations
.map(t => t.translations)
.slice(0, 2)
.map(t => t[0])
.map(t => t.to.trim())
.join(", ");
}
static async fetchData({ line, sourceLanguage = LANGUAGE_FROM, targetLanguage = LANGUAGE_TO, outputFile = OUTPUT_FILE, noResultsFile = NO_RESULTS_FILE }) {
if(!fs.existsSync(outputFile) || !fs.existsSync(noResultsFile)) {
console.error('Must enter valid output file and no results file!');
return;
}
const hint = `(${line[0]})`;
const response = await wordReferenceApi(
encodeURI(line.normalize("NFD").replace(/[\u0300-\u036f]/g, "")),
sourceLanguage,
targetLanguage
).then(result => {
const { translations } = result;
if (translations.length === 0) {
console.error(`No translations found for word: ${line}`);
fs.appendFile(noResultsFile, `; ${line} \n`, () => {});
return;
}
const firstTwoResults = WordReferenceUtils.formatTranslations(
translations
);
const dataToAppend = `${Array.from(
new Set(firstTwoResults.split(", "))
).join("/")}${hint}; ${line} \n`;
fs.appendFile(outputFile, dataToAppend, () => {});
});
return response;
}
static formatLines(data) {
return data
.split(";")
.map(line => line.replace("/n", "").trim())
.filter(line => line && line.length);
}
}
module.exports = {
WordReferenceUtils
};