-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeakerStats.ts
More file actions
72 lines (63 loc) · 1.84 KB
/
speakerStats.ts
File metadata and controls
72 lines (63 loc) · 1.84 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
import type { TranscriptEntry, Speaker } from '@transcript-parser/types'
export interface SpeakerStats {
speaker: Speaker
segmentCount: number
totalDuration: number
percentage: number
}
/**
* Calculate statistics for each speaker in the transcript
*/
export function calculateSpeakerStats(
entries: TranscriptEntry[],
speakers: Speaker[]
): SpeakerStats[] {
// Calculate total transcript duration
const totalDuration = entries.reduce((total, entry) => {
return total + (entry.endTime - entry.startTime)
}, 0)
// Calculate stats for each speaker
const statsMap = new Map<
number,
{ segmentCount: number; totalDuration: number }
>()
entries.forEach(entry => {
const duration = entry.endTime - entry.startTime
const existing = statsMap.get(entry.speakerNumber) || {
segmentCount: 0,
totalDuration: 0,
}
statsMap.set(entry.speakerNumber, {
segmentCount: existing.segmentCount + 1,
totalDuration: existing.totalDuration + duration,
})
})
// Convert to SpeakerStats array
return speakers
.map(speaker => {
const stats = statsMap.get(speaker.id) || {
segmentCount: 0,
totalDuration: 0,
}
return {
speaker,
segmentCount: stats.segmentCount,
totalDuration: stats.totalDuration,
percentage:
totalDuration > 0 ? (stats.totalDuration / totalDuration) * 100 : 0,
}
})
.sort((a, b) => b.totalDuration - a.totalDuration) // Sort by duration descending
}
/**
* Format duration in seconds to MM:SS or HH:MM:SS
*/
export function formatDuration(seconds: number): string {
const hours = Math.floor(seconds / 3600)
const minutes = Math.floor((seconds % 3600) / 60)
const secs = Math.floor(seconds % 60)
if (hours > 0) {
return `${hours}h ${minutes}m ${secs}s`
}
return `${minutes}m ${secs}s`
}