forked from Language-Research-Technology/caat-data-prep
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess-transcripts.js
More file actions
643 lines (534 loc) · 17.4 KB
/
Copy pathprocess-transcripts.js
File metadata and controls
643 lines (534 loc) · 17.4 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const { execFileSync } = require('child_process');
const { ROCrate } = require('ro-crate');
const Workbook = require('ro-crate-excel/lib/workbook.js');
const { unicodeName } = require('unicode-name');
function printUsage() {
console.log(`Usage: node process-transcripts.js --input <dir> --output <dir> [options]
Options:
--input, -i Directory containing .docx transcript files (default: ./input)
--output, -o Directory for generated CSV outputs (default: ./output)
--header-rows Number of leading rows to remove from each transcript (default: 0)
--footer-rows Number of trailing rows to remove from each transcript (default: 0)
--pre-start Start index for PRE rows (optional)
--pre-end End index for PRE rows (optional)
--post-start Start index for POST rows (optional)
--post-end End index for POST rows (optional)
--help, -h Show this help message
`);
}
function parseArgs(argv) {
const config = {
input: './input',
output: './output',
headerRows: 0,
footerRows: 0,
preStart: null,
preEnd: null,
postStart: null,
postEnd: null,
};
for (let i = 0; i < argv.length; i += 1) {
const arg = argv[i];
switch (arg) {
case '--help':
case '-h':
printUsage();
process.exit(0);
case '--input':
case '-i':
config.input = argv[++i];
break;
case '--output':
case '-o':
config.output = argv[++i];
break;
case '--header-rows':
config.headerRows = Number(argv[++i] || 0);
break;
case '--footer-rows':
config.footerRows = Number(argv[++i] || 0);
break;
case '--pre-start':
config.preStart = Number(argv[++i] || 0);
break;
case '--pre-end':
config.preEnd = Number(argv[++i] || 0);
break;
case '--post-start':
config.postStart = Number(argv[++i] || 0);
break;
case '--post-end':
config.postEnd = Number(argv[++i] || 0);
break;
default:
throw new Error(`Unknown argument: ${arg}`);
}
}
return config;
}
function ensurePythonDocxInstalled() {
try {
execFileSync('python3', ['-c', 'import docx'], { stdio: 'ignore' });
return true;
} catch (error) {
return false;
}
}
function extractParagraphs(docxPath) {
const result = execFileSync(
'python3',
[
'-c',
`
import json, sys
from docx import Document
doc = Document(sys.argv[1])
print(json.dumps([p.text for p in doc.paragraphs]))
`,
docxPath,
],
{ encoding: 'utf8' }
);
return JSON.parse(result);
}
function normalizeText(text) {
let normalized = String(text || '');
normalized = normalized.replace(/\r\n/g, '\n');
normalized = normalized.replace(/\r/g, '\n');
normalized = normalized.replace(/\u00A0/g, ' ');
normalized = normalized.replace(/^[\t ]+/gm, '\t');
normalized = normalized.replace(/^([A-Z]):[\t ]+/gm, '$1:\t');
normalized = normalized.replace(/^([A-Z])[\t ]+/gm, '$1:\t');
normalized = normalized.replace(/(\t.*) \t/gm, '$1 ');
normalized = normalized.replace(/^.*END OF TRANSCRIPT.*$/gm, '');
return normalized;
}
function mergeContinuationLines(text) {
let merged = text;
const protectedLines = new Set([
'Speakers:',
'PRELIMINARIES',
'MAIN',
'POSTLIMINARIES',
'Transcript:',
'Recording date:',
'Length of audio recording:',
'Length of video recording:',
'Transcriber:',
]);
for (let iteration = 0; iteration < 100; iteration += 1) {
const lines = merged.split('\n');
const repaired = [];
let changed = false;
for (let i = 0; i < lines.length; i += 1) {
const line = lines[i];
const trimmed = line.trim();
const isSpeakerLine = /^([A-Z][A-Z0-9]?\s*:|[A-Z][A-Z0-9]?:)\s*(\t|.*)$/.test(line);
if (protectedLines.has(trimmed)) {
repaired.push(line);
continue;
}
if (!isSpeakerLine && repaired.length > 0) {
const previous = repaired[repaired.length - 1];
const nextValue = previous.trimEnd() + ' ' + line.trim();
repaired[repaired.length - 1] = nextValue;
changed = true;
} else {
repaired.push(line);
}
}
const candidate = repaired.join('\n');
if (!changed || candidate === merged) {
merged = candidate;
break;
}
merged = candidate;
}
return merged;
}
function parseSpeakerBlock(lines, warnings) {
const speakers = new Map();
let inSpeakerSection = false;
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed) {
continue;
}
if (trimmed === 'Speakers:') {
inSpeakerSection = true;
continue;
}
if (inSpeakerSection && trimmed === 'PRELIMINARIES') {
inSpeakerSection = false;
break;
}
if (!inSpeakerSection) {
continue;
}
const speakerMatch = trimmed.match(/^([A-Z][A-Z0-9]?)\s*:\s*(.*)$/);
if (!speakerMatch) {
continue;
}
const speakerID = speakerMatch[1];
const speakerText = speakerMatch[2].trim();
const optionalCode = speakerText.match(/(#\S+)/)?.[1] ?? null;
const resolvedSpeakerID = optionalCode || speakerID;
speakers.set(speakerID, { label: speakerText.replace(/\s*#\S+\s*$/, '').trim(), optionalCode, resolvedSpeakerID });
if (!optionalCode) {
warnings.push(`Speaker ${speakerID} is missing an optional #speaker code.`);
}
}
return speakers;
}
function buildSpeakerPersonEntities(speakerMap) {
const entities = [];
for (const [speakerID, details] of speakerMap.entries()) {
const entityId = details.optionalCode ? details.optionalCode : `#${speakerID}`;
const entity = {
'@id': entityId,
'@type': 'Person',
name: details.label || speakerID,
};
if (details.optionalCode) {
entity.identifier = details.optionalCode;
}
entities.push(entity);
}
return entities;
}
function buildRoCrateMetadata(collectionName, documents) {
const crate = new ROCrate({ array: true, link: true });
crate.addContext({ ldac: 'https://w3id.org/ldac/terms#' });
crate.addContext({ pcdm: 'http://pcdm.org/models#' });
crate.rootDataset['@id'] = './';
crate.rootDataset['@type'] = ['Dataset', 'RepositoryCollection'];
crate.rootDataset.name = collectionName;
crate.rootDataset.conformsTo = { '@id': 'https://w3id.org/ldac/profile' };
crate.descriptor.about = { '@id': './' };
const collectionEntity = {
'@id': './collection',
'@type': 'RepositoryCollection',
name: collectionName,
hasMember: documents.map((document) => ({ '@id': document.objectId })),
};
crate.addEntity(collectionEntity);
crate.rootDataset.hasMember = documents.map((document) => ({ '@id': document.objectId }));
for (const document of documents) {
const objectEntity = {
'@id': document.objectId,
'@type': 'RepositoryObject',
name: document.baseName,
hasPart: [
{ '@id': document.docxId },
{ '@id': document.csvId },
],
speaker: document.speakerRefs,
};
const annotationEntity = {
'@id': document.annotationId,
'@type': 'Annotation',
annotationOf: { '@id': document.objectId },
annotationBody: { '@id': document.csvId },
};
crate.addEntity(objectEntity);
crate.addEntity(annotationEntity);
crate.addEntity({
'@id': document.docxId,
'@type': 'File',
name: document.docxName,
encodingFormat: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
});
crate.addEntity({
'@id': document.csvId,
'@type': 'File',
name: document.csvName,
encodingFormat: 'text/csv',
});
for (const person of document.persons) {
crate.addEntity(person);
}
}
return crate;
}
function validateSectionOrder(foundSections, warnings) {
const expected = ['PRELIMINARIES', 'MAIN', 'POSTLIMINARIES'];
const actual = foundSections.slice();
if (actual.length === 0) {
warnings.push('No section markers found. Defaulting all rows to MAIN.');
return;
}
const firstMarker = actual[0];
if (firstMarker !== 'PRELIMINARIES') {
warnings.push(`Unexpected first section marker: ${firstMarker ?? 'none'}. Expected PRELIMINARIES.`);
}
const orderedSeen = [];
for (const marker of expected) {
if (actual.includes(marker)) {
orderedSeen.push(marker);
}
}
if (orderedSeen.length > 0 && orderedSeen[0] !== 'PRELIMINARIES') {
warnings.push(`Section order warning: expected PRELIMINARIES before MAIN.`);
}
if (orderedSeen.includes('MAIN') && orderedSeen.indexOf('MAIN') < orderedSeen.indexOf('PRELIMINARIES')) {
warnings.push('Section order warning: MAIN appears before PRELIMINARIES.');
}
if (actual.includes('POSTLIMINARIES')) {
const mainIndex = actual.indexOf('MAIN');
const postIndex = actual.indexOf('POSTLIMINARIES');
if (mainIndex !== -1 && postIndex !== -1 && postIndex < mainIndex) {
warnings.push('Section order warning: POSTLIMINARIES appears before MAIN.');
}
}
}
function parseRows(text, warnings) {
const rows = [];
const lines = text.split('\n');
const speakers = parseSpeakerBlock(lines, warnings);
let sectionOrder = [];
let currentSection = 'MAIN';
let transcriptStarted = false;
let lastRow = null;
for (const rawLine of lines) {
const line = rawLine.trim();
if (!line) {
continue;
}
if (line === 'Speakers:') {
transcriptStarted = false;
continue;
}
if (line === 'PRELIMINARIES') {
transcriptStarted = true;
currentSection = 'PRE';
sectionOrder.push(line);
continue;
}
if (line === 'MAIN') {
transcriptStarted = true;
currentSection = 'MAIN';
sectionOrder.push(line);
continue;
}
if (line === 'POSTLIMINARIES') {
transcriptStarted = true;
currentSection = 'POST';
sectionOrder.push(line);
continue;
}
if (!transcriptStarted) {
continue;
}
if (speakers.size > 0 && /^([A-Z][A-Z0-9]?)\s*:\s*/.test(line)) {
const match = line.match(/^([A-Z][A-Z0-9]?)\s*:\s*(.*)$/);
if (!match) {
continue;
}
const rawSpeakerID = match[1];
const transcriptText = match[2].trim();
const speakerDetails = speakers.get(rawSpeakerID);
const speakerID = speakerDetails?.optionalCode || rawSpeakerID;
if (!speakerID || !transcriptText) {
continue;
}
lastRow = { speakerID, text: transcriptText, section: currentSection };
rows.push(lastRow);
continue;
}
if (!lastRow) {
continue;
}
lastRow.text = `${lastRow.text} ${line.trim()}`.trim();
}
validateSectionOrder(sectionOrder, warnings);
return rows;
}
function cleanCharacterValues(value) {
const replacements = {
'“': '"',
'”': '"',
'‘': "'",
'’': "'",
'—': '-',
'–': '-',
};
if (typeof value !== 'string') {
return value;
}
let cleaned = value.trim();
for (const [oldChar, newChar] of Object.entries(replacements)) {
cleaned = cleaned.replaceAll(oldChar, newChar);
}
return cleaned;
}
function collectCharacterInventory(rows) {
const chars = new Set();
for (const row of rows) {
const values = [row.speakerID, row.text, row.section];
for (const value of values) {
const text = String(value ?? '');
for (const char of text) {
chars.add(char);
}
}
}
return [...chars].sort();
}
function collectUnresolvedSpeakerRows(rows) {
return rows
.map((row, index) => ({ index, speakerID: row.speakerID || '' }))
.filter(({ speakerID }) => !String(speakerID).includes('#'));
}
function formatUnresolvedSpeakerRows(rows) {
const unresolved = collectUnresolvedSpeakerRows(rows);
if (unresolved.length === 0) {
return 'Unresolved speakerIDs: none';
}
const lines = [`Unresolved speakerIDs (${unresolved.length}):`];
for (const item of unresolved) {
lines.push(`Row ${item.index}: ${item.speakerID}`);
}
return lines.join('\n');
}
function formatCharacterInventory(rows) {
const chars = collectCharacterInventory(rows);
const lines = ['Character inventory:'];
for (const char of chars) {
const code = `U+${char.codePointAt(0).toString(16).toUpperCase().padStart(4, '0')}`;
const name = unicodeName(char) || unicodeName(char.codePointAt(0)) || '<NO NAME>';
lines.push(`${JSON.stringify(char)} ${code} ${name}`);
}
return lines.join('\n');
}
function applySectionRanges(rows, config) {
return rows.map((row) => ({
...row,
section: row.section || 'MAIN',
}));
}
function toCsv(rows) {
const output = [];
output.push('speakerID,text,section');
for (const row of rows) {
const speakerID = escapeCsv(row.speakerID || '');
const text = escapeCsv(row.text || '');
const section = escapeCsv(row.section || 'MAIN');
output.push(`${speakerID},${text},${section}`);
}
return output.join('\n') + '\n';
}
function escapeCsv(value) {
const stringValue = String(value ?? '');
if (/[",\n]/.test(stringValue)) {
return `"${stringValue.replace(/"/g, '""')}"`;
}
return stringValue;
}
function writeLogFile(logPath, message) {
fs.writeFileSync(logPath, `${message}\n`, 'utf8');
}
function processFile(filePath, outputDir, config) {
const paragraphs = extractParagraphs(filePath);
const cleanedText = normalizeText(paragraphs.join('\n'));
const mergedText = mergeContinuationLines(cleanedText);
const warnings = [];
const speakerMap = parseSpeakerBlock(mergedText.split('\n'), warnings);
let rows = parseRows(mergedText, warnings);
if (config.headerRows > 0) {
rows = rows.slice(config.headerRows);
}
if (config.footerRows > 0) {
rows = rows.slice(0, Math.max(0, rows.length - config.footerRows));
}
rows = applySectionRanges(rows, config);
rows = rows.map((row) => ({
speakerID: cleanCharacterValues(row.speakerID),
text: cleanCharacterValues(row.text),
section: cleanCharacterValues(row.section || 'MAIN'),
}));
const fileName = path.basename(filePath, '.docx');
const csvPath = path.join(outputDir, `${fileName}.csv`);
const logPath = path.join(outputDir, `${fileName}.log.txt`);
fs.writeFileSync(csvPath, toCsv(rows), 'utf8');
const logSummary = [
`Source file: ${filePath}`,
`Rows processed: ${rows.length}`,
`Header rows removed: ${config.headerRows}`,
`Footer rows removed: ${config.footerRows}`,
`PRE range: ${config.preStart ?? 'n/a'}-${config.preEnd ?? 'n/a'}`,
`POST range: ${config.postStart ?? 'n/a'}-${config.postEnd ?? 'n/a'}`,
`Warnings: ${warnings.length ? warnings.join('; ') : 'none'}`,
'Transformations applied: text normalization, continuation repair, speaker block review, section classification, character cleanup.',
'',
formatUnresolvedSpeakerRows(rows),
'',
formatCharacterInventory(rows),
].join('\n');
writeLogFile(logPath, logSummary);
const sourceRelativePath = path.relative(outputDir, filePath).replace(/\\/g, '/');
const speakerRefs = Array.from(speakerMap.entries()).map(([speakerID, details]) => ({
'@id': details.optionalCode || `#${speakerID}`,
}));
const personEntities = buildSpeakerPersonEntities(speakerMap);
if (warnings.length > 0) {
console.warn(`Processed ${fileName}.docx with warnings: ${warnings.join('; ')}`);
} else {
console.log(`Processed ${fileName}.docx -> ${csvPath}`);
}
return {
baseName: fileName,
docxName: path.basename(filePath),
csvName: `${fileName}.csv`,
sourcePath: sourceRelativePath,
objectId: `./${fileName}`,
docxId: sourceRelativePath,
csvId: `./${fileName}.csv`,
annotationId: `#annotation-${fileName}`,
speakerRefs,
persons: personEntities,
};
}
async function main() {
try {
const config = parseArgs(process.argv.slice(2));
if (!ensurePythonDocxInstalled()) {
console.error('python-docx is not installed. Run: python3 -m pip install -r requirements.txt');
process.exit(1);
}
fs.mkdirSync(config.output, { recursive: true });
if (!fs.existsSync(config.input)) {
throw new Error(`Input directory does not exist: ${config.input}`);
}
const files = fs.readdirSync(config.input)
.filter((entry) => entry.toLowerCase().endsWith('.docx') && !entry.startsWith('~$'))
.map((entry) => path.join(config.input, entry))
.sort();
if (files.length === 0) {
console.log(`No .docx files found in ${config.input}.`);
return;
}
const documentRecords = [];
for (const file of files) {
const record = processFile(file, config.output, config);
documentRecords.push(record);
}
const collectionName = path.basename(path.resolve(config.input));
const crate = buildRoCrateMetadata(collectionName, documentRecords);
const crateOutputPath = path.join(config.output, 'ro-crate-metadata.json');
const xlsxOutputPath = path.join(config.output, 'ro-crate-metadata.xlsx');
fs.writeFileSync(crateOutputPath, JSON.stringify(crate.getJson(), null, 2) + '\n', 'utf8');
const workbook = new Workbook({ crate });
await workbook.crateToWorkbook();
const xlsxBytes = await workbook.workbook.xlsx.writeBuffer();
fs.writeFileSync(xlsxOutputPath, Buffer.from(xlsxBytes));
console.log(`RO-Crate metadata written to ${crateOutputPath}`);
console.log(`RO-Crate workbook written to ${xlsxOutputPath}`);
} catch (error) {
console.error(`Error: ${error.message}`);
process.exit(1);
}
}
main();