|
16 | 16 | from ._models import SpeakerSegmentView |
17 | 17 | from ._models import SpeechFragment |
18 | 18 |
|
| 19 | +# Constants |
| 20 | +PAUSE_MIN_GAP_S = 0.1 # minimum gap in seconds to consider a pause |
| 21 | +WPM_RECENT_WORD_WINDOW = 10 # number of recent words to estimate WPM from |
| 22 | +WPM_VERY_SLOW_MAX = 80 # wpm < 80 => VERY_SLOW_SPEAKER |
| 23 | +WPM_SLOW_MAX = 110 # 80 <= wpm < 110 => SLOW_SPEAKER |
| 24 | +WPM_FAST_MIN = 250 # wpm > 250 => FAST_SPEAKER |
| 25 | + |
19 | 26 |
|
20 | 27 | class FragmentUtils: |
21 | 28 | """Set of utility functions for working with SpeechFragment and SpeakerSegment objects.""" |
@@ -288,17 +295,18 @@ def _annotate_segment(segment: SpeakerSegment) -> AnnotationResult: |
288 | 295 | # Rate of speech |
289 | 296 | if len(words) > 1: |
290 | 297 | # Calculate the approximate words-per-minute (for last few words) |
291 | | - recent_words = words[-10:] |
| 298 | + recent_words = words[-WPM_RECENT_WORD_WINDOW:] |
292 | 299 | word_time_span = recent_words[-1].end_time - recent_words[0].start_time |
293 | | - wpm = (len(recent_words) / word_time_span) * 60 |
| 300 | + if (word_time_span != 0): |
| 301 | + wpm = (len(recent_words) / word_time_span) * 60 |
294 | 302 |
|
295 | | - # Categorize the speaker |
296 | | - if wpm < 80: |
297 | | - result.add(AnnotationFlags.VERY_SLOW_SPEAKER) |
298 | | - elif wpm < 110: |
299 | | - result.add(AnnotationFlags.SLOW_SPEAKER) |
300 | | - elif wpm > 250: |
301 | | - result.add(AnnotationFlags.FAST_SPEAKER) |
| 303 | + # Categorize the speaker |
| 304 | + if wpm < WPM_VERY_SLOW_MAX: |
| 305 | + result.add(AnnotationFlags.VERY_SLOW_SPEAKER) |
| 306 | + elif wpm < WPM_SLOW_MAX: |
| 307 | + result.add(AnnotationFlags.SLOW_SPEAKER) |
| 308 | + elif wpm > WPM_FAST_MIN: |
| 309 | + result.add(AnnotationFlags.FAST_SPEAKER) |
302 | 310 |
|
303 | 311 | # Return the annotation result |
304 | 312 | return result |
@@ -400,7 +408,7 @@ def find_segment_pauses(session: ClientSessionInfo, view: SpeakerSegmentView) -> |
400 | 408 | next_word = words[i + 1] |
401 | 409 | gap_start = word.end_time |
402 | 410 | gap_end = next_word.start_time |
403 | | - if gap_end - gap_start > 0.1: |
| 411 | + if gap_end - gap_start > PAUSE_MIN_GAP_S: |
404 | 412 | segment.fragments.append( |
405 | 413 | SpeechFragment( |
406 | 414 | idx=word.idx + 1, |
|
0 commit comments