Skip to content

Open-source speech recognition toolkit for training, inference, streaming ASR, VAD, punctuation, speaker diarization pipelines, and OpenAI-compatible/MCP serving.

fix: retain unpunctuated tail in sentence timestamps (#3754)

Chi-Wei Leed77b31d8c22f
diff --git a/funasr/utils/timestamp_tools.py b/funasr/utils/timestamp_tools.py
index fb77702eb..b97e405f3 100644
--- a/funasr/utils/timestamp_tools.py
+++ b/funasr/utils/timestamp_tools.py
@@ -216,6 +216,16 @@ def timestamp_sentence(
sentence_text_seg = ""
ts_list = []
sentence_start = None
+ if sentence_text and all(timestamp is not None for timestamp in ts_list):
+ sentence = {
+ "text": sentence_text,
+ "start": sentence_start,
+ "end": sentence_end,
+ "timestamp": ts_list,
+ }
+ if return_raw_text:
+ sentence["raw_text"] = sentence_text_seg.rstrip()
+ res.append(sentence)
return res
@@ -310,4 +320,14 @@ def timestamp_sentence_en(
sentence_text_seg = ""
ts_list = []
sentence_start = None
+ if sentence_text and all(timestamp is not None for timestamp in ts_list):
+ sentence = {
+ "text": sentence_text,
+ "start": sentence_start,
+ "end": sentence_end,
+ "timestamp": ts_list,
+ }
+ if return_raw_text:
+ sentence["raw_text"] = sentence_text_seg.rstrip()
+ res.append(sentence)
return res
diff --git a/tests/test_punc_model_none.py b/tests/test_punc_model_none.py
index c803b822c..4e14eb705 100644
--- a/tests/test_punc_model_none.py
+++ b/tests/test_punc_model_none.py
@@ -646,6 +646,33 @@ class TestPuncModelNone(unittest.TestCase):
self.assertEqual(results[0]["sentence_info"][0]["text"], "你好。")
+ @patch("funasr.auto.auto_model.slice_padding_audio_samples")
+ @patch("funasr.auto.auto_model.load_audio_text_image_video")
+ @patch("funasr.auto.auto_model.prepare_data_iterator")
+ def test_sentence_timestamp_keeps_unpunctuated_tail_without_asr_words(
+ self, mock_prep, mock_load, mock_slice
+ ):
+ am = self._make_auto_model(punc_model=MagicMock())
+ results_seq = [
+ [{"key": "test_utt", "value": [[0, 300]]}],
+ [{"text": "hello world", "timestamp": [[0, 100], [100, 300]]}],
+ [{"text": "hello world", "punc_array": [1, 1]}],
+ ]
+ am.inference = MagicMock(side_effect=lambda *args, **kwargs: results_seq.pop(0))
+ mock_prep.return_value = (["test_utt"], [np.zeros(4800, dtype=np.float32)])
+ mock_load.return_value = np.zeros(4800, dtype=np.float32)
+ mock_slice.return_value = ([np.zeros(4800, dtype=np.float32)], [4800])
+
+ result = am.inference_with_vad(
+ "dummy_input", sentence_timestamp=True, en_post_proc=True
+ )
+
+ self.assertEqual(
+ [item["text"].strip() for item in result[0]["sentence_info"]],
+ ["hello world"],
+ )
+ self.assertEqual(result[0]["sentence_info"][0]["timestamp"], [[0, 100], [100, 300]])
+
@patch("funasr.auto.auto_model.slice_padding_audio_samples")
@patch("funasr.auto.auto_model.load_audio_text_image_video")
@patch("funasr.auto.auto_model.prepare_data_iterator")
diff --git a/tests/test_timestamp_tools.py b/tests/test_timestamp_tools.py
index d5af9408e..a9dd41d18 100644
--- a/tests/test_timestamp_tools.py
+++ b/tests/test_timestamp_tools.py
@@ -1,3 +1,5 @@
+import pytest
+
from funasr.utils.timestamp_tools import timestamp_sentence, timestamp_sentence_en
@@ -64,3 +66,40 @@ def test_timestamp_sentence_raw_text_keeps_token_separators():
)
assert [sentence["raw_text"] for sentence in result] == ["我 用", "iphone pro max"]
+
+
+ ("splitter", "words", "first", "last"),
+ [
+ (timestamp_sentence, "你 好 世界", "你好。", "世界"),
+ (timestamp_sentence_en, "hello world again", "hello world.", "again"),
+ ],
+)
+def test_timestamp_sentence_keeps_unpunctuated_tail(splitter, words, first, last):
+ result = splitter(
+ [1, 3, 1],
+ [[0, 100], [100, 200], [200, 300]],
+ words,
+ return_raw_text=True,
+ )
+
+ assert [sentence["text"].strip() for sentence in result] == [first, last]
+ assert result[-1] == {
+ "text": last,
+ "start": 200,
+ "end": 300,
+ "timestamp": [[200, 300]],
+ "raw_text": last,
+ }
+
+
[email protected]("splitter", [timestamp_sentence, timestamp_sentence_en])
+def test_timestamp_sentence_does_not_emit_tail_without_its_timestamp(splitter):
+ result = splitter(
+ [1, 3, 1],
+ [[0, 100], [100, 200]],
+ "hello world again",
+ )
+
+ assert len(result) == 1
+ assert result[0]["end"] == 200