dsh-ai-subtitle 0.3.3 → 0.4.0
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/README.en.md +6 -1
- package/README.ja.md +3 -1
- package/README.ko.md +3 -1
- package/README.md +7 -2
- package/client.js +307 -20
- package/docs/REVIEW.md +4 -4
- package/index.js +54 -3
- package/package.json +1 -1
- package/python/worker.py +86 -5
- package/src/api.js +148 -4
- package/src/audio.js +20 -0
- package/src/cues.js +222 -9
- package/src/export-name.js +22 -0
- package/src/glossary.js +21 -1
- package/src/library.js +98 -0
- package/src/llm.js +209 -32
- package/src/quality.js +61 -0
- package/src/resplit.js +143 -0
- package/src/review.js +9 -5
package/src/cues.js
CHANGED
|
@@ -18,7 +18,7 @@ export function validateCues(cues, total, options = {}) {
|
|
|
18
18
|
const errors = []
|
|
19
19
|
if (cues.length === 0) return { ok: false, errors: ['no cues'] }
|
|
20
20
|
let cursor = Number(cues[0].from)
|
|
21
|
-
let
|
|
21
|
+
let previous = null
|
|
22
22
|
let lastTo = 0
|
|
23
23
|
for (const cue of cues) {
|
|
24
24
|
const from = Number(cue.from)
|
|
@@ -31,16 +31,18 @@ export function validateCues(cues, total, options = {}) {
|
|
|
31
31
|
errors.push(`invalid range ${from}-${to}`)
|
|
32
32
|
continue
|
|
33
33
|
}
|
|
34
|
-
|
|
35
|
-
//
|
|
36
|
-
const
|
|
37
|
-
|
|
34
|
+
// Consecutive cues with the same range are one sentence split into several
|
|
35
|
+
// subtitles; their explicit start/end carries the timing.
|
|
36
|
+
const repeats = previous !== null && from === previous.from && to === previous.to
|
|
37
|
+
const closesSplit = previous !== null && from === previous.from && to > previous.to
|
|
38
|
+
if (!repeats && !closesSplit && from !== cursor) {
|
|
38
39
|
errors.push(`expected from=${cursor}, got ${from} (…${String(cue.jp).slice(0, 20)})`)
|
|
39
40
|
}
|
|
40
|
-
if (
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
if (!repeats) {
|
|
42
|
+
cursor = Math.max(cursor, to + 1)
|
|
43
|
+
lastTo = Math.max(lastTo, to)
|
|
44
|
+
previous = { from, to }
|
|
45
|
+
}
|
|
44
46
|
}
|
|
45
47
|
// Export needs the whole file; an incremental write only needs a contiguous prefix.
|
|
46
48
|
if (options.requireFull !== false && lastTo !== total) errors.push(`coverage ends at ${lastTo}, expected ${total}`)
|
|
@@ -252,3 +254,214 @@ export function writeSrt(path, rows) {
|
|
|
252
254
|
writeFileSync(path, '\uFEFF' + toSrt(rows), 'utf8')
|
|
253
255
|
}
|
|
254
256
|
|
|
257
|
+
/**
|
|
258
|
+
* Merge adjacent short cues so a one-word answer does not become a subtitle of
|
|
259
|
+
* its own. Conservative by default: both sides must be short, close in time,
|
|
260
|
+
* and carry no manual timing or skip marker.
|
|
261
|
+
* @param cues - cues ordered by fragment range.
|
|
262
|
+
* @param fragments - asr fragments, for cue times and gaps.
|
|
263
|
+
* @param options - thresholds; the defaults match the panel's wording.
|
|
264
|
+
* @returns the merged list plus how many merges happened.
|
|
265
|
+
*/
|
|
266
|
+
export function mergeShortCues(cues, fragments, options = {}) {
|
|
267
|
+
const maxGapMs = options.maxGapMs ?? 800
|
|
268
|
+
const maxEachChars = options.maxEachChars ?? 10
|
|
269
|
+
const maxCombinedChars = options.maxCombinedChars ?? 24
|
|
270
|
+
const maxSpanMs = options.maxSpanMs ?? 6500
|
|
271
|
+
const startMs = (cue) => Math.round((cue.start !== undefined ? Number(cue.start) : fragments[cue.from - 1].start) * 1000)
|
|
272
|
+
const endMs = (cue) => Math.round((cue.end !== undefined ? Number(cue.end) : fragments[cue.to - 1].end) * 1000)
|
|
273
|
+
const mergeable = (cue) => !cue.skip && cue.start === undefined && cue.end === undefined && String(cue.zh || '').trim() !== ''
|
|
274
|
+
|
|
275
|
+
const merged = []
|
|
276
|
+
let count = 0
|
|
277
|
+
for (const cue of cues) {
|
|
278
|
+
const previous = merged[merged.length - 1]
|
|
279
|
+
if (
|
|
280
|
+
previous
|
|
281
|
+
&& mergeable(previous)
|
|
282
|
+
&& mergeable(cue)
|
|
283
|
+
&& Number(previous.to) + 1 === Number(cue.from)
|
|
284
|
+
&& startMs(cue) - endMs(previous) <= maxGapMs
|
|
285
|
+
&& endMs(cue) - startMs(previous) <= maxSpanMs
|
|
286
|
+
// Repeated lines are real speech: keep both subtitles instead of folding
|
|
287
|
+
// them into one.
|
|
288
|
+
&& String(previous.zh || '') !== String(cue.zh || '')
|
|
289
|
+
&& String(previous.jp || '') !== String(cue.jp || '')
|
|
290
|
+
&& String(previous.zh).length <= maxEachChars
|
|
291
|
+
&& String(cue.zh).length <= maxEachChars
|
|
292
|
+
&& String(previous.zh).length + String(cue.zh).length <= maxCombinedChars
|
|
293
|
+
) {
|
|
294
|
+
merged[merged.length - 1] = {
|
|
295
|
+
from: previous.from,
|
|
296
|
+
to: cue.to,
|
|
297
|
+
jp: joinJapanese(previous.jp, cue.jp),
|
|
298
|
+
zh: joinClauses(previous.zh, cue.zh),
|
|
299
|
+
}
|
|
300
|
+
count += 1
|
|
301
|
+
continue
|
|
302
|
+
}
|
|
303
|
+
merged.push({ ...cue })
|
|
304
|
+
}
|
|
305
|
+
return { cues: merged, merged: count }
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** Join two clauses, keeping a separator when the left side would otherwise run on. */
|
|
309
|
+
/** Same rule for Japanese: keep punctuation, add a period between clauses. */
|
|
310
|
+
function joinJapanese(left, right) {
|
|
311
|
+
const a = String(left || '').trim()
|
|
312
|
+
const b = String(right || '').trim()
|
|
313
|
+
if (!a) return b
|
|
314
|
+
if (!b) return a
|
|
315
|
+
if (/[、。?!…?!.,;:]$/.test(a) || /^[、。?!…?!.,;:]/.test(b)) return a + b
|
|
316
|
+
return a.length >= 3 || b.length >= 3 ? a + '。' + b : a + b
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function joinClauses(left, right) {
|
|
320
|
+
const a = String(left || '').trim()
|
|
321
|
+
const b = String(right || '').trim()
|
|
322
|
+
if (!a) return b
|
|
323
|
+
if (!b) return a
|
|
324
|
+
if (/[,。?!…、;:?!.,;:]$/.test(a) || /^[,。?!…、;:?!.,;:]/.test(b)) return a + b
|
|
325
|
+
return a.length >= 3 || b.length >= 3 ? a + '。' + b : a + b
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/** Split one text into chunks no longer than max, preferring punctuation and spaces. */
|
|
329
|
+
function chunkText(value, max) {
|
|
330
|
+
const tokens = value.match(/[^\s,。?!;、…:]+\s*|[,。?!;、…:]+\s*/g) || [value]
|
|
331
|
+
const chunks = []
|
|
332
|
+
let current = ''
|
|
333
|
+
for (const token of tokens) {
|
|
334
|
+
if (!current) {
|
|
335
|
+
current = token
|
|
336
|
+
continue
|
|
337
|
+
}
|
|
338
|
+
if ((current + token).trim().length <= max) current += token
|
|
339
|
+
else {
|
|
340
|
+
chunks.push(current.trim())
|
|
341
|
+
current = token
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
if (current.trim()) chunks.push(current.trim())
|
|
345
|
+
const out = []
|
|
346
|
+
for (const chunk of chunks) {
|
|
347
|
+
if (chunk.length <= max) {
|
|
348
|
+
out.push(chunk)
|
|
349
|
+
continue
|
|
350
|
+
}
|
|
351
|
+
let rest = chunk
|
|
352
|
+
while (rest.length > max) {
|
|
353
|
+
let cut = rest.lastIndexOf(' ', max)
|
|
354
|
+
if (cut <= 0) cut = max
|
|
355
|
+
out.push(rest.slice(0, cut).trim())
|
|
356
|
+
rest = rest.slice(cut).trim()
|
|
357
|
+
}
|
|
358
|
+
if (rest) out.push(rest)
|
|
359
|
+
}
|
|
360
|
+
return out
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/** Split into exactly count parts, cutting at the nearest space. */
|
|
364
|
+
function splitExact(value, count) {
|
|
365
|
+
const trimmed = value.trim()
|
|
366
|
+
const out = []
|
|
367
|
+
let start = 0
|
|
368
|
+
for (let index = 1; index < count; index += 1) {
|
|
369
|
+
const target = Math.round((trimmed.length * index) / count)
|
|
370
|
+
let cut = trimmed.lastIndexOf(' ', target)
|
|
371
|
+
if (cut <= start) cut = target
|
|
372
|
+
out.push(trimmed.slice(start, cut).trim())
|
|
373
|
+
start = cut
|
|
374
|
+
}
|
|
375
|
+
out.push(trimmed.slice(start).trim())
|
|
376
|
+
return out.filter(Boolean)
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/** Divide the Japanese text into count parts, preferring punctuation boundaries. */
|
|
380
|
+
function splitJapanese(value, count) {
|
|
381
|
+
const parts = value.match(/[^、。!?…]+[、。!?…]?/g) || [value]
|
|
382
|
+
if (parts.length === count) return parts
|
|
383
|
+
const out = []
|
|
384
|
+
let consumed = 0
|
|
385
|
+
for (let i = 0; i < count; i += 1) {
|
|
386
|
+
const end = Math.round((value.length * (i + 1)) / count)
|
|
387
|
+
out.push(value.slice(consumed, end))
|
|
388
|
+
consumed = end
|
|
389
|
+
}
|
|
390
|
+
return out
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Split over-long cues at punctuation and spread the fragment's time across the
|
|
395
|
+
* pieces by character count, so one ASR fragment of 30 seconds becomes several
|
|
396
|
+
* readable subtitles instead of one wall of text.
|
|
397
|
+
* @param cues - cues ordered by fragment range.
|
|
398
|
+
* @param fragments - asr fragments, for the original time span.
|
|
399
|
+
* @param options - maxZh (default 24), maxSeconds (default 10).
|
|
400
|
+
*/
|
|
401
|
+
export function splitLongCues(cues, fragments, options = {}) {
|
|
402
|
+
const maxZh = options.maxZh ?? 24
|
|
403
|
+
const result = []
|
|
404
|
+
let split = 0
|
|
405
|
+
for (const cue of cues) {
|
|
406
|
+
const zh = String(cue.zh || '')
|
|
407
|
+
const jp = String(cue.jp || '')
|
|
408
|
+
const manual = cue.start !== undefined || cue.end !== undefined
|
|
409
|
+
if (cue.skip || manual) {
|
|
410
|
+
result.push(cue)
|
|
411
|
+
continue
|
|
412
|
+
}
|
|
413
|
+
const start = fragments[Number(cue.from) - 1].start
|
|
414
|
+
const end = fragments[Number(cue.to) - 1].end
|
|
415
|
+
const span = end - start
|
|
416
|
+
const maxSeconds = options.maxSeconds ?? 10
|
|
417
|
+
const byText = Math.ceil(zh.length / maxZh)
|
|
418
|
+
const byTime = Math.ceil(span / maxSeconds)
|
|
419
|
+
const count = Math.max(byText, byTime)
|
|
420
|
+
if (count < 2) {
|
|
421
|
+
result.push(cue)
|
|
422
|
+
continue
|
|
423
|
+
}
|
|
424
|
+
const units = zh.split(/[\s,。?!;、…:]+/).filter(Boolean)
|
|
425
|
+
if (byText < 2 && units.length < 2 && (span <= 15 || zh.length < 8)) {
|
|
426
|
+
result.push(cue)
|
|
427
|
+
continue
|
|
428
|
+
}
|
|
429
|
+
let zhParts = []
|
|
430
|
+
let pieces = count
|
|
431
|
+
for (;;) {
|
|
432
|
+
zhParts = zh.length > maxZh
|
|
433
|
+
? (() => {
|
|
434
|
+
const chunks = chunkText(zh, maxZh)
|
|
435
|
+
return chunks.length >= pieces ? chunks : splitExact(zh, pieces)
|
|
436
|
+
})()
|
|
437
|
+
: splitExact(zh, pieces)
|
|
438
|
+
const total = zhParts.reduce((sum, part) => sum + part.length, 0)
|
|
439
|
+
const longest = Math.max(...zhParts.map((part) => part.length))
|
|
440
|
+
if ((longest / total) * span <= maxSeconds || pieces >= 12) break
|
|
441
|
+
pieces += 1
|
|
442
|
+
}
|
|
443
|
+
if (zhParts.length < 2) {
|
|
444
|
+
result.push(cue)
|
|
445
|
+
continue
|
|
446
|
+
}
|
|
447
|
+
const jpParts = splitJapanese(jp, zhParts.length)
|
|
448
|
+
const totalChars = zhParts.reduce((sum, part) => sum + part.length, 0)
|
|
449
|
+
const round = (value) => Number(value.toFixed(3))
|
|
450
|
+
let cursor = start
|
|
451
|
+
for (let index = 0; index < zhParts.length; index += 1) {
|
|
452
|
+
const share = (span * zhParts[index].length) / totalChars
|
|
453
|
+
const next = index === zhParts.length - 1 ? end : round(cursor + share)
|
|
454
|
+
result.push({
|
|
455
|
+
from: cue.from,
|
|
456
|
+
to: cue.to,
|
|
457
|
+
jp: jpParts[index] ?? '',
|
|
458
|
+
zh: zhParts[index],
|
|
459
|
+
start: round(cursor),
|
|
460
|
+
end: next,
|
|
461
|
+
})
|
|
462
|
+
cursor = next
|
|
463
|
+
}
|
|
464
|
+
split += zhParts.length - 1
|
|
465
|
+
}
|
|
466
|
+
return { cues: result, split }
|
|
467
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { existsSync, renameSync } from 'node:fs'
|
|
2
|
+
import { basename, dirname, extname, join } from 'node:path'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Rename an existing file to "name (1).ext" (Windows Explorer style), picking
|
|
6
|
+
* the first free number, so a new export can take the original name.
|
|
7
|
+
* @param target - the absolute path the new file will occupy.
|
|
8
|
+
* @returns the backup path, or null when the target did not exist.
|
|
9
|
+
*/
|
|
10
|
+
export function makeRoom(target) {
|
|
11
|
+
if (!existsSync(target)) return null
|
|
12
|
+
const ext = extname(target)
|
|
13
|
+
const base = basename(target, ext)
|
|
14
|
+
for (let index = 1; index <= 10000; index += 1) {
|
|
15
|
+
const candidate = join(dirname(target), `${base} (${index})${ext}`)
|
|
16
|
+
if (!existsSync(candidate)) {
|
|
17
|
+
renameSync(target, candidate)
|
|
18
|
+
return candidate
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
throw new Error(`cannot find a free name for ${target}`)
|
|
22
|
+
}
|
package/src/glossary.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
|
|
2
|
+
import { basename, dirname } from 'node:path'
|
|
3
|
+
import { loadLibrary } from './library.js'
|
|
2
4
|
import { join } from 'node:path'
|
|
3
5
|
|
|
4
6
|
/**
|
|
@@ -20,7 +22,7 @@ export function glossaryPath(projectDir) {
|
|
|
20
22
|
|
|
21
23
|
const isText = (value) => typeof value === 'string' && value.trim().length > 0
|
|
22
24
|
|
|
23
|
-
|
|
25
|
+
function readGlossary(path) {
|
|
24
26
|
const empty = { path, exists: false, strict: false, terms: [], corrections: [], errors: [] }
|
|
25
27
|
if (!path || !existsSync(path)) return empty
|
|
26
28
|
let data
|
|
@@ -60,6 +62,24 @@ export function loadGlossary(path) {
|
|
|
60
62
|
}
|
|
61
63
|
}
|
|
62
64
|
|
|
65
|
+
/**
|
|
66
|
+
* Load a project glossary and merge the shared library terms into it. A term in
|
|
67
|
+
* the project wins over the same spelling in the library.
|
|
68
|
+
* @param path - path to glossary.json (a bare path is read without merging).
|
|
69
|
+
*/
|
|
70
|
+
export function loadGlossary(path) {
|
|
71
|
+
const glossary = readGlossary(path)
|
|
72
|
+
if (path && basename(path) === 'glossary.json' && Array.isArray(glossary.terms)) {
|
|
73
|
+
const library = loadLibrary(dirname(path))
|
|
74
|
+
if (library.terms.length > 0) {
|
|
75
|
+
const known = new Set(glossary.terms.map((term) => term.jp))
|
|
76
|
+
const extra = library.terms.filter((term) => !known.has(term.jp))
|
|
77
|
+
if (extra.length > 0) glossary.terms = [...glossary.terms, ...extra]
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return glossary
|
|
81
|
+
}
|
|
82
|
+
|
|
63
83
|
/** Literal replacements, longest source first so a short rule cannot eat a long one. */
|
|
64
84
|
export function applyCorrections(text, corrections) {
|
|
65
85
|
let output = text
|
package/src/library.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
|
|
2
|
+
import { dirname, join, resolve } from 'node:path'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A shared library that lives next to the projects directory, so terms and
|
|
6
|
+
* background presets can be reused across projects.
|
|
7
|
+
* @param projectDir - any project directory under the projects root.
|
|
8
|
+
*/
|
|
9
|
+
export function libraryPath(projectDir) {
|
|
10
|
+
return join(dirname(resolve(projectDir)), '..', 'library.json')
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function emptyLibrary() {
|
|
14
|
+
return { version: 1, terms: [], hints: [] }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function loadLibrary(projectDir) {
|
|
18
|
+
const file = libraryPath(projectDir)
|
|
19
|
+
if (!existsSync(file)) return emptyLibrary()
|
|
20
|
+
try {
|
|
21
|
+
const data = JSON.parse(readFileSync(file, 'utf8'))
|
|
22
|
+
return {
|
|
23
|
+
version: 1,
|
|
24
|
+
terms: Array.isArray(data.terms) ? data.terms : [],
|
|
25
|
+
hints: Array.isArray(data.hints) ? data.hints : [],
|
|
26
|
+
}
|
|
27
|
+
} catch {
|
|
28
|
+
return emptyLibrary()
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function saveLibrary(projectDir, data) {
|
|
33
|
+
const file = libraryPath(projectDir)
|
|
34
|
+
writeFileSync(
|
|
35
|
+
file,
|
|
36
|
+
JSON.stringify({ version: 1, terms: data.terms || [], hints: data.hints || [] }, null, 1) + '\n',
|
|
37
|
+
'utf8',
|
|
38
|
+
)
|
|
39
|
+
return file
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Add or replace a term by its Japanese spelling. */
|
|
43
|
+
export function upsertTerm(projectDir, term) {
|
|
44
|
+
const library = loadLibrary(projectDir)
|
|
45
|
+
const jp = String(term.jp || '').trim()
|
|
46
|
+
const zh = String(term.zh || '').trim()
|
|
47
|
+
if (!jp || !zh) throw new Error('术语需要日文和中文')
|
|
48
|
+
const rest = library.terms.filter((item) => item.jp !== jp)
|
|
49
|
+
rest.push({ jp, zh, kind: term.kind || 'term' })
|
|
50
|
+
library.terms = rest
|
|
51
|
+
saveLibrary(projectDir, library)
|
|
52
|
+
return library
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function removeTerm(projectDir, jp) {
|
|
56
|
+
const library = loadLibrary(projectDir)
|
|
57
|
+
library.terms = library.terms.filter((item) => item.jp !== jp)
|
|
58
|
+
saveLibrary(projectDir, library)
|
|
59
|
+
return library
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Save the current project glossary entries into the shared library. */
|
|
63
|
+
export function absorbProjectTerms(projectDir, terms) {
|
|
64
|
+
const library = loadLibrary(projectDir)
|
|
65
|
+
for (const term of terms) {
|
|
66
|
+
const index = library.terms.findIndex((item) => item.jp === term.jp)
|
|
67
|
+
const next = { jp: term.jp, zh: term.zh, kind: term.kind || 'term' }
|
|
68
|
+
if (index >= 0) library.terms[index] = next
|
|
69
|
+
else library.terms.push(next)
|
|
70
|
+
}
|
|
71
|
+
saveLibrary(projectDir, library)
|
|
72
|
+
return library
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Save or replace a background preset under its name. */
|
|
76
|
+
export function saveHintPreset(projectDir, preset) {
|
|
77
|
+
const library = loadLibrary(projectDir)
|
|
78
|
+
const name = String(preset.name || '').trim()
|
|
79
|
+
if (!name) throw new Error('背景模板需要名称')
|
|
80
|
+
const next = {
|
|
81
|
+
id: String(preset.id || name).replace(/\s+/g, '-'),
|
|
82
|
+
name,
|
|
83
|
+
tags: Array.isArray(preset.tags) ? preset.tags : [],
|
|
84
|
+
note: String(preset.note || ''),
|
|
85
|
+
}
|
|
86
|
+
const index = library.hints.findIndex((item) => item.name === name)
|
|
87
|
+
if (index >= 0) library.hints[index] = next
|
|
88
|
+
else library.hints.push(next)
|
|
89
|
+
saveLibrary(projectDir, library)
|
|
90
|
+
return library
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function removeHintPreset(projectDir, name) {
|
|
94
|
+
const library = loadLibrary(projectDir)
|
|
95
|
+
library.hints = library.hints.filter((item) => item.name !== name)
|
|
96
|
+
saveLibrary(projectDir, library)
|
|
97
|
+
return library
|
|
98
|
+
}
|