-
Notifications
You must be signed in to change notification settings - Fork 1
/
TartuNLP.js
41 lines (35 loc) · 1017 Bytes
/
TartuNLP.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
/**
* Homepage: https://github.com/TartuNLP/translation-api
* Demo: https://translate.ut.ee/
* API docs: https://api.tartunlp.ai/translation/docs
*/
class TartuNLP {
translate = (text, from, to) => {
return fetch('https://api.tartunlp.ai/translation/v2', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ text, src: from, tgt: to }),
})
.then((r) => r.json())
.then((r) => r.result);
};
translateBatch = (texts, from, to) =>
Promise.all(texts.map((text) => this.translate(text, from, to)));
getLengthLimit = () => 5000;
getRequestsTimeout = () => 300;
checkLimitExceeding = (text) => {
const textLength = !Array.isArray(text)
? text.length
: text.reduce((len, text) => len + text.length, 0);
return textLength - this.getLengthLimit();
};
static isSupportedAutoFrom = () => false;
// prettier-ignore
static getSupportedLanguages = () => [
"en", "et", "de", "lt", "lv",
"fi", "ru", "no", "hu", "se",
];
}
TartuNLP;