From a4a648bbd0488e68f56ef782f73a54223b4b4859 Mon Sep 17 00:00:00 2001 From: Nurlan Mammadli Date: Fri, 20 Nov 2026 19:05:38 -0500 Subject: [PATCH] fix(sensevoice): keep event dedupe state across text segments Co-Authored-By: Claude Opus 5.5 --- funasr/utils/postprocess_utils.py | 5 ++-- .../funasr_torch/utils/postprocess_utils.py | 5 ++-- .../funasr_onnx/utils/postprocess_utils.py | 5 ++-- .../test_rich_transcription_event_segments.py | 26 +++++++++++++++++++ 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/funasr/utils/postprocess_utils.py b/funasr/utils/postprocess_utils.py index 505bed87d..8b64de4a3 100644 --- a/funasr/utils/postprocess_utils.py +++ b/funasr/utils/postprocess_utils.py @@ -483,12 +483,13 @@ def get_event(s): for i in range(1, len(s_list)): if len(s_list[i]) == 0: continue - if get_event(s_list[i]) == cur_ent_event and get_event(s_list[i]) != None: + event = get_event(s_list[i]) + if event == cur_ent_event and event != None: s_list[i] = s_list[i][1:] if len(s_list[i]) == 0: continue # else: - cur_ent_event = get_event(s_list[i]) + cur_ent_event = event if get_emo(s_list[i]) != None and get_emo(s_list[i]) == get_emo(new_s): new_s = new_s[:-1] segment = s_list[i].strip() diff --git a/runtime/python/libtorch/funasr_torch/utils/postprocess_utils.py b/runtime/python/libtorch/funasr_torch/utils/postprocess_utils.py index d2e01a484..e662e2793 100644 --- a/runtime/python/libtorch/funasr_torch/utils/postprocess_utils.py +++ b/runtime/python/libtorch/funasr_torch/utils/postprocess_utils.py @@ -373,12 +373,13 @@ def get_event(s): for i in range(1, len(s_list)): if len(s_list[i]) == 0: continue - if get_event(s_list[i]) == cur_ent_event and get_event(s_list[i]) != None: + event = get_event(s_list[i]) + if event == cur_ent_event and event != None: s_list[i] = s_list[i][1:] if len(s_list[i]) == 0: continue # else: - cur_ent_event = get_event(s_list[i]) + cur_ent_event = event if get_emo(s_list[i]) != None and get_emo(s_list[i]) == get_emo(new_s): new_s = new_s[:-1] segment = s_list[i].strip() diff --git a/runtime/python/onnxruntime/funasr_onnx/utils/postprocess_utils.py b/runtime/python/onnxruntime/funasr_onnx/utils/postprocess_utils.py index beda12ee1..6c150888d 100644 --- a/runtime/python/onnxruntime/funasr_onnx/utils/postprocess_utils.py +++ b/runtime/python/onnxruntime/funasr_onnx/utils/postprocess_utils.py @@ -427,12 +427,13 @@ def get_event(s): for i in range(1, len(s_list)): if len(s_list[i]) == 0: continue - if get_event(s_list[i]) == cur_ent_event and get_event(s_list[i]) != None: + event = get_event(s_list[i]) + if event == cur_ent_event and event != None: s_list[i] = s_list[i][1:] if len(s_list[i]) == 0: continue # else: - cur_ent_event = get_event(s_list[i]) + cur_ent_event = event if get_emo(s_list[i]) != None and get_emo(s_list[i]) == get_emo(new_s): new_s = new_s[:-1] segment = s_list[i].strip() diff --git a/tests/test_rich_transcription_event_segments.py b/tests/test_rich_transcription_event_segments.py index a565b5c3c..01f8c290b 100644 --- a/tests/test_rich_transcription_event_segments.py +++ b/tests/test_rich_transcription_event_segments.py @@ -52,3 +52,29 @@ def test_distinct_event_only_segments_remain_visible(postprocess): assert postprocess.rich_transcription_postprocess(text) == ( postprocess.event_dict["<|Applause|>"] + postprocess.event_dict["<|Laughter|>"] ) + + +@pytest.mark.parametrize("event", ["<|Cough|>", "<|Sneeze|>", "<|Applause|>"]) +def test_repeated_event_with_text_across_three_segments(postprocess, event): + text = ( + f"<|en|>{event}hello" + f"<|en|>{event}world" + f"<|en|>{event}again" + ) + emoji = postprocess.event_dict[event] + + assert postprocess.rich_transcription_postprocess(text) == f"{emoji}hello world again" + + +def test_different_event_between_same_events_keeps_all_emojis(postprocess): + text = ( + "<|en|><|Applause|>hello" + "<|en|><|Laughter|>world" + "<|en|><|Applause|>again" + ) + applause = postprocess.event_dict["<|Applause|>"] + laughter = postprocess.event_dict["<|Laughter|>"] + + assert postprocess.rich_transcription_postprocess(text) == ( + f"{applause}hello{laughter}world{applause}again" + )