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_punc_model_none.py b/tests/test_punc_model_none.py index d827bca01..91f4fde2d 100644 --- a/tests/test_punc_model_none.py +++ b/tests/test_punc_model_none.py @@ -677,7 +677,7 @@ def test_sentence_timestamp_keeps_unpunctuated_tail_without_asr_words( @patch("funasr.auto.auto_model.load_audio_text_image_video") @patch("funasr.auto.auto_model.prepare_data_iterator") def test_sentence_timestamp_handles_unsized_punc_array(self, mock_prep, mock_load, mock_slice): - """Malformed punctuation metadata must use the legacy no-punctuation fallback.""" + """Malformed punctuation metadata must use normalized sentence output.""" am = self._make_auto_model(punc_model=MagicMock()) results_seq = [ [{"key": "test_utt", "value": [[0, 1000]]}], @@ -697,7 +697,9 @@ def test_sentence_timestamp_handles_unsized_punc_array(self, mock_prep, mock_loa results = am.inference_with_vad("dummy_input", sentence_timestamp=True) - self.assertEqual(results[0]["sentence_info"][0]["text"], ["你", "好"]) + sentence = results[0]["sentence_info"][0] + self.assertEqual(sentence["text"], "你好") + self.assertEqual(sentence["timestamp"], [[0, 500], [500, 1000]]) 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"} + ]