File size: 11,617 Bytes
832e106
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Precision alignment service - Word-center-based speaker assignment.
Merges word-level transcription with speaker diarization using precise timestamps.
"""
import logging
from pathlib import Path
from typing import List, Tuple, Optional
from dataclasses import dataclass

from app.core.config import get_settings
from app.schemas.models import TranscriptSegment
from app.services.transcription import WordTimestamp
from app.services.diarization import SpeakerSegment

logger = logging.getLogger(__name__)
settings = get_settings()


@dataclass
class WordWithSpeaker:
    """A word with assigned speaker."""
    word: str
    start: float
    end: float
    speaker: str


class AlignmentService:
    """
    Precision alignment service.
    Uses word-center-based algorithm for accurate speaker-to-text mapping.
    """
    
    # Pause threshold for splitting segments (seconds)
    PAUSE_THRESHOLD = 1.0
    
    @staticmethod
    def get_word_center(word: WordTimestamp) -> float:
        """Calculate the center time of a word."""
        return (word.start + word.end) / 2
    
    @classmethod
    def find_speaker_at_time(
        cls,
        time: float,
        speaker_segments: List[SpeakerSegment]
    ) -> Optional[str]:
        """
        Find which speaker is speaking at a given time.
        
        Args:
            time: Time point in seconds
            speaker_segments: List of speaker segments from diarization
            
        Returns:
            Speaker label or None if no speaker found
        """
        for seg in speaker_segments:
            if seg.start <= time <= seg.end:
                return seg.speaker
        return None
    
    @classmethod
    def find_closest_speaker(
        cls,
        time: float,
        speaker_segments: List[SpeakerSegment]
    ) -> str:
        """
        Find the closest speaker to a given time (for gaps/silence).
        
        Args:
            time: Time point in seconds
            speaker_segments: List of speaker segments
            
        Returns:
            Closest speaker label or "Unknown"
        """
        if not speaker_segments:
            return "Unknown"
        
        min_distance = float('inf')
        closest_speaker = "Unknown"
        
        for seg in speaker_segments:
            # Distance to segment start or end
            dist_to_start = abs(time - seg.start)
            dist_to_end = abs(time - seg.end)
            min_seg_dist = min(dist_to_start, dist_to_end)
            
            if min_seg_dist < min_distance:
                min_distance = min_seg_dist
                closest_speaker = seg.speaker
        
        return closest_speaker
    
    @classmethod
    def assign_speakers_to_words(
        cls,
        words: List[WordTimestamp],
        speaker_segments: List[SpeakerSegment]
    ) -> List[WordWithSpeaker]:
        """
        Step 3c: Assign speakers to each word based on word center time.
        
        Args:
            words: List of words with timestamps from transcription
            speaker_segments: List of speaker segments from diarization
            
        Returns:
            List of words with speaker assignments
        """
        if not speaker_segments:
            # No diarization available, assign all to "Speaker 1"
            logger.warning("No speaker segments available, using single speaker")
            return [
                WordWithSpeaker(
                    word=w.word,
                    start=w.start,
                    end=w.end,
                    speaker="Speaker 1"
                )
                for w in words
            ]
        
        words_with_speakers = []
        
        for word in words:
            # Calculate word center time
            center_time = cls.get_word_center(word)
            
            # Find speaker at this time
            speaker = cls.find_speaker_at_time(center_time, speaker_segments)
            
            # If no direct match, find closest speaker
            if speaker is None:
                speaker = cls.find_closest_speaker(center_time, speaker_segments)
            
            words_with_speakers.append(WordWithSpeaker(
                word=word.word,
                start=word.start,
                end=word.end,
                speaker=speaker
            ))
        
        logger.debug(f"Assigned speakers to {len(words_with_speakers)} words")
        return words_with_speakers
    
    @classmethod
    def reconstruct_segments(
        cls,
        words_with_speakers: List[WordWithSpeaker]
    ) -> List[TranscriptSegment]:
        """
        Step 3d: Reconstruct sentence segments from words.
        
        Groups consecutive words of the same speaker into segments.
        Creates new segment when:
        - Speaker changes
        - Pause > PAUSE_THRESHOLD between words
        
        Args:
            words_with_speakers: List of words with speaker assignments
            
        Returns:
            List of TranscriptSegment with complete sentences
        """
        if not words_with_speakers:
            return []
        
        segments = []
        
        # Start first segment
        current_speaker = words_with_speakers[0].speaker
        current_start = words_with_speakers[0].start
        current_end = words_with_speakers[0].end
        current_words = [words_with_speakers[0].word]
        
        for i in range(1, len(words_with_speakers)):
            word = words_with_speakers[i]
            prev_word = words_with_speakers[i - 1]
            
            # Calculate pause between words
            pause = word.start - prev_word.end
            
            # Check if we need to start a new segment
            speaker_changed = word.speaker != current_speaker
            significant_pause = pause > cls.PAUSE_THRESHOLD
            
            if speaker_changed or significant_pause:
                # Save current segment
                segments.append(TranscriptSegment(
                    start=current_start,
                    end=current_end,
                    speaker=current_speaker,
                    text=" ".join(current_words)
                ))
                
                # Start new segment
                current_speaker = word.speaker
                current_start = word.start
                current_end = word.end
                current_words = [word.word]
            else:
                # Continue current segment
                current_end = word.end
                current_words.append(word.word)
        
        # Don't forget the last segment
        if current_words:
            segments.append(TranscriptSegment(
                start=current_start,
                end=current_end,
                speaker=current_speaker,
                text=" ".join(current_words)
            ))
        
        logger.debug(f"Reconstructed {len(segments)} segments from {len(words_with_speakers)} words")
        return segments
    
    @classmethod
    def resize_and_merge_segments(
        cls,
        segments: List[TranscriptSegment]
    ) -> List[TranscriptSegment]:
        """
        Merge consecutive segments of the same speaker if the gap is small.
        Also filters out extremely short segments.
        """
        if not segments:
            return []
            
        # Filter 1: Remove extremely short blips (noise)
        segments = [s for s in segments if (s.end - s.start) >= settings.min_segment_duration_s]
        
        if not segments:
            return []
            
        merged = []
        curr = segments[0]
        
        for i in range(1, len(segments)):
            next_seg = segments[i]
            
            # If same speaker and gap is small, merge
            gap = next_seg.start - curr.end
            if next_seg.speaker == curr.speaker and gap < settings.merge_threshold_s:
                curr.end = next_seg.end
                curr.text += " " + next_seg.text
            else:
                merged.append(curr)
                curr = next_seg
                
        merged.append(curr)
        
        logger.debug(f"Merged segments: {len(segments)} -> {len(merged)}")
        return merged

    @classmethod
    def align_precision(
        cls,
        words: List[WordTimestamp],
        speaker_segments: List[SpeakerSegment]
    ) -> List[TranscriptSegment]:
        """
        Full precision alignment pipeline.
        
        Args:
            words: Word-level timestamps from transcription
            speaker_segments: Speaker segments from diarization
            
        Returns:
            List of TranscriptSegment with proper speaker assignments
        """
        # Step 3c: Assign speakers to words
        words_with_speakers = cls.assign_speakers_to_words(words, speaker_segments)
        
        # Step 3d: Reconstruct segments
        segments = cls.reconstruct_segments(words_with_speakers)
        
        # Step 3e: Clustering/Merging (Optimization)
        segments = cls.resize_and_merge_segments(segments)
        
        return segments
    
    @staticmethod
    def format_timestamp_txt(seconds: float) -> str:
        """Format timestamp for TXT output: HH:MM:SS"""
        hours = int(seconds // 3600)
        minutes = int((seconds % 3600) // 60)
        secs = int(seconds % 60)
        return f"{hours:02d}:{minutes:02d}:{secs:02d}"
    
    @staticmethod
    def format_timestamp_srt(seconds: float) -> str:
        """Format timestamp for SRT output: HH:MM:SS,mmm"""
        hours = int(seconds // 3600)
        minutes = int((seconds % 3600) // 60)
        secs = int(seconds % 60)
        millis = int((seconds % 1) * 1000)
        return f"{hours:02d}:{minutes:02d}:{secs:02d},{millis:03d}"
    
    @classmethod
    def generate_txt(cls, segments: List[TranscriptSegment], output_path: Path) -> Path:
        """
        Generate TXT transcript file.
        
        Format: [HH:MM:SS - HH:MM:SS] Speaker: Text
        """
        lines = []
        for seg in segments:
            start = cls.format_timestamp_txt(seg.start)
            end = cls.format_timestamp_txt(seg.end)
            lines.append(f"[{start} - {end}] {seg.speaker}: {seg.text}")
        
        output_path.write_text("\n".join(lines), encoding="utf-8")
        logger.info(f"Generated TXT: {output_path}")
        
        return output_path
    
    @classmethod
    def generate_srt(cls, segments: List[TranscriptSegment], output_path: Path) -> Path:
        """
        Generate SRT subtitle file.
        """
        lines = []
        for i, seg in enumerate(segments, 1):
            start = cls.format_timestamp_srt(seg.start)
            end = cls.format_timestamp_srt(seg.end)
            lines.append(str(i))
            lines.append(f"{start} --> {end}")
            lines.append(f"[{seg.speaker}] {seg.text}")
            lines.append("")  # Empty line between entries
        
        output_path.write_text("\n".join(lines), encoding="utf-8")
        logger.info(f"Generated SRT: {output_path}")
        
        return output_path
    
    @classmethod
    def generate_outputs(
        cls,
        segments: List[TranscriptSegment],
        base_filename: str
    ) -> Tuple[Path, Path]:
        """Generate both TXT and SRT output files."""
        txt_path = settings.processed_dir / f"{base_filename}.txt"
        srt_path = settings.processed_dir / f"{base_filename}.srt"
        
        cls.generate_txt(segments, txt_path)
        cls.generate_srt(segments, srt_path)
        
        return txt_path, srt_path