From 481ce009759ddddff0e8188850be14bedfcc1c90 Mon Sep 17 00:00:00 2001 From: Kushal Dave Date: Fri, 13 Nov 2026 21:59:38 -0500 Subject: [PATCH] fix(timestamp): return string text from timestamp_sentence without punctuation With no punctuation ids, timestamp_sentence and timestamp_sentence_en returned the text as a list of tokens and ignored return_raw_text. Let the regular loop build the single sentence so text is a string and raw_text is honored. --- funasr/utils/timestamp_tools.py | 28 ++++++++-------------------- tests/test_timestamp_tools.py | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/funasr/utils/timestamp_tools.py b/funasr/utils/timestamp_tools.py index 8009ec757..7a76e620a 100644 --- a/funasr/utils/timestamp_tools.py +++ b/funasr/utils/timestamp_tools.py @@ -150,16 +150,10 @@ def timestamp_sentence( return res if punc_id_list is None or len(punc_id_list) == 0: - res.append( - { - "text": text_postprocessed.split(), - "start": timestamp_postprocessed[0][0], - "end": timestamp_postprocessed[-1][1], - "timestamp": timestamp_postprocessed, - } - ) - return res - if len(punc_id_list) != len(timestamp_postprocessed): + # No punctuation: the loop below yields a single sentence whose text is a + # string, like every other result. + punc_id_list = [] + elif len(punc_id_list) != len(timestamp_postprocessed): logging.warning("length mismatch between punc and timestamp") sentence_text = "" sentence_text_seg = "" @@ -257,16 +251,10 @@ def timestamp_sentence_en( return res if punc_id_list is None or len(punc_id_list) == 0: - res.append( - { - "text": text_postprocessed.split(), - "start": timestamp_postprocessed[0][0], - "end": timestamp_postprocessed[-1][1], - "timestamp": timestamp_postprocessed, - } - ) - return res - if len(punc_id_list) != len(timestamp_postprocessed): + # No punctuation: the loop below yields a single sentence whose text is a + # string, like every other result. + punc_id_list = [] + elif len(punc_id_list) != len(timestamp_postprocessed): logging.warning("length mismatch between punc and timestamp") sentence_text = "" sentence_text_seg = "" diff --git a/tests/test_timestamp_tools.py b/tests/test_timestamp_tools.py index 763c14e19..c584015ab 100644 --- a/tests/test_timestamp_tools.py +++ b/tests/test_timestamp_tools.py @@ -128,3 +128,18 @@ def test_timestamp_sentence_does_not_emit_tail_without_its_timestamp(splitter): assert len(result) == 1 assert result[0]["end"] == 200 + + +@pytest.mark.parametrize("punc_id_list", [None, []]) +def test_timestamp_sentence_without_punctuation_returns_string_text(punc_id_list): + timestamps = [[0, 100], [100, 200]] + + zh = timestamp_sentence(punc_id_list, timestamps, "你 好", return_raw_text=True) + en = timestamp_sentence_en(punc_id_list, timestamps, "hello world", return_raw_text=True) + + assert zh == [ + {"text": "你好", "start": 0, "end": 200, "timestamp": timestamps, "raw_text": "你 好"} + ] + assert en == [ + {"text": "hello world", "start": 0, "end": 200, "timestamp": timestamps, "raw_text": "hello world"} + ]