Skip to content

Commit 539b89b

Browse files
committed
Use constants for WPM thresholds
1 parent 457f721 commit 539b89b

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

sdk/voice/speechmatics/voice/_utils.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
from ._models import SpeakerSegmentView
1717
from ._models import SpeechFragment
1818

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+
1926

2027
class FragmentUtils:
2128
"""Set of utility functions for working with SpeechFragment and SpeakerSegment objects."""
@@ -288,17 +295,18 @@ def _annotate_segment(segment: SpeakerSegment) -> AnnotationResult:
288295
# Rate of speech
289296
if len(words) > 1:
290297
# Calculate the approximate words-per-minute (for last few words)
291-
recent_words = words[-10:]
298+
recent_words = words[-WPM_RECENT_WORD_WINDOW:]
292299
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
294302

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)
302310

303311
# Return the annotation result
304312
return result
@@ -400,7 +408,7 @@ def find_segment_pauses(session: ClientSessionInfo, view: SpeakerSegmentView) ->
400408
next_word = words[i + 1]
401409
gap_start = word.end_time
402410
gap_end = next_word.start_time
403-
if gap_end - gap_start > 0.1:
411+
if gap_end - gap_start > PAUSE_MIN_GAP_S:
404412
segment.fragments.append(
405413
SpeechFragment(
406414
idx=word.idx + 1,

0 commit comments

Comments
 (0)