@@ -278,63 +278,15 @@ def get_words_speaker_mapping(wrd_ts, spk_ts, word_anchor_option="start"):
278278
279279sentence_ending_punctuations = ".?!"
280280
281- # --------------------------------------------------------------------
282- # Old code kept for reference.
283- # Change: add bounds checks so punctuation-based realignment does not
284- # raise IndexError on empty/short word lists or malformed indices.
285- # --------------------------------------------------------------------
286- # def get_first_word_idx_of_sentence(word_idx, word_list, speaker_list, max_words):
287- # is_word_sentence_end = (
288- # lambda x: x >= 0 and word_list[x][-1] in sentence_ending_punctuations
289- # )
290- # left_idx = word_idx
291- # while (
292- # left_idx > 0
293- # and word_idx - left_idx < max_words
294- # and speaker_list[left_idx - 1] == speaker_list[left_idx]
295- # and not is_word_sentence_end(left_idx - 1)
296- # ):
297- # left_idx -= 1
298- #
299- # return left_idx if left_idx == 0 or is_word_sentence_end(left_idx - 1) else -1
300- #
301- # def get_last_word_idx_of_sentence(word_idx, word_list, max_words):
302- # is_word_sentence_end = (
303- # lambda x: x >= 0 and word_list[x][-1] in sentence_ending_punctuations
304- # )
305- # right_idx = word_idx
306- # while (
307- # right_idx < len(word_list) - 1
308- # and right_idx - word_idx < max_words
309- # and not is_word_sentence_end(right_idx)
310- # ):
311- # right_idx += 1
312- #
313- # return (
314- # right_idx
315- # if right_idx == len(word_list) - 1 or is_word_sentence_end(right_idx)
316- # else -1
317- # )
318281
319282def get_first_word_idx_of_sentence (word_idx , word_list , speaker_list , max_words ):
320- def is_word_sentence_end (x ):
321- return (
322- 0 <= x < len (word_list )
323- and isinstance (word_list [x ], str )
324- and word_list [x ]
325- and word_list [x ][- 1 ] in sentence_ending_punctuations
326- )
327-
328- if not word_list or not speaker_list :
329- return - 1
330- if not (0 <= word_idx < len (word_list )) or word_idx >= len (speaker_list ):
331- return - 1
332-
283+ is_word_sentence_end = (
284+ lambda x : x >= 0 and word_list [x ][- 1 ] in sentence_ending_punctuations
285+ )
333286 left_idx = word_idx
334287 while (
335288 left_idx > 0
336289 and word_idx - left_idx < max_words
337- and left_idx - 1 < len (speaker_list )
338290 and speaker_list [left_idx - 1 ] == speaker_list [left_idx ]
339291 and not is_word_sentence_end (left_idx - 1 )
340292 ):
@@ -344,17 +296,9 @@ def is_word_sentence_end(x):
344296
345297
346298def get_last_word_idx_of_sentence (word_idx , word_list , max_words ):
347- def is_word_sentence_end (x ):
348- return (
349- 0 <= x < len (word_list )
350- and isinstance (word_list [x ], str )
351- and word_list [x ]
352- and word_list [x ][- 1 ] in sentence_ending_punctuations
353- )
354-
355- if not word_list or not (0 <= word_idx < len (word_list )):
356- return - 1
357-
299+ is_word_sentence_end = (
300+ lambda x : x >= 0 and word_list [x ][- 1 ] in sentence_ending_punctuations
301+ )
358302 right_idx = word_idx
359303 while (
360304 right_idx < len (word_list ) - 1
@@ -363,7 +307,11 @@ def is_word_sentence_end(x):
363307 ):
364308 right_idx += 1
365309
366- return right_idx if right_idx == len (word_list ) - 1 or is_word_sentence_end (right_idx ) else - 1
310+ return (
311+ right_idx
312+ if right_idx == len (word_list ) - 1 or is_word_sentence_end (right_idx )
313+ else - 1
314+ )
367315
368316
369317def get_realigned_ws_mapping_with_punctuation (
0 commit comments