Skip to content

Commit 940b0d2

Browse files
authored
fix(cli): warn when jieba is unavailable for readable SRT splitting (#3726)
Report the punctuation-only subtitle fallback once per process without changing subtitle output. Signed-off-by: LauraGPT <zhifu.gzf@alibaba-inc.com>
1 parent fbbfe01 commit 940b0d2

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

‎funasr/cli.py‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import argparse
44
import json
5+
import logging
56
import os
67
import re
78
import sys
@@ -184,13 +185,28 @@ def _timestamps_are_ordered(timestamps):
184185
)
185186

186187

188+
_SUBTITLE_WORD_BOUNDARY_FALLBACK_WARNED = False
189+
190+
191+
def _warn_subtitle_word_boundary_fallback(error):
192+
"""Report once that subtitle splitting lost its word-boundary scoring."""
193+
global _SUBTITLE_WORD_BOUNDARY_FALLBACK_WARNED
194+
if _SUBTITLE_WORD_BOUNDARY_FALLBACK_WARNED:
195+
return
196+
_SUBTITLE_WORD_BOUNDARY_FALLBACK_WARNED = True
197+
logging.warning(
198+
f"jieba word segmentation is unavailable ({error}); readable subtitles "
199+
"fall back to punctuation-only boundaries and can split words across "
200+
"cues. Install jieba to restore word-aware subtitle splitting."
201+
)
202+
203+
187204
def _subtitle_break_weights(text, token_spans):
188205
"""Map token-boundary indices to lexical and punctuation preferences."""
189206
boundary_to_token = {span[1]: index + 1 for index, span in enumerate(token_spans)}
190207
breaks = {len(token_spans): 0.0}
191208

192209
try:
193-
import logging
194210
import warnings
195211

196212
with warnings.catch_warnings():
@@ -222,8 +238,8 @@ def _subtitle_break_weights(text, token_spans):
222238
breaks[token_index] = max(
223239
breaks.get(token_index, 0.0), strength
224240
)
225-
except (ImportError, RuntimeError, ValueError):
226-
pass
241+
except (ImportError, RuntimeError, ValueError) as error:
242+
_warn_subtitle_word_boundary_fallback(error)
227243

228244
for index, span in enumerate(token_spans[:-1], 1):
229245
boundary = span[1]

‎tests/test_cli.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,3 +609,20 @@ def test_merge_subtitle_segments_avoids_single_character_phrase_breaks():
609609
for boundary in boundary_offsets
610610
)
611611
search_from = phrase_start + 1
612+
613+
614+
def test_subtitle_break_weights_warns_once_without_jieba(monkeypatch):
615+
text = "钟书成为朴家保姆后他轻易的偷偷扭了一下儿子的耳朵"
616+
token_spans = [[index, index + 1] for index in range(len(text))]
617+
monkeypatch.setattr(cli, "_SUBTITLE_WORD_BOUNDARY_FALLBACK_WARNED", False, raising=False)
618+
619+
with (
620+
patch.dict(sys.modules, {"jieba": None}),
621+
patch("logging.warning") as warning,
622+
):
623+
first = cli._subtitle_break_weights(text, token_spans)
624+
second = cli._subtitle_break_weights(text, token_spans)
625+
626+
assert first == second == {len(token_spans): 0.0}
627+
assert warning.call_count == 1
628+
assert "jieba" in warning.call_args[0][0]

0 commit comments

Comments
 (0)