|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | +import { OblUtil } from '../../src/optional/analytics/index'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Script to bulk-analyze OBLA clinical data and extract utterances to a CSV. |
| 7 | + */ |
| 8 | + |
| 9 | +const OBLA_DIR = path.join(__dirname, '../../obla-improvements/small-obla/small'); |
| 10 | +const OUTPUT_CSV = path.join(__dirname, 'obl_utterances.csv'); |
| 11 | + |
| 12 | +interface UtteranceRecord { |
| 13 | + file: string; |
| 14 | + userId: string; |
| 15 | + timestamp: string; |
| 16 | + type: string; |
| 17 | + content: string; |
| 18 | + boardId?: string; |
| 19 | +} |
| 20 | + |
| 21 | +function run() { |
| 22 | + console.log(`Analyzing OBLA data in: ${OBLA_DIR}...`); |
| 23 | + |
| 24 | + if (!fs.existsSync(OBLA_DIR)) { |
| 25 | + console.error(`Directory not found: ${OBLA_DIR}`); |
| 26 | + process.exit(1); |
| 27 | + } |
| 28 | + |
| 29 | + const files = fs.readdirSync(OBLA_DIR).filter(f => f.endsWith('.obla')); |
| 30 | + console.log(`Found ${files.length} files.`); |
| 31 | + |
| 32 | + const records: any[] = []; |
| 33 | + |
| 34 | + for (const file of files) { |
| 35 | + try { |
| 36 | + const content = fs.readFileSync(path.join(OBLA_DIR, file), 'utf8'); |
| 37 | + const obl = OblUtil.parse(content); |
| 38 | + |
| 39 | + for (const session of obl.sessions) { |
| 40 | + let currentSentence: string[] = []; |
| 41 | + let sentenceStartTime: string | null = null; |
| 42 | + |
| 43 | + // Sort events within session by timestamp to be sure |
| 44 | + const sortedEvents = [...session.events].sort((a, b) => a.timestamp.localeCompare(b.timestamp)); |
| 45 | + |
| 46 | + for (let i = 0; i < sortedEvents.length; i++) { |
| 47 | + const event = sortedEvents[i]; |
| 48 | + const nextEvent = sortedEvents[i + 1]; |
| 49 | + |
| 50 | + if (!sentenceStartTime) sentenceStartTime = event.timestamp; |
| 51 | + |
| 52 | + let text = ''; |
| 53 | + let isBoundary = false; |
| 54 | + |
| 55 | + if (event.type === 'button') { |
| 56 | + text = (event as any).label || (event as any).vocalization || '[?]'; |
| 57 | + } else if (event.type === 'utterance') { |
| 58 | + text = (event as any).text; |
| 59 | + isBoundary = true; // Utterances are usually complete sentences |
| 60 | + } else if (event.type === 'action') { |
| 61 | + const action = (event as any).action; |
| 62 | + if (action === ':clear' || action === ':speak' || action === ':home') { |
| 63 | + isBoundary = true; |
| 64 | + } |
| 65 | + if (action === ':backspace') { |
| 66 | + currentSentence.pop(); |
| 67 | + } else { |
| 68 | + text = `[${action}]`; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if (text) currentSentence.push(text); |
| 73 | + |
| 74 | + // Check for time gap boundary (> 15 seconds) |
| 75 | + if (nextEvent) { |
| 76 | + const currentMs = new Date(event.timestamp).getTime(); |
| 77 | + const nextMs = new Date(nextEvent.timestamp).getTime(); |
| 78 | + if (nextMs - currentMs > 15000) isBoundary = true; |
| 79 | + } else { |
| 80 | + isBoundary = true; // End of session |
| 81 | + } |
| 82 | + |
| 83 | + if (isBoundary && currentSentence.length > 0) { |
| 84 | + records.push({ |
| 85 | + timestamp: sentenceStartTime, |
| 86 | + userId: obl.user_id, |
| 87 | + sentence: currentSentence.join(' '), |
| 88 | + file: file |
| 89 | + }); |
| 90 | + currentSentence = []; |
| 91 | + sentenceStartTime = null; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } catch (err) { |
| 96 | + console.error(`Error processing ${file}:`, err); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // Sort by timestamp |
| 101 | + records.sort((a, b) => a.timestamp.localeCompare(b.timestamp)); |
| 102 | + |
| 103 | + // Write CSV |
| 104 | + const header = 'Timestamp,User ID,Sentence,File\n'; |
| 105 | + const csvLines = records.map(r => { |
| 106 | + const escapedSentence = `"${r.sentence.replace(/"/g, '""')}"`; |
| 107 | + return `${r.timestamp},${r.userId},${escapedSentence},${r.file}`; |
| 108 | + }); |
| 109 | + |
| 110 | + fs.writeFileSync(OUTPUT_CSV, header + csvLines.join('\n')); |
| 111 | + |
| 112 | + console.log(`\nAnalysis complete!`); |
| 113 | + console.log(`Total sentences reconstructed: ${records.length}`); |
| 114 | + console.log(`Results saved to: ${OUTPUT_CSV}`); |
| 115 | + |
| 116 | + // Show a preview |
| 117 | + console.log('\nPreview (10 Reconstructed Sentences):'); |
| 118 | + const preview = records.filter(r => r.sentence.length > 5 && !r.sentence.includes('000')).slice(0, 20); |
| 119 | + preview.forEach(r => { |
| 120 | + console.log(`[${r.timestamp}] ${r.sentence}`); |
| 121 | + }); |
| 122 | +} |
| 123 | + |
| 124 | +run(); |
0 commit comments