dsh-cait 1.1.3 → 1.1.7
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 +186 -0
- package/README.ja.md +51 -14
- package/README.md +137 -30
- package/README.zh-CN.md +51 -14
- package/client/components.js +2033 -48
- package/client/entry.js +2 -0
- package/client/helpers.js +12 -0
- package/client/locales.js +658 -8
- package/client/styles.js +111 -4
- package/client.js +2814 -58
- package/lib/aligner.js +6 -6
- package/lib/analysis.js +88 -0
- package/lib/api.js +46 -0
- package/lib/brief.js +275 -0
- package/lib/docx.js +155 -26
- package/lib/formats.js +37 -0
- package/lib/glossary.js +20 -4
- package/lib/import-export.js +212 -13
- package/lib/libraries.js +156 -0
- package/lib/library-export.js +7 -1
- package/lib/localization.js +1098 -0
- package/lib/lock.js +30 -0
- package/lib/notes.js +144 -0
- package/lib/persistence.js +54 -7
- package/lib/pptx.js +33 -1
- package/lib/pre-translate.js +62 -45
- package/lib/propagate.js +3 -0
- package/lib/qa.js +94 -4
- package/lib/replace.js +243 -0
- package/lib/review.js +131 -0
- package/lib/segment-edit.js +199 -0
- package/lib/store-utils.js +33 -6
- package/lib/store.js +507 -47
- package/lib/subtitles.js +213 -0
- package/lib/term-extract.js +105 -3
- package/lib/terms.js +4 -2
- package/lib/tm-maintenance.js +157 -0
- package/lib/tm.js +4 -1
- package/lib/tools.js +294 -0
- package/lib/xlsx.js +176 -66
- package/package.json +1 -1
package/lib/lock.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { now } from './store-utils.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A locked segment is a review boundary: MT, replacement and editing may not
|
|
5
|
+
* change its target until the user unlocks it. The flag lives on the segment
|
|
6
|
+
* so it survives reload and travels with exports that carry segment metadata.
|
|
7
|
+
*/
|
|
8
|
+
export const lockMethods = {
|
|
9
|
+
setSegmentLocked({ documentId, index, locked = true } = {}) {
|
|
10
|
+
const doc = this.requireDocument(documentId)
|
|
11
|
+
const segment = this.findSegment(doc, index)
|
|
12
|
+
const next = Boolean(locked)
|
|
13
|
+
if (Boolean(segment.locked) === next) {
|
|
14
|
+
return { documentId: doc.id, index: segment.index, locked: next, unchanged: true }
|
|
15
|
+
}
|
|
16
|
+
const previous = segment.locked
|
|
17
|
+
const previousUpdatedAt = segment.updatedAt
|
|
18
|
+
segment.locked = next
|
|
19
|
+
segment.updatedAt = now()
|
|
20
|
+
this.touch()
|
|
21
|
+
try {
|
|
22
|
+
this.persist()
|
|
23
|
+
} catch (error) {
|
|
24
|
+
segment.locked = previous
|
|
25
|
+
segment.updatedAt = previousUpdatedAt
|
|
26
|
+
throw error
|
|
27
|
+
}
|
|
28
|
+
return { documentId: doc.id, index: segment.index, locked: next }
|
|
29
|
+
},
|
|
30
|
+
}
|
package/lib/notes.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { CatError } from './errors.js'
|
|
2
|
+
import { makeId, now } from './store-utils.js'
|
|
3
|
+
|
|
4
|
+
const MAX_NOTE_CHARS = 2000
|
|
5
|
+
const MAX_NOTES = 50
|
|
6
|
+
|
|
7
|
+
function cleanNoteAuthor(author) {
|
|
8
|
+
const value = String(author ?? 'user').trim().slice(0, 40)
|
|
9
|
+
return value || 'user'
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Notes are review context, not translation state: they never affect TM,
|
|
14
|
+
* confirmation, QA or export. Keeping them as a small array on the segment
|
|
15
|
+
* lets a reviewer leave more than one comment and lets the agent add one
|
|
16
|
+
* without overwriting the human's note.
|
|
17
|
+
*/
|
|
18
|
+
export function normalizeNotes(raw) {
|
|
19
|
+
if (!Array.isArray(raw)) return []
|
|
20
|
+
const seen = new Set()
|
|
21
|
+
const notes = []
|
|
22
|
+
for (const item of raw) {
|
|
23
|
+
if (!item || typeof item !== 'object') continue
|
|
24
|
+
const text = String(item.text ?? '').trim()
|
|
25
|
+
if (!text) continue
|
|
26
|
+
let id = typeof item.id === 'string' && /^n_[A-Za-z0-9-]{4,40}$/.test(item.id) ? item.id : makeId('n')
|
|
27
|
+
while (seen.has(id)) id = makeId('n')
|
|
28
|
+
seen.add(id)
|
|
29
|
+
notes.push({
|
|
30
|
+
id,
|
|
31
|
+
text: text.slice(0, MAX_NOTE_CHARS),
|
|
32
|
+
author: cleanNoteAuthor(item.author),
|
|
33
|
+
createdAt: typeof item.createdAt === 'string' ? item.createdAt : now(),
|
|
34
|
+
updatedAt: typeof item.updatedAt === 'string' ? item.updatedAt : now(),
|
|
35
|
+
})
|
|
36
|
+
if (notes.length >= MAX_NOTES) break
|
|
37
|
+
}
|
|
38
|
+
return notes
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function noteView(note) {
|
|
42
|
+
return { ...note }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function notesView(notes) {
|
|
46
|
+
return (Array.isArray(notes) ? notes : []).map(noteView)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export const noteMethods = {
|
|
50
|
+
notesForSegment({ documentId, index } = {}) {
|
|
51
|
+
const doc = this.requireDocument(documentId)
|
|
52
|
+
const segment = this.findSegment(doc, index)
|
|
53
|
+
return { documentId: doc.id, index, notes: notesView(segment.notes) }
|
|
54
|
+
},
|
|
55
|
+
|
|
56
|
+
addSegmentNote({ documentId, index, text, author = 'user' } = {}) {
|
|
57
|
+
const doc = this.requireDocument(documentId)
|
|
58
|
+
const segment = this.findSegment(doc, index)
|
|
59
|
+
const value = String(text ?? '').trim()
|
|
60
|
+
if (!value) throw new CatError('EMPTY_NOTE', '备注内容不能为空。')
|
|
61
|
+
if (Array.from(value).length > MAX_NOTE_CHARS) {
|
|
62
|
+
throw new CatError('NOTE_TOO_LONG', `备注不能超过 ${MAX_NOTE_CHARS} 个字符。`)
|
|
63
|
+
}
|
|
64
|
+
const notes = Array.isArray(segment.notes) ? segment.notes : []
|
|
65
|
+
if (notes.length >= MAX_NOTES) {
|
|
66
|
+
throw new CatError('TOO_MANY_NOTES', `每个句段最多 ${MAX_NOTES} 条备注。`)
|
|
67
|
+
}
|
|
68
|
+
const stamp = now()
|
|
69
|
+
const note = {
|
|
70
|
+
id: makeId('n'),
|
|
71
|
+
text: value.slice(0, MAX_NOTE_CHARS),
|
|
72
|
+
author: cleanNoteAuthor(author),
|
|
73
|
+
createdAt: stamp,
|
|
74
|
+
updatedAt: stamp,
|
|
75
|
+
}
|
|
76
|
+
const previous = notes.map((item) => ({ ...item }))
|
|
77
|
+
const previousUpdatedAt = segment.updatedAt
|
|
78
|
+
notes.push(note)
|
|
79
|
+
segment.notes = notes
|
|
80
|
+
segment.updatedAt = stamp
|
|
81
|
+
try {
|
|
82
|
+
this.touch()
|
|
83
|
+
this.persist()
|
|
84
|
+
} catch (error) {
|
|
85
|
+
segment.notes = previous
|
|
86
|
+
segment.updatedAt = previousUpdatedAt
|
|
87
|
+
throw error
|
|
88
|
+
}
|
|
89
|
+
return { note: noteView(note), notes: notesView(notes) }
|
|
90
|
+
},
|
|
91
|
+
|
|
92
|
+
updateSegmentNote({ documentId, index, noteId, text } = {}) {
|
|
93
|
+
const doc = this.requireDocument(documentId)
|
|
94
|
+
const segment = this.findSegment(doc, index)
|
|
95
|
+
const notes = Array.isArray(segment.notes) ? segment.notes : []
|
|
96
|
+
const note = notes.find((item) => item.id === noteId)
|
|
97
|
+
if (!note) throw new CatError('NOTE_NOT_FOUND', '找不到这条备注。')
|
|
98
|
+
const value = String(text ?? '').trim()
|
|
99
|
+
if (!value) throw new CatError('EMPTY_NOTE', '备注内容不能为空。')
|
|
100
|
+
if (Array.from(value).length > MAX_NOTE_CHARS) {
|
|
101
|
+
throw new CatError('NOTE_TOO_LONG', `备注不能超过 ${MAX_NOTE_CHARS} 个字符。`)
|
|
102
|
+
}
|
|
103
|
+
const previous = { text: note.text, updatedAt: note.updatedAt }
|
|
104
|
+
const previousSegmentUpdatedAt = segment.updatedAt
|
|
105
|
+
const stamp = now()
|
|
106
|
+
note.text = value.slice(0, MAX_NOTE_CHARS)
|
|
107
|
+
note.updatedAt = stamp
|
|
108
|
+
segment.updatedAt = stamp
|
|
109
|
+
try {
|
|
110
|
+
this.touch()
|
|
111
|
+
this.persist()
|
|
112
|
+
} catch (error) {
|
|
113
|
+
note.text = previous.text
|
|
114
|
+
note.updatedAt = previous.updatedAt
|
|
115
|
+
segment.updatedAt = previousSegmentUpdatedAt
|
|
116
|
+
throw error
|
|
117
|
+
}
|
|
118
|
+
return { note: noteView(note), notes: notesView(notes) }
|
|
119
|
+
},
|
|
120
|
+
|
|
121
|
+
deleteSegmentNote({ documentId, index, noteId } = {}) {
|
|
122
|
+
const doc = this.requireDocument(documentId)
|
|
123
|
+
const segment = this.findSegment(doc, index)
|
|
124
|
+
const notes = Array.isArray(segment.notes) ? segment.notes : []
|
|
125
|
+
const at = notes.findIndex((item) => item.id === noteId)
|
|
126
|
+
if (at < 0) throw new CatError('NOTE_NOT_FOUND', '找不到这条备注。')
|
|
127
|
+
const previous = notes.slice()
|
|
128
|
+
const previousSegmentUpdatedAt = segment.updatedAt
|
|
129
|
+
notes.splice(at, 1)
|
|
130
|
+
segment.notes = notes
|
|
131
|
+
segment.updatedAt = now()
|
|
132
|
+
try {
|
|
133
|
+
this.touch()
|
|
134
|
+
this.persist()
|
|
135
|
+
} catch (error) {
|
|
136
|
+
segment.notes = previous
|
|
137
|
+
segment.updatedAt = previousSegmentUpdatedAt
|
|
138
|
+
throw error
|
|
139
|
+
}
|
|
140
|
+
return { notes: notesView(notes) }
|
|
141
|
+
},
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export { MAX_NOTE_CHARS, MAX_NOTES }
|
package/lib/persistence.js
CHANGED
|
@@ -4,7 +4,9 @@ import { join, resolve } from 'node:path'
|
|
|
4
4
|
import { CatError } from './errors.js'
|
|
5
5
|
import { normalizeText } from './similarity.js'
|
|
6
6
|
import { contextHashOf, normalizeContext } from './context.js'
|
|
7
|
-
import {
|
|
7
|
+
import { normalizeBrief } from './brief.js'
|
|
8
|
+
import { normalizeNotes } from './notes.js'
|
|
9
|
+
import { documentFingerprint, DEFAULT_GLOSSARY_ID, DEFAULT_GLOSSARY_NAME, DEFAULT_SETTINGS, DEFAULT_TM_ID, DEFAULT_TM_NAME, MAX_SEGMENTS, MAX_STORE_BYTES, SEGMENT_ORIGINS, SEGMENT_STATUSES, STORE_FILENAME, STORE_VERSION, clamp, clampInt, cleanFormatMeta, cleanName, cleanSegmentMeta, makeId, normalizeSegmentState, now, restoreLibraries, restoreMountIds, restoreSettings, restoredDate, restoredLang, restoredText } from './store-utils.js'
|
|
8
10
|
|
|
9
11
|
export const persistenceMethods = {
|
|
10
12
|
load() {
|
|
@@ -61,12 +63,7 @@ export const persistenceMethods = {
|
|
|
61
63
|
}
|
|
62
64
|
// v1 and v2 predate the three-state model: their segments only carried
|
|
63
65
|
// pending/confirmed, so their target text maps onto `translated`.
|
|
64
|
-
if (
|
|
65
|
-
payload.version !== 1 &&
|
|
66
|
-
payload.version !== 2 &&
|
|
67
|
-
payload.version !== 3 &&
|
|
68
|
-
payload.version !== STORE_VERSION
|
|
69
|
-
) {
|
|
66
|
+
if (![1, 2, 3, 4, 5, 6, STORE_VERSION].includes(payload.version)) {
|
|
70
67
|
throw new Error(`不兼容的数据版本: ${String(payload.version ?? 'missing')}`)
|
|
71
68
|
}
|
|
72
69
|
if (!Array.isArray(payload.documents) || !Array.isArray(payload.tm) || !Array.isArray(payload.glossary)) {
|
|
@@ -84,6 +81,7 @@ export const persistenceMethods = {
|
|
|
84
81
|
|
|
85
82
|
const sourceLang = restoredLang(rawDoc.sourceLang, 'en')
|
|
86
83
|
const targetLang = restoredLang(rawDoc.targetLang, 'zh-CN')
|
|
84
|
+
const segmentUids = new Set()
|
|
87
85
|
const segments = rawDoc.segments.map((rawSegment, index) => {
|
|
88
86
|
if (!rawSegment || typeof rawSegment !== 'object') {
|
|
89
87
|
throw new Error(`文档 ${rawDoc.id} 的第 ${index + 1} 个句段不是对象`)
|
|
@@ -98,11 +96,18 @@ export const persistenceMethods = {
|
|
|
98
96
|
? 'manual'
|
|
99
97
|
: null
|
|
100
98
|
const meta = cleanSegmentMeta(rawSegment.meta)
|
|
99
|
+
let uid =
|
|
100
|
+
typeof rawSegment.uid === 'string' && /^u_[A-Za-z0-9-]{4,40}$/.test(rawSegment.uid)
|
|
101
|
+
? rawSegment.uid
|
|
102
|
+
: makeId('u')
|
|
103
|
+
while (segmentUids.has(uid)) uid = makeId('u')
|
|
104
|
+
segmentUids.add(uid)
|
|
101
105
|
const segment = {
|
|
102
106
|
id:
|
|
103
107
|
typeof rawSegment.id === 'string' && rawSegment.id
|
|
104
108
|
? rawSegment.id
|
|
105
109
|
: `s${index + 1}`,
|
|
110
|
+
uid,
|
|
106
111
|
source,
|
|
107
112
|
target,
|
|
108
113
|
// normalizeSegmentState re-derives this from target/suggestion; the
|
|
@@ -116,6 +121,8 @@ export const persistenceMethods = {
|
|
|
116
121
|
? rawSegment.suggestionReason.slice(0, 500)
|
|
117
122
|
: '',
|
|
118
123
|
paragraph: Number.isInteger(rawSegment.paragraph) ? rawSegment.paragraph : index,
|
|
124
|
+
notes: normalizeNotes(rawSegment.notes),
|
|
125
|
+
locked: Boolean(rawSegment.locked),
|
|
119
126
|
...(meta ? { meta } : {}),
|
|
120
127
|
confirmedAt: confirmed ? restoredDate(rawSegment.confirmedAt) : null,
|
|
121
128
|
updatedAt: restoredDate(rawSegment.updatedAt),
|
|
@@ -132,6 +139,16 @@ export const persistenceMethods = {
|
|
|
132
139
|
cursor: clampInt(rawDoc.cursor, 0, segments.length - 1, 0),
|
|
133
140
|
createdAt: restoredDate(rawDoc.createdAt),
|
|
134
141
|
updatedAt: restoredDate(rawDoc.updatedAt),
|
|
142
|
+
tmIds: Array.isArray(rawDoc.tmIds)
|
|
143
|
+
? [...new Set(rawDoc.tmIds.filter((id) => typeof id === 'string' && id.trim()).map((id) => id.trim()))]
|
|
144
|
+
: null,
|
|
145
|
+
glossaryIds: Array.isArray(rawDoc.glossaryIds)
|
|
146
|
+
? [...new Set(rawDoc.glossaryIds.filter((id) => typeof id === 'string' && id.trim()).map((id) => id.trim()))]
|
|
147
|
+
: null,
|
|
148
|
+
defaultWriteTmId:
|
|
149
|
+
typeof rawDoc.defaultWriteTmId === 'string' && rawDoc.defaultWriteTmId.trim()
|
|
150
|
+
? rawDoc.defaultWriteTmId.trim()
|
|
151
|
+
: null,
|
|
135
152
|
format: typeof rawDoc.format === 'string' ? rawDoc.format.slice(0, 24) : '',
|
|
136
153
|
fingerprint:
|
|
137
154
|
typeof rawDoc.fingerprint === 'string' && rawDoc.fingerprint
|
|
@@ -140,6 +157,8 @@ export const persistenceMethods = {
|
|
|
140
157
|
}
|
|
141
158
|
const formatMeta = cleanFormatMeta(rawDoc.formatMeta)
|
|
142
159
|
if (formatMeta) doc.formatMeta = formatMeta
|
|
160
|
+
const brief = normalizeBrief(rawDoc.brief)
|
|
161
|
+
if (brief) doc.brief = brief
|
|
143
162
|
const sourceDocx = rawDoc.sourceDocx
|
|
144
163
|
if (
|
|
145
164
|
sourceDocx &&
|
|
@@ -168,6 +187,20 @@ export const persistenceMethods = {
|
|
|
168
187
|
name: cleanName(sourcePptx.name, 'source.pptx'),
|
|
169
188
|
}
|
|
170
189
|
}
|
|
190
|
+
const sourceXlsx = rawDoc.sourceXlsx
|
|
191
|
+
if (
|
|
192
|
+
sourceXlsx &&
|
|
193
|
+
typeof sourceXlsx === 'object' &&
|
|
194
|
+
sourceXlsx.kind === 'file' &&
|
|
195
|
+
typeof sourceXlsx.asset === 'string' &&
|
|
196
|
+
/^sources\/[A-Za-z0-9_.-]+\.xlsx$/.test(sourceXlsx.asset)
|
|
197
|
+
) {
|
|
198
|
+
doc.sourceXlsx = {
|
|
199
|
+
kind: 'file',
|
|
200
|
+
asset: sourceXlsx.asset,
|
|
201
|
+
name: cleanName(sourceXlsx.name, 'source.xlsx'),
|
|
202
|
+
}
|
|
203
|
+
}
|
|
171
204
|
const sourceEpub = rawDoc.sourceEpub
|
|
172
205
|
if (
|
|
173
206
|
sourceEpub &&
|
|
@@ -235,6 +268,8 @@ export const persistenceMethods = {
|
|
|
235
268
|
? restoredLang(rawEntry.sourceLang, '')
|
|
236
269
|
: '',
|
|
237
270
|
caseSensitive: Boolean(rawEntry.caseSensitive),
|
|
271
|
+
forbidden: Boolean(rawEntry.forbidden),
|
|
272
|
+
preferred: Boolean(rawEntry.preferred),
|
|
238
273
|
createdAt: restoredDate(rawEntry.createdAt),
|
|
239
274
|
updatedAt: restoredDate(rawEntry.updatedAt),
|
|
240
275
|
})
|
|
@@ -278,6 +313,18 @@ export const persistenceMethods = {
|
|
|
278
313
|
this.tmLibraries = tmLibraries
|
|
279
314
|
this.glossaryLibraries = glossaryLibraries
|
|
280
315
|
this.mounts = { tmIds, glossaryIds, defaultTmId, defaultGlossaryId }
|
|
316
|
+
// Library ids may have been removed while the document was closed. Drop
|
|
317
|
+
// dangling bindings rather than failing the whole restore.
|
|
318
|
+
const knownTm = new Set(tmLibraries.map((library) => library.tmId))
|
|
319
|
+
const knownGlossary = new Set(glossaryLibraries.map((library) => library.glossaryId))
|
|
320
|
+
for (const doc of docs.values()) {
|
|
321
|
+
if (doc.tmIds) doc.tmIds = doc.tmIds.filter((id) => knownTm.has(id))
|
|
322
|
+
if (doc.glossaryIds) doc.glossaryIds = doc.glossaryIds.filter((id) => knownGlossary.has(id))
|
|
323
|
+
const effective = doc.tmIds ?? tmIds
|
|
324
|
+
if (doc.defaultWriteTmId && !effective.includes(doc.defaultWriteTmId)) {
|
|
325
|
+
doc.defaultWriteTmId = null
|
|
326
|
+
}
|
|
327
|
+
}
|
|
281
328
|
const hasOpenDocIds = Array.isArray(payload.openDocIds)
|
|
282
329
|
const openDocIds = []
|
|
283
330
|
for (const id of hasOpenDocIds ? payload.openDocIds : []) {
|
package/lib/pptx.js
CHANGED
|
@@ -15,6 +15,13 @@ function slideNames(entries) {
|
|
|
15
15
|
.sort((left, right) => Number(left.match(/(\d+)\.xml$/)[1]) - Number(right.match(/(\d+)\.xml$/)[1]))
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
+
function noteNames(entries) {
|
|
19
|
+
return entries
|
|
20
|
+
.map((entry) => entry.name)
|
|
21
|
+
.filter((name) => /^ppt\/notesSlides\/notesSlide\d+\.xml$/i.test(name))
|
|
22
|
+
.sort((left, right) => Number(left.match(/(\d+)\.xml$/)[1]) - Number(right.match(/(\d+)\.xml$/)[1]))
|
|
23
|
+
}
|
|
24
|
+
|
|
18
25
|
function paragraphText(paragraph) {
|
|
19
26
|
return [...String(paragraph).matchAll(/<a:t\b[^>]*>([\s\S]*?)<\/a:t>/gi)]
|
|
20
27
|
.map((match) => decodeXml(match[1]))
|
|
@@ -47,8 +54,33 @@ export function parsePptxDetailed(buffer) {
|
|
|
47
54
|
})
|
|
48
55
|
}
|
|
49
56
|
}
|
|
57
|
+
for (const name of noteNames(entries)) {
|
|
58
|
+
const xml = entries.find((entry) => entry.name === name)?.data.toString('utf8') ?? ''
|
|
59
|
+
let index = 0
|
|
60
|
+
for (const match of xml.matchAll(/<a:p\b[^>]*>([\s\S]*?)<\/a:p>/gi)) {
|
|
61
|
+
const source = paragraphText(match[1]).trim()
|
|
62
|
+
const noteParagraph = index
|
|
63
|
+
index += 1
|
|
64
|
+
if (!source) continue
|
|
65
|
+
segments.push({
|
|
66
|
+
source,
|
|
67
|
+
target: '',
|
|
68
|
+
paragraph: segments.length,
|
|
69
|
+
meta: {
|
|
70
|
+
format: 'pptx',
|
|
71
|
+
pptxSlide: name,
|
|
72
|
+
pptxParagraph: noteParagraph,
|
|
73
|
+
context: 'notes',
|
|
74
|
+
},
|
|
75
|
+
})
|
|
76
|
+
}
|
|
77
|
+
}
|
|
50
78
|
if (!segments.length) throw fail('PPTX_NO_UNITS', 'PPTX 中没有可用的正文文本。')
|
|
51
|
-
return {
|
|
79
|
+
return {
|
|
80
|
+
format: 'pptx',
|
|
81
|
+
segments,
|
|
82
|
+
warnings: noteNames(entries).length ? ['已导入演讲者备注(notesSlides)。'] : [],
|
|
83
|
+
}
|
|
52
84
|
}
|
|
53
85
|
|
|
54
86
|
function patchParagraph(paragraph, text) {
|
package/lib/pre-translate.js
CHANGED
|
@@ -1,30 +1,10 @@
|
|
|
1
1
|
import { CatError } from './errors.js'
|
|
2
|
-
import { DEFAULT_TM_ID, clampInt, normalizeSegmentState, now } from './store-utils.js'
|
|
2
|
+
import { DEFAULT_TM_ID, MT_BATCH_MAX, clampInt, normalizeSegmentState, now } from './store-utils.js'
|
|
3
3
|
import { normalizeText } from './similarity.js'
|
|
4
4
|
import { contextHashOf, contextOf } from './context.js'
|
|
5
5
|
|
|
6
6
|
export const PRETRANSLATE_BLOCK_MAX = 32
|
|
7
7
|
export const PRETRANSLATE_DEFAULT_COUNT = 8
|
|
8
|
-
export const PRETRANSLATE_CONCURRENCY = 2
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Run `worker` over `items` with a small pool. Machine translation is network
|
|
12
|
-
* bound, so a little concurrency buys most of the speed without hammering the
|
|
13
|
-
* provider or the event loop.
|
|
14
|
-
*/
|
|
15
|
-
async function runPool(items, limit, worker) {
|
|
16
|
-
const results = new Array(items.length)
|
|
17
|
-
let cursor = 0
|
|
18
|
-
const runners = Array.from({ length: Math.min(limit, items.length) }, async () => {
|
|
19
|
-
while (cursor < items.length) {
|
|
20
|
-
const position = cursor
|
|
21
|
-
cursor += 1
|
|
22
|
-
results[position] = await worker(items[position], position)
|
|
23
|
-
}
|
|
24
|
-
})
|
|
25
|
-
await Promise.all(runners)
|
|
26
|
-
return results
|
|
27
|
-
}
|
|
28
8
|
|
|
29
9
|
export const preTranslateMethods = {
|
|
30
10
|
/**
|
|
@@ -34,7 +14,7 @@ export const preTranslateMethods = {
|
|
|
34
14
|
* be resolved as ICE first and as a plain 100% match second.
|
|
35
15
|
*/
|
|
36
16
|
tmExactIndex(doc) {
|
|
37
|
-
const mounted = new Set(this.
|
|
17
|
+
const mounted = new Set(this.effectiveTmIds(doc))
|
|
38
18
|
const index = new Map()
|
|
39
19
|
for (const entry of this.tm) {
|
|
40
20
|
if (!mounted.has(entry.tmId || DEFAULT_TM_ID)) continue
|
|
@@ -96,6 +76,10 @@ export const preTranslateMethods = {
|
|
|
96
76
|
|
|
97
77
|
for (let index = start; index < end; index += 1) {
|
|
98
78
|
const segment = doc.segments[index]
|
|
79
|
+
if (segment.locked) {
|
|
80
|
+
skipped.push({ index, reason: 'locked' })
|
|
81
|
+
continue
|
|
82
|
+
}
|
|
99
83
|
if (segment.status === 'confirmed') {
|
|
100
84
|
skipped.push({ index, reason: 'confirmed' })
|
|
101
85
|
continue
|
|
@@ -108,17 +92,57 @@ export const preTranslateMethods = {
|
|
|
108
92
|
}
|
|
109
93
|
|
|
110
94
|
const failed = []
|
|
111
|
-
const filled =
|
|
112
|
-
|
|
95
|
+
const filled = []
|
|
96
|
+
const mtWork = []
|
|
97
|
+
|
|
98
|
+
const apply = (segment, text, origin) => {
|
|
99
|
+
segment.target = text
|
|
100
|
+
// Keep the baseline the machine produced: the review pane shows it in
|
|
101
|
+
// the suggestion column so the human can compare against their edit.
|
|
102
|
+
segment.suggestion = text
|
|
103
|
+
segment.origin = origin
|
|
104
|
+
segment.updatedAt = now()
|
|
105
|
+
normalizeSegmentState(segment)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Resolve exact TM matches first. Only the remaining segments go to the
|
|
109
|
+
// provider, and those are sent in one JSON batch so they share the brief
|
|
110
|
+
// and the same terminology decisions.
|
|
111
|
+
for (const item of work) {
|
|
112
|
+
const match = this.tmEntryForSegment(tmIndex, doc, item.index, item.segment)
|
|
113
|
+
if (match.entry && match.entry.target.trim()) {
|
|
114
|
+
const text = match.entry.target
|
|
115
|
+
apply(item.segment, text, 'tm')
|
|
116
|
+
filled.push({ index: item.index, status: item.segment.status, origin: 'tm', ice: match.ice })
|
|
117
|
+
} else if (translationMode === 'tm') {
|
|
118
|
+
filled.push({ index: item.index, reason: 'no-tm-match' })
|
|
119
|
+
} else {
|
|
120
|
+
mtWork.push({ ...item, match })
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
for (let offset = 0; offset < mtWork.length; offset += MT_BATCH_MAX) {
|
|
125
|
+
const chunk = mtWork.slice(offset, offset + MT_BATCH_MAX)
|
|
126
|
+
let batch = new Map()
|
|
113
127
|
try {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
128
|
+
const result = await this.mtSuggestBatch({
|
|
129
|
+
documentId: doc.id,
|
|
130
|
+
items: chunk.map(({ index, segment }) => ({ index, text: segment.source })),
|
|
131
|
+
})
|
|
132
|
+
batch = new Map((result?.items ?? []).map((item) => [item.index, item.text]))
|
|
133
|
+
} catch {
|
|
134
|
+
// A malformed or failed batch must not lose the whole block: retry the
|
|
135
|
+
// affected segments one by one below.
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
for (const { index, segment, match } of chunk) {
|
|
139
|
+
const batched = String(batch.get(index) ?? '').trim()
|
|
140
|
+
if (batched) {
|
|
141
|
+
apply(segment, batched, 'mt')
|
|
142
|
+
filled.push({ index, status: segment.status, origin: 'mt', ice: false })
|
|
143
|
+
continue
|
|
144
|
+
}
|
|
145
|
+
try {
|
|
122
146
|
const result = await this.mtSuggest({
|
|
123
147
|
text: segment.source,
|
|
124
148
|
documentId: doc.id,
|
|
@@ -126,22 +150,15 @@ export const preTranslateMethods = {
|
|
|
126
150
|
sourceLang: doc.sourceLang,
|
|
127
151
|
targetLang: doc.targetLang,
|
|
128
152
|
})
|
|
129
|
-
text = String(result?.text ?? '').trim()
|
|
153
|
+
const text = String(result?.text ?? '').trim()
|
|
130
154
|
if (!text) throw new CatError('MT_EMPTY', '机器翻译没有返回译文。', 502)
|
|
155
|
+
apply(segment, text, 'mt')
|
|
156
|
+
filled.push({ index, status: segment.status, origin: 'mt', ice: false })
|
|
157
|
+
} catch (error) {
|
|
158
|
+
failed.push({ index, code: error?.code || 'PRETRANSLATE_FAILED', message: error?.message || String(error) })
|
|
131
159
|
}
|
|
132
|
-
segment.target = text
|
|
133
|
-
// Keep the baseline the machine produced: the review pane shows it in
|
|
134
|
-
// the suggestion column so the human can compare against their edit.
|
|
135
|
-
segment.suggestion = text
|
|
136
|
-
segment.origin = origin
|
|
137
|
-
segment.updatedAt = now()
|
|
138
|
-
normalizeSegmentState(segment)
|
|
139
|
-
return { index, status: segment.status, origin, ice: origin === 'tm' && match.ice }
|
|
140
|
-
} catch (error) {
|
|
141
|
-
failed.push({ index, code: error?.code || 'PRETRANSLATE_FAILED', message: error?.message || String(error) })
|
|
142
|
-
return null
|
|
143
160
|
}
|
|
144
|
-
}
|
|
161
|
+
}
|
|
145
162
|
|
|
146
163
|
const segments = filled.filter(Boolean)
|
|
147
164
|
const unchanged = segments.filter((item) => item.reason)
|
package/lib/propagate.js
CHANGED
|
@@ -62,6 +62,7 @@ export const propagateMethods = {
|
|
|
62
62
|
for (const item of group) {
|
|
63
63
|
if (item === index) continue
|
|
64
64
|
const segment = doc.segments[item]
|
|
65
|
+
if (segment.locked) continue
|
|
65
66
|
if (segment.status === 'confirmed') continue
|
|
66
67
|
if (segment.target === source) continue
|
|
67
68
|
segment.target = source
|
|
@@ -88,6 +89,7 @@ export const propagateMethods = {
|
|
|
88
89
|
for (const item of group) {
|
|
89
90
|
if (item === index) continue
|
|
90
91
|
const sibling = doc.segments[item]
|
|
92
|
+
if (sibling.locked) continue
|
|
91
93
|
if (sibling.status === 'confirmed') continue
|
|
92
94
|
if (sibling.target === segment.target) continue
|
|
93
95
|
sibling.target = segment.target
|
|
@@ -110,6 +112,7 @@ export const propagateMethods = {
|
|
|
110
112
|
for (const index of indices) {
|
|
111
113
|
if (index === reference.index) continue
|
|
112
114
|
const segment = doc.segments[index]
|
|
115
|
+
if (segment.locked) continue
|
|
113
116
|
if (segment.status === 'confirmed') continue
|
|
114
117
|
if (segment.target === reference.segment.target) continue
|
|
115
118
|
segment.target = reference.segment.target
|
package/lib/qa.js
CHANGED
|
@@ -5,6 +5,23 @@ import { clampInt, DEFAULT_GLOSSARY_ID } from './store-utils.js'
|
|
|
5
5
|
const TOKEN_PATTERN = /\{\d+\}/g
|
|
6
6
|
const NUMBER_PATTERN = /\d+(?:[.,]\d+)*/g
|
|
7
7
|
|
|
8
|
+
/** Severity is derived from the code so the report stays storage-free. */
|
|
9
|
+
const RISK_BY_CODE = {
|
|
10
|
+
EMPTY_TARGET: 'high',
|
|
11
|
+
PLACEHOLDER_MISSING: 'high',
|
|
12
|
+
NUMBER_MISMATCH: 'high',
|
|
13
|
+
TERM_FORBIDDEN: 'high',
|
|
14
|
+
UNTRANSLATED_RESIDUE: 'high',
|
|
15
|
+
SUBTITLE_OVERLAP: 'high',
|
|
16
|
+
TERM_NOT_USED: 'medium',
|
|
17
|
+
INCONSISTENT_TARGET: 'medium',
|
|
18
|
+
LENGTH_DRIFT: 'low',
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function riskOf(code) {
|
|
22
|
+
return RISK_BY_CODE[code] || 'low'
|
|
23
|
+
}
|
|
24
|
+
|
|
8
25
|
/** Inline tokens are `{0}`, `{1}`… — the only markup CAIT keeps verbatim. */
|
|
9
26
|
function tokensIn(text) {
|
|
10
27
|
return String(text ?? '').match(TOKEN_PATTERN) || []
|
|
@@ -74,14 +91,15 @@ export const qaMethods = {
|
|
|
74
91
|
|
|
75
92
|
getQaReport({ documentId } = {}) {
|
|
76
93
|
const doc = this.requireDocument(documentId)
|
|
77
|
-
const mounted = new Set(this.
|
|
94
|
+
const mounted = new Set(this.effectiveGlossaryIds(doc))
|
|
78
95
|
const terms = this.glossary.filter(
|
|
79
96
|
(entry) =>
|
|
80
97
|
mounted.has(entry.glossaryId || DEFAULT_GLOSSARY_ID) &&
|
|
81
98
|
(!entry.sourceLang || entry.sourceLang === doc.sourceLang),
|
|
82
99
|
)
|
|
83
100
|
const issues = []
|
|
84
|
-
const push = (index, code, message
|
|
101
|
+
const push = (index, code, message, extra = {}) =>
|
|
102
|
+
issues.push({ index, code, message, risk: riskOf(code), ...extra })
|
|
85
103
|
let checked = 0
|
|
86
104
|
let withText = 0
|
|
87
105
|
// Group by source so a repeated sentence with two different renderings is
|
|
@@ -123,7 +141,31 @@ export const qaMethods = {
|
|
|
123
141
|
const used = entry.caseSensitive
|
|
124
142
|
? target.includes(entry.target)
|
|
125
143
|
: target.toLocaleLowerCase().includes(entry.target.toLocaleLowerCase())
|
|
126
|
-
if (
|
|
144
|
+
if (entry.forbidden) {
|
|
145
|
+
if (used) {
|
|
146
|
+
push(index, 'TERM_FORBIDDEN', `译文使用了禁用术语「${entry.source} → ${entry.target}」。`)
|
|
147
|
+
}
|
|
148
|
+
continue
|
|
149
|
+
}
|
|
150
|
+
if (!used) {
|
|
151
|
+
push(
|
|
152
|
+
index,
|
|
153
|
+
'TERM_NOT_USED',
|
|
154
|
+
entry.preferred
|
|
155
|
+
? `必用术语「${entry.source} → ${entry.target}」未在译文中出现。`
|
|
156
|
+
: `术语「${entry.source} → ${entry.target}」未在译文中出现。`,
|
|
157
|
+
{ term: entry.source, termTarget: entry.target, preferred: Boolean(entry.preferred) },
|
|
158
|
+
)
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// A very short or very long rendering is a cheap signal that content was
|
|
163
|
+
// dropped or padded. The bounds are deliberately wide: EN→ZH legitimately
|
|
164
|
+
// shrinks, while inflected languages can grow.
|
|
165
|
+
const sourceLength = [...String(segment.source ?? '').replace(/\s+/g, '')].length
|
|
166
|
+
const targetLength = [...target.replace(/\s+/g, '')].length
|
|
167
|
+
if (sourceLength >= 10 && targetLength > 0 && (targetLength < sourceLength * 0.2 || targetLength > sourceLength * 5)) {
|
|
168
|
+
push(index, 'LENGTH_DRIFT', `译文长度与源文差异较大(${sourceLength} → ${targetLength} 字符)。`)
|
|
127
169
|
}
|
|
128
170
|
|
|
129
171
|
// Copying the source into the target is the most common disguise for a
|
|
@@ -149,11 +191,57 @@ export const qaMethods = {
|
|
|
149
191
|
}
|
|
150
192
|
}
|
|
151
193
|
|
|
194
|
+
// Subtitle cues are allowed to be adjacent, not overlapping. Do not touch
|
|
195
|
+
// the timing here: an overlap is a product decision for the translator.
|
|
196
|
+
if (doc.format === 'srt' || doc.format === 'vtt') {
|
|
197
|
+
let previous = null
|
|
198
|
+
for (let index = 0; index < doc.segments.length; index += 1) {
|
|
199
|
+
const segment = doc.segments[index]
|
|
200
|
+
const start = Number(segment.meta?.start)
|
|
201
|
+
const end = Number(segment.meta?.end)
|
|
202
|
+
if (!Number.isFinite(start) || !Number.isFinite(end) || end <= start) continue
|
|
203
|
+
if (previous && start < previous.end) {
|
|
204
|
+
push(index, 'SUBTITLE_OVERLAP', `第 ${index + 1} 条字幕与上一条时间轴重叠。`)
|
|
205
|
+
}
|
|
206
|
+
previous = { end }
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
152
210
|
const counts = issues.reduce(
|
|
153
211
|
(acc, issue) => ({ ...acc, [issue.code]: (acc[issue.code] || 0) + 1 }),
|
|
154
|
-
{
|
|
212
|
+
{
|
|
213
|
+
PLACEHOLDER_MISSING: 0,
|
|
214
|
+
NUMBER_MISMATCH: 0,
|
|
215
|
+
TERM_NOT_USED: 0,
|
|
216
|
+
TERM_FORBIDDEN: 0,
|
|
217
|
+
EMPTY_TARGET: 0,
|
|
218
|
+
UNTRANSLATED_RESIDUE: 0,
|
|
219
|
+
INCONSISTENT_TARGET: 0,
|
|
220
|
+
LENGTH_DRIFT: 0,
|
|
221
|
+
SUBTITLE_OVERLAP: 0,
|
|
222
|
+
},
|
|
155
223
|
)
|
|
156
224
|
|
|
225
|
+
// Collapse per-code findings into one review priority per segment. The UI
|
|
226
|
+
// sorts by this, so a number mismatch is never hidden behind a style note.
|
|
227
|
+
const byIndex = new Map()
|
|
228
|
+
for (const issue of issues) {
|
|
229
|
+
const bucket = byIndex.get(issue.index)
|
|
230
|
+
if (bucket) {
|
|
231
|
+
bucket.codes.add(issue.code)
|
|
232
|
+
if (issue.risk === 'high') bucket.risk = 'high'
|
|
233
|
+
else if (issue.risk === 'medium' && bucket.risk !== 'high') bucket.risk = 'medium'
|
|
234
|
+
} else {
|
|
235
|
+
byIndex.set(issue.index, { index: issue.index, risk: issue.risk, codes: new Set([issue.code]) })
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
const riskRank = { high: 0, medium: 1, low: 2 }
|
|
239
|
+
const risks = [...byIndex.values()]
|
|
240
|
+
.map((item) => ({ index: item.index, risk: item.risk, codes: [...item.codes] }))
|
|
241
|
+
.sort((a, b) => (riskRank[a.risk] ?? 3) - (riskRank[b.risk] ?? 3) || a.index - b.index)
|
|
242
|
+
const riskCounts = { high: 0, medium: 0, low: 0 }
|
|
243
|
+
for (const item of risks) riskCounts[item.risk] += 1
|
|
244
|
+
|
|
157
245
|
return {
|
|
158
246
|
documentId: doc.id,
|
|
159
247
|
total: doc.segments.length,
|
|
@@ -161,6 +249,8 @@ export const qaMethods = {
|
|
|
161
249
|
withText,
|
|
162
250
|
issueCount: issues.length,
|
|
163
251
|
counts,
|
|
252
|
+
risks,
|
|
253
|
+
riskCounts,
|
|
164
254
|
issues,
|
|
165
255
|
}
|
|
166
256
|
},
|