-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
52 lines (48 loc) · 1.25 KB
/
index.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
/**
* Copyright (c) Myia 2023-2023 - All Rights Reserved
*/
import cld3 from './cld3.cjs';
/**
* Identify the languages in a text using the Compact Language Detector v3 neural network.
*
* Identification is less reliable on short texts.
*
* @example
* getLanguages('This piece of text is in English. Гэты тэкст на беларускай мове.');
* [
* {
* language: 'be',
* probability: 0.9173873066902161,
* is_reliable: true,
* proportion: 0.5853658318519592
* },
* {
* language: 'en',
* probability: 0.9999790191650391,
* is_reliable: true,
* proportion: 0.4146341383457184
* }
* ]
* @param {string} txt
*
* @return {null|Array<{language: string, probability: number, is_reliable: boolean, proportion: number}>}
*/
export const getLanguages = function (txt) {
const oracleResult = cld3.getLanguages(txt);
const bestResults = oracleResult.filter(
({ language, is_reliable }) => language !== 'und' && is_reliable
);
if (bestResults.length > 0) {
return bestResults;
}
for (const result of oracleResult) {
if (
result.language !== 'und' &&
result.probability > 0.5 &&
result.proportion === 1
) {
return [result];
}
}
return null;
};