( transcript: TranscriptSegment[] )
| 21 | * For edge cases (15-80% punctuation ratio), uses average text length as tiebreaker. |
| 22 | */ |
| 23 | export function detectTranscriptFormat( |
| 24 | transcript: TranscriptSegment[] |
| 25 | ): 'old' | 'new' { |
| 26 | if (!transcript || transcript.length === 0) { |
| 27 | return 'new'; // Default to new format for empty transcripts |
| 28 | } |
| 29 | |
| 30 | // Sample first 100 segments for analysis (or all if fewer) |
| 31 | const sampleSize = Math.min(100, transcript.length); |
| 32 | const sample = transcript.slice(0, sampleSize); |
| 33 | |
| 34 | // Calculate average text length |
| 35 | const totalTextLength = sample.reduce((sum, seg) => sum + (seg.text?.length || 0), 0); |
| 36 | const avgTextLength = totalTextLength / sampleSize; |
| 37 | |
| 38 | // Calculate percentage of segments ending with sentence punctuation |
| 39 | const sentenceEndingRegex = /[.!?\u3002\uff01\uff1f\u203c\u2047\u2048]\s*$/; |
| 40 | const sentenceEndingCount = sample.filter(seg => |
| 41 | sentenceEndingRegex.test(seg.text?.trim() || '') |
| 42 | ).length; |
| 43 | const sentenceEndingRatio = sentenceEndingCount / sampleSize; |
| 44 | |
| 45 | // Prioritize punctuation ratio as the primary indicator |
| 46 | // OLD format: Most segments break mid-sentence (low punctuation ratio) |
| 47 | if (sentenceEndingRatio < 0.15) { |
| 48 | return 'old'; |
| 49 | } |
| 50 | |
| 51 | // NEW format: Most segments end at sentence boundaries (high punctuation ratio) |
| 52 | if (sentenceEndingRatio > 0.80) { |
| 53 | return 'new'; |
| 54 | } |
| 55 | |
| 56 | // Medium punctuation ratio (15-80%): Use text length as tiebreaker |
| 57 | // Longer segments more likely to be merged sentences |
| 58 | return avgTextLength > 40 ? 'new' : 'old'; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Ensures transcript is in the new merged sentence format. |
no outgoing calls
no test coverage detected