@torrent-tv/proxy 2.73.1 → 2.74.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1453 -1437
- package/CLAUDE.md +165 -160
- package/docs/container-architecture.md +192 -184
- package/package.json +1 -1
- package/routes/api/subtitles/get.js +205 -205
- package/services/container/Container.js +400 -135
- package/services/container/ContainerFactory.js +55 -31
- package/services/container/MatroskaContainer.js +1166 -516
- package/services/container/Mp4Container.js +898 -392
- package/services/container/SubtitleFileContainer.js +323 -261
- package/services/controllers/SubtitleController.js +128 -127
- package/services/delivery-probe.js +64 -6
- package/services/hls-session-manager.js +32 -35
- package/services/language-detect.js +174 -228
- package/services/playback-planner.js +747 -747
- package/services/produced-index.js +300 -0
- package/services/torrent-worker/subtitle-cues.js +549 -633
- package/services/tracks/TextSubtitleTrack.js +287 -47
- package/services/tracks/index.js +14 -14
- package/test/delivery-probe.test.js +67 -0
- package/test/matroska-blocks.test.js +0 -0
- package/test/mp4-subtitles.test.js +173 -127
- package/test/produced-index.test.js +188 -0
- package/test/subtitle-cue-framing.test.js +200 -202
- package/test/subtitle-cue-walk.test.js +369 -0
- package/test/subtitle-defaults.test.js +97 -97
- package/test/subtitle-language.test.js +252 -252
- package/test/subtitle-track-numbering.test.js +370 -370
- package/services/container-index/matroska-blocks.js +0 -202
- package/services/container-index/matroska-subtitles.js +0 -372
- package/services/container-index/mp4-subtitles.js +0 -404
- package/services/subtitle-convert.js +0 -144
- package/services/subtitle-defaults.js +0 -157
- package/services/tracks/subtitle-markup.js +0 -104
|
@@ -1,228 +1,174 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file Content-based subtitle language detection (proxy side).
|
|
3
|
-
*
|
|
4
|
-
* Uses `franc` (n-gram / trigram frequency against per-language reference
|
|
5
|
-
* profiles — MIT). Runs on the proxy where the full subtitle text and
|
|
6
|
-
* node_modules live, so no detection model ships to the browser. Detection is
|
|
7
|
-
* restricted to a curated set of plausible subtitle languages via franc's
|
|
8
|
-
* `only` option: this both maps ISO 639-3 → ISO 639-1 + English name and
|
|
9
|
-
* avoids exotic false positives on short text (e.g. English mis-detected as
|
|
10
|
-
* Scots). Returns null when franc is not confident (too little text, or
|
|
11
|
-
* undetermined).
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import { franc } from "franc";
|
|
15
|
-
|
|
16
|
-
/** ISO 639-3 (franc output) → { code: ISO 639-1 / BCP-47, name }. Curated allowlist. */
|
|
17
|
-
const LANG_3_TO_1 = {
|
|
18
|
-
eng: { code: "en", name: "English" },
|
|
19
|
-
rus: { code: "ru", name: "Russian" },
|
|
20
|
-
ukr: { code: "uk", name: "Ukrainian" },
|
|
21
|
-
bel: { code: "be", name: "Belarusian" },
|
|
22
|
-
jpn: { code: "ja", name: "Japanese" },
|
|
23
|
-
kor: { code: "ko", name: "Korean" },
|
|
24
|
-
cmn: { code: "zh", name: "Chinese" },
|
|
25
|
-
spa: { code: "es", name: "Spanish" },
|
|
26
|
-
fra: { code: "fr", name: "French" },
|
|
27
|
-
deu: { code: "de", name: "German" },
|
|
28
|
-
ita: { code: "it", name: "Italian" },
|
|
29
|
-
por: { code: "pt", name: "Portuguese" },
|
|
30
|
-
pol: { code: "pl", name: "Polish" },
|
|
31
|
-
nld: { code: "nl", name: "Dutch" },
|
|
32
|
-
arb: { code: "ar", name: "Arabic" },
|
|
33
|
-
tur: { code: "tr", name: "Turkish" },
|
|
34
|
-
vie: { code: "vi", name: "Vietnamese" },
|
|
35
|
-
tha: { code: "th", name: "Thai" },
|
|
36
|
-
hin: { code: "hi", name: "Hindi" },
|
|
37
|
-
ind: { code: "id", name: "Indonesian" },
|
|
38
|
-
zlm: { code: "ms", name: "Malay" },
|
|
39
|
-
ces: { code: "cs", name: "Czech" },
|
|
40
|
-
slk: { code: "sk", name: "Slovak" },
|
|
41
|
-
ron: { code: "ro", name: "Romanian" },
|
|
42
|
-
hun: { code: "hu", name: "Hungarian" },
|
|
43
|
-
srp: { code: "sr", name: "Serbian" },
|
|
44
|
-
hrv: { code: "hr", name: "Croatian" },
|
|
45
|
-
bul: { code: "bg", name: "Bulgarian" },
|
|
46
|
-
ell: { code: "el", name: "Greek" },
|
|
47
|
-
heb: { code: "he", name: "Hebrew" },
|
|
48
|
-
dan: { code: "da", name: "Danish" },
|
|
49
|
-
fin: { code: "fi", name: "Finnish" },
|
|
50
|
-
nob: { code: "no", name: "Norwegian" },
|
|
51
|
-
swe: { code: "sv", name: "Swedish" },
|
|
52
|
-
fas: { code: "fa", name: "Persian" }
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
const ONLY = Object.keys(LANG_3_TO_1);
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* The least text, in characters, that supports an answer of each language.
|
|
59
|
-
*
|
|
60
|
-
* MEASURED, not chosen — `research/franc-boundary-2026-09-02.md`. Method:
|
|
61
|
-
* Wikipedia extracts per language (deliberately NOT the UDHR, which is what
|
|
62
|
-
* franc's own profiles are built from and would read optimistically), 120
|
|
63
|
-
* windows cut at random from them at each length of a ladder from 40 to 1300
|
|
64
|
-
* characters, and the figure recorded is the shortest length from which franc
|
|
65
|
-
* answered correctly in at least 95 % of trials AND kept doing so at every
|
|
66
|
-
* longer length measured.
|
|
67
|
-
*
|
|
68
|
-
* Why it is per language rather than one number: the answer is not equally hard
|
|
69
|
-
* to reach, and the spread is fivefold. Greek and Korean settle at 40
|
|
70
|
-
* characters because their script settles it; English needs 130; Russian and
|
|
71
|
-
* Czech need 650, because each competes with neighbours in this very list for
|
|
72
|
-
* the same trigrams — Russian with Bulgarian, Serbian and Ukrainian, Czech with
|
|
73
|
-
* Slovak.
|
|
74
|
-
*
|
|
75
|
-
* Two languages are deliberately ABSENT. Swedish and Chinese did not settle
|
|
76
|
-
* anywhere in the ladder on the corpora collected, so no figure for them is
|
|
77
|
-
* measured and none is invented; they take the fallback below.
|
|
78
|
-
*
|
|
79
|
-
* A language with no entry gets the WORST measured figure. That is the
|
|
80
|
-
* conservative reading and it is still a measurement rather than a guess: it
|
|
81
|
-
* says "no better than the hardest language we have measured".
|
|
82
|
-
*
|
|
83
|
-
* @type {Record<string, number>}
|
|
84
|
-
*/
|
|
85
|
-
const LEAST_TEXT = {
|
|
86
|
-
bel: 100,
|
|
87
|
-
bul: 80,
|
|
88
|
-
ces: 650,
|
|
89
|
-
deu: 200,
|
|
90
|
-
ell: 40,
|
|
91
|
-
eng: 130,
|
|
92
|
-
fra: 80,
|
|
93
|
-
heb: 60,
|
|
94
|
-
ita: 160,
|
|
95
|
-
kor: 40,
|
|
96
|
-
nld: 130,
|
|
97
|
-
pol: 200,
|
|
98
|
-
por: 200,
|
|
99
|
-
rus: 650,
|
|
100
|
-
spa: 100,
|
|
101
|
-
srp: 100,
|
|
102
|
-
tur: 160,
|
|
103
|
-
ukr: 250
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
/** The worst measured figure, used for any language not in the table. */
|
|
107
|
-
const LEAST_TEXT_WORST = Math.max(...Object.values(LEAST_TEXT), 0);
|
|
108
|
-
|
|
109
|
-
/** franc's own floor: below this it is not asked at all. */
|
|
110
|
-
const FRANC_FLOOR = 15;
|
|
111
|
-
|
|
112
|
-
/** One space between words, nothing else — the form every figure above is in. */
|
|
113
|
-
function normalise(text) {
|
|
114
|
-
return typeof text === "string" ? text.replace(/\s+/g, " ").trim() : "";
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* franc's answer for this text, or null when it has none.
|
|
119
|
-
*
|
|
120
|
-
* @param {string} text
|
|
121
|
-
* @returns {string | null} ISO 639-3.
|
|
122
|
-
*/
|
|
123
|
-
function ask(text) {
|
|
124
|
-
if (text.length < FRANC_FLOOR) {
|
|
125
|
-
return null;
|
|
126
|
-
}
|
|
127
|
-
const iso3 = franc(text, { only: ONLY, minLength: FRANC_FLOOR });
|
|
128
|
-
return iso3 === "und" ? null : iso3;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Best-effort detect the language of subtitle text.
|
|
133
|
-
*
|
|
134
|
-
* **Give this the words a viewer reads and nothing else.** franc scores letter
|
|
135
|
-
* trigrams over the whole string it is handed, so anything around the words
|
|
136
|
-
* competes with them. An ASS file is markup by half: measured 2026-09-01 on
|
|
137
|
-
* `[HorribleSubs] Drifters - 03 [1080p].ass`, 5040 Latin characters of Aegisub
|
|
138
|
-
* headers, style and font names, `Format:`/`Dialogue:` field prefixes and
|
|
139
|
-
* `{\…}` override groups against 5983 Cyrillic characters of dialogue —
|
|
140
|
-
* `franc(the file) = eng`, `franc(the dialogue) = rus`. The proxy had already
|
|
141
|
-
* built the markup-free text and detected on the file anyway, so a Russian
|
|
142
|
-
* track was offered to the viewer as English.
|
|
143
|
-
*
|
|
144
|
-
* `detectLanguageFromVtt` below is the safe entry point for a whole document;
|
|
145
|
-
* this one is for text that is already only text.
|
|
146
|
-
*
|
|
147
|
-
* @param {string} text - Subtitle text with no markup left in it.
|
|
148
|
-
* @returns {{ code: string, name: string } | null} Detected language, or null when uncertain.
|
|
149
|
-
*/
|
|
150
|
-
export function detectLanguage(text) {
|
|
151
|
-
const words = normalise(text);
|
|
152
|
-
if (words.length < FRANC_FLOOR) {
|
|
153
|
-
return null;
|
|
154
|
-
}
|
|
155
|
-
const candidate = ask(words);
|
|
156
|
-
if (candidate === null) {
|
|
157
|
-
return null;
|
|
158
|
-
}
|
|
159
|
-
// Enough text to support THIS answer. The figure is the language's own,
|
|
160
|
-
// because the languages are not alike: Russian shares its trigrams with
|
|
161
|
-
// Bulgarian, Serbian and Ukrainian and needs several times what English does.
|
|
162
|
-
if (words.length < (LEAST_TEXT[candidate] ?? LEAST_TEXT_WORST)) {
|
|
163
|
-
return null;
|
|
164
|
-
}
|
|
165
|
-
// And an answer that does not survive losing half the text was an accident of
|
|
166
|
-
// where the text happened to stop, not a reading of it. Free — franc costs
|
|
167
|
-
// about 2 ms whatever the size, measured — and it needs no figure of its own,
|
|
168
|
-
// because the test is taken on the text in hand.
|
|
169
|
-
const middle = Math.floor(words.length / 2);
|
|
170
|
-
if (ask(words.slice(0, middle)) !== candidate || ask(words.slice(middle)) !== candidate) {
|
|
171
|
-
return null;
|
|
172
|
-
}
|
|
173
|
-
return LANG_3_TO_1[candidate] ?? null;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* The words of a WebVTT document — what a viewer reads, with everything the
|
|
178
|
-
* format puts around them removed.
|
|
179
|
-
*
|
|
180
|
-
* A WebVTT document is a series of blocks separated by blank lines. A block
|
|
181
|
-
* that holds a timing line (`00:00:12.060 --> 00:00:13.270`) is a cue, and the
|
|
182
|
-
* lines after that timing line are its text; the lines before it are the cue's
|
|
183
|
-
* optional identifier. A block with NO timing line is the `WEBVTT` header or a
|
|
184
|
-
* `NOTE` / `STYLE` / `REGION` block, and none of those is anybody's language.
|
|
185
|
-
* That one rule removes the identifiers, the timings and the headers together.
|
|
186
|
-
*
|
|
187
|
-
* What is left can still carry WebVTT's own inline markup — `<v Speaker>`,
|
|
188
|
-
* `<i>`, `<c.yellow>` — and character references. Both are dropped: a speaker
|
|
189
|
-
* name and a class name are written in whatever language the releaser's tooling
|
|
190
|
-
* used, which is not the language of the film.
|
|
191
|
-
*
|
|
192
|
-
* @param {string} vtt - A WebVTT document.
|
|
193
|
-
* @returns {string} The cue text, blocks joined by newlines.
|
|
194
|
-
*/
|
|
195
|
-
export function cueTextOfVtt(vtt) {
|
|
196
|
-
if (typeof vtt !== "string") {
|
|
197
|
-
return "";
|
|
198
|
-
}
|
|
199
|
-
const blocks = vtt.replace(/\r\n/g, "\n").replace(/\r/g, "\n").split(/\n{2,}/);
|
|
200
|
-
const spoken = [];
|
|
201
|
-
for (const block of blocks) {
|
|
202
|
-
const lines = block.split("\n");
|
|
203
|
-
const timingAt = lines.findIndex((line) => line.includes("-->"));
|
|
204
|
-
if (timingAt < 0) {
|
|
205
|
-
continue;
|
|
206
|
-
}
|
|
207
|
-
for (const line of lines.slice(timingAt + 1)) {
|
|
208
|
-
spoken.push(line);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
return spoken
|
|
212
|
-
.join("\n")
|
|
213
|
-
.replace(/<[^>]*>/g, "")
|
|
214
|
-
// A character reference stands for one character and never for a word, so a
|
|
215
|
-
// space in its place keeps the neighbouring words apart and adds nothing.
|
|
216
|
-
.replace(/&[a-z]+;|&#\d+;|&#x[0-9a-f]+;/gi, " ")
|
|
217
|
-
.trim();
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Detect the language of a WebVTT document, reading only its cue text.
|
|
222
|
-
*
|
|
223
|
-
* @param {string} vtt - A WebVTT document.
|
|
224
|
-
* @returns {{ code: string, name: string } | null} Detected language, or null when uncertain.
|
|
225
|
-
*/
|
|
226
|
-
export function detectLanguageFromVtt(vtt) {
|
|
227
|
-
return detectLanguage(cueTextOfVtt(vtt));
|
|
228
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @file Content-based subtitle language detection (proxy side).
|
|
3
|
+
*
|
|
4
|
+
* Uses `franc` (n-gram / trigram frequency against per-language reference
|
|
5
|
+
* profiles — MIT). Runs on the proxy where the full subtitle text and
|
|
6
|
+
* node_modules live, so no detection model ships to the browser. Detection is
|
|
7
|
+
* restricted to a curated set of plausible subtitle languages via franc's
|
|
8
|
+
* `only` option: this both maps ISO 639-3 → ISO 639-1 + English name and
|
|
9
|
+
* avoids exotic false positives on short text (e.g. English mis-detected as
|
|
10
|
+
* Scots). Returns null when franc is not confident (too little text, or
|
|
11
|
+
* undetermined).
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { franc } from "franc";
|
|
15
|
+
|
|
16
|
+
/** ISO 639-3 (franc output) → { code: ISO 639-1 / BCP-47, name }. Curated allowlist. */
|
|
17
|
+
const LANG_3_TO_1 = {
|
|
18
|
+
eng: { code: "en", name: "English" },
|
|
19
|
+
rus: { code: "ru", name: "Russian" },
|
|
20
|
+
ukr: { code: "uk", name: "Ukrainian" },
|
|
21
|
+
bel: { code: "be", name: "Belarusian" },
|
|
22
|
+
jpn: { code: "ja", name: "Japanese" },
|
|
23
|
+
kor: { code: "ko", name: "Korean" },
|
|
24
|
+
cmn: { code: "zh", name: "Chinese" },
|
|
25
|
+
spa: { code: "es", name: "Spanish" },
|
|
26
|
+
fra: { code: "fr", name: "French" },
|
|
27
|
+
deu: { code: "de", name: "German" },
|
|
28
|
+
ita: { code: "it", name: "Italian" },
|
|
29
|
+
por: { code: "pt", name: "Portuguese" },
|
|
30
|
+
pol: { code: "pl", name: "Polish" },
|
|
31
|
+
nld: { code: "nl", name: "Dutch" },
|
|
32
|
+
arb: { code: "ar", name: "Arabic" },
|
|
33
|
+
tur: { code: "tr", name: "Turkish" },
|
|
34
|
+
vie: { code: "vi", name: "Vietnamese" },
|
|
35
|
+
tha: { code: "th", name: "Thai" },
|
|
36
|
+
hin: { code: "hi", name: "Hindi" },
|
|
37
|
+
ind: { code: "id", name: "Indonesian" },
|
|
38
|
+
zlm: { code: "ms", name: "Malay" },
|
|
39
|
+
ces: { code: "cs", name: "Czech" },
|
|
40
|
+
slk: { code: "sk", name: "Slovak" },
|
|
41
|
+
ron: { code: "ro", name: "Romanian" },
|
|
42
|
+
hun: { code: "hu", name: "Hungarian" },
|
|
43
|
+
srp: { code: "sr", name: "Serbian" },
|
|
44
|
+
hrv: { code: "hr", name: "Croatian" },
|
|
45
|
+
bul: { code: "bg", name: "Bulgarian" },
|
|
46
|
+
ell: { code: "el", name: "Greek" },
|
|
47
|
+
heb: { code: "he", name: "Hebrew" },
|
|
48
|
+
dan: { code: "da", name: "Danish" },
|
|
49
|
+
fin: { code: "fi", name: "Finnish" },
|
|
50
|
+
nob: { code: "no", name: "Norwegian" },
|
|
51
|
+
swe: { code: "sv", name: "Swedish" },
|
|
52
|
+
fas: { code: "fa", name: "Persian" }
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const ONLY = Object.keys(LANG_3_TO_1);
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The least text, in characters, that supports an answer of each language.
|
|
59
|
+
*
|
|
60
|
+
* MEASURED, not chosen — `research/franc-boundary-2026-09-02.md`. Method:
|
|
61
|
+
* Wikipedia extracts per language (deliberately NOT the UDHR, which is what
|
|
62
|
+
* franc's own profiles are built from and would read optimistically), 120
|
|
63
|
+
* windows cut at random from them at each length of a ladder from 40 to 1300
|
|
64
|
+
* characters, and the figure recorded is the shortest length from which franc
|
|
65
|
+
* answered correctly in at least 95 % of trials AND kept doing so at every
|
|
66
|
+
* longer length measured.
|
|
67
|
+
*
|
|
68
|
+
* Why it is per language rather than one number: the answer is not equally hard
|
|
69
|
+
* to reach, and the spread is fivefold. Greek and Korean settle at 40
|
|
70
|
+
* characters because their script settles it; English needs 130; Russian and
|
|
71
|
+
* Czech need 650, because each competes with neighbours in this very list for
|
|
72
|
+
* the same trigrams — Russian with Bulgarian, Serbian and Ukrainian, Czech with
|
|
73
|
+
* Slovak.
|
|
74
|
+
*
|
|
75
|
+
* Two languages are deliberately ABSENT. Swedish and Chinese did not settle
|
|
76
|
+
* anywhere in the ladder on the corpora collected, so no figure for them is
|
|
77
|
+
* measured and none is invented; they take the fallback below.
|
|
78
|
+
*
|
|
79
|
+
* A language with no entry gets the WORST measured figure. That is the
|
|
80
|
+
* conservative reading and it is still a measurement rather than a guess: it
|
|
81
|
+
* says "no better than the hardest language we have measured".
|
|
82
|
+
*
|
|
83
|
+
* @type {Record<string, number>}
|
|
84
|
+
*/
|
|
85
|
+
const LEAST_TEXT = {
|
|
86
|
+
bel: 100,
|
|
87
|
+
bul: 80,
|
|
88
|
+
ces: 650,
|
|
89
|
+
deu: 200,
|
|
90
|
+
ell: 40,
|
|
91
|
+
eng: 130,
|
|
92
|
+
fra: 80,
|
|
93
|
+
heb: 60,
|
|
94
|
+
ita: 160,
|
|
95
|
+
kor: 40,
|
|
96
|
+
nld: 130,
|
|
97
|
+
pol: 200,
|
|
98
|
+
por: 200,
|
|
99
|
+
rus: 650,
|
|
100
|
+
spa: 100,
|
|
101
|
+
srp: 100,
|
|
102
|
+
tur: 160,
|
|
103
|
+
ukr: 250
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/** The worst measured figure, used for any language not in the table. */
|
|
107
|
+
const LEAST_TEXT_WORST = Math.max(...Object.values(LEAST_TEXT), 0);
|
|
108
|
+
|
|
109
|
+
/** franc's own floor: below this it is not asked at all. */
|
|
110
|
+
const FRANC_FLOOR = 15;
|
|
111
|
+
|
|
112
|
+
/** One space between words, nothing else — the form every figure above is in. */
|
|
113
|
+
function normalise(text) {
|
|
114
|
+
return typeof text === "string" ? text.replace(/\s+/g, " ").trim() : "";
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* franc's answer for this text, or null when it has none.
|
|
119
|
+
*
|
|
120
|
+
* @param {string} text
|
|
121
|
+
* @returns {string | null} ISO 639-3.
|
|
122
|
+
*/
|
|
123
|
+
function ask(text) {
|
|
124
|
+
if (text.length < FRANC_FLOOR) {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
const iso3 = franc(text, { only: ONLY, minLength: FRANC_FLOOR });
|
|
128
|
+
return iso3 === "und" ? null : iso3;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Best-effort detect the language of subtitle text.
|
|
133
|
+
*
|
|
134
|
+
* **Give this the words a viewer reads and nothing else.** franc scores letter
|
|
135
|
+
* trigrams over the whole string it is handed, so anything around the words
|
|
136
|
+
* competes with them. An ASS file is markup by half: measured 2026-09-01 on
|
|
137
|
+
* `[HorribleSubs] Drifters - 03 [1080p].ass`, 5040 Latin characters of Aegisub
|
|
138
|
+
* headers, style and font names, `Format:`/`Dialogue:` field prefixes and
|
|
139
|
+
* `{\…}` override groups against 5983 Cyrillic characters of dialogue —
|
|
140
|
+
* `franc(the file) = eng`, `franc(the dialogue) = rus`. The proxy had already
|
|
141
|
+
* built the markup-free text and detected on the file anyway, so a Russian
|
|
142
|
+
* track was offered to the viewer as English.
|
|
143
|
+
*
|
|
144
|
+
* `detectLanguageFromVtt` below is the safe entry point for a whole document;
|
|
145
|
+
* this one is for text that is already only text.
|
|
146
|
+
*
|
|
147
|
+
* @param {string} text - Subtitle text with no markup left in it.
|
|
148
|
+
* @returns {{ code: string, name: string } | null} Detected language, or null when uncertain.
|
|
149
|
+
*/
|
|
150
|
+
export function detectLanguage(text) {
|
|
151
|
+
const words = normalise(text);
|
|
152
|
+
if (words.length < FRANC_FLOOR) {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
const candidate = ask(words);
|
|
156
|
+
if (candidate === null) {
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
// Enough text to support THIS answer. The figure is the language's own,
|
|
160
|
+
// because the languages are not alike: Russian shares its trigrams with
|
|
161
|
+
// Bulgarian, Serbian and Ukrainian and needs several times what English does.
|
|
162
|
+
if (words.length < (LEAST_TEXT[candidate] ?? LEAST_TEXT_WORST)) {
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
// And an answer that does not survive losing half the text was an accident of
|
|
166
|
+
// where the text happened to stop, not a reading of it. Free — franc costs
|
|
167
|
+
// about 2 ms whatever the size, measured — and it needs no figure of its own,
|
|
168
|
+
// because the test is taken on the text in hand.
|
|
169
|
+
const middle = Math.floor(words.length / 2);
|
|
170
|
+
if (ask(words.slice(0, middle)) !== candidate || ask(words.slice(middle)) !== candidate) {
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
return LANG_3_TO_1[candidate] ?? null;
|
|
174
|
+
}
|