From 70ab19d23fcdd7c676a0fdcbb88b2caa89546940 Mon Sep 17 00:00:00 2001 From: korexil <289361616+korexil@users.noreply.github.com> Date: Mon, 12 Oct 2026 17:26:40 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20TTS=20=E5=88=86=E5=8F=A5=E5=9C=A8?= =?UTF-8?q?=E5=BC=95=E5=8F=B7=E5=86=85=E5=88=87=E5=BC=80=E5=8F=A5=E5=AD=90?= =?UTF-8?q?=20(#3413)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit _get_segment_text 会在引号内的标点处切分(如「わぁ!」って、“好!”然后), 把引号拆到两段里,或留下只有「。」的片段。新增 _quote_safe_cut:切分点后的 右引号与句末标点一并带走;位于未闭合引号内或右引号后句子仍在继续时,顺延到 下一个标点。不含引号的文本切分结果不变。 --- .../xiaozhi-server/core/providers/tts/base.py | 35 +++++++++++ .../tests/core/providers/__init__.py | 0 .../tests/core/providers/tts/__init__.py | 0 .../providers/tts/test_quote_safe_segment.py | 58 +++++++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 main/xiaozhi-server/tests/core/providers/__init__.py create mode 100644 main/xiaozhi-server/tests/core/providers/tts/__init__.py create mode 100644 main/xiaozhi-server/tests/core/providers/tts/test_quote_safe_segment.py diff --git a/main/xiaozhi-server/core/providers/tts/base.py b/main/xiaozhi-server/core/providers/tts/base.py index 2ff779c03..7c2feb4ff 100644 --- a/main/xiaozhi-server/core/providers/tts/base.py +++ b/main/xiaozhi-server/core/providers/tts/base.py @@ -483,6 +483,37 @@ class TTSProviderBase(ABC): if hasattr(self, "ws") and self.ws: await self.ws.close() + # 右引号/右括号,以及它们对应的左侧符号 + _QUOTE_PAIRS = (("「", "」"), ("『", "』"), ("“", "”"), ("(", ")"), ("《", "》")) + _CLOSERS = "」』”)》" + + @classmethod + def _quote_safe_cut(cls, text, pos, punctuations): + """避免在引号内切分(如 「わぁ!」って / “好!”他说)。 + + 候选位置之后紧跟的右引号和句末标点一并带走;若候选位于未闭合的引号内, + 或右引号后句子还在继续,则顺延到下一个标点;都不安全时返回 -1,等待更多文本。 + """ + candidates = [pos] + [ + i for i, ch in enumerate(text) if i > pos and ch in punctuations + ] + for candidate in candidates: + end = candidate + 1 + carried_closer = False + while end < len(text) and ( + text[end] in cls._CLOSERS or text[end] in punctuations + ): + carried_closer = carried_closer or text[end] in cls._CLOSERS + end += 1 + segment = text[:end] + if any(segment.count(o) > segment.count(c) for o, c in cls._QUOTE_PAIRS): + continue + if carried_closer and (end == len(text) or not text[end].isspace()): + # 右引号之后句子还在继续(「…」って、“…”他说),或暂时看不到后文 + continue + return end - 1 + return -1 + def _get_segment_text(self): # 合并当前全部文本并处理未分割部分 full_text = "".join(self.tts_text_buff) @@ -503,6 +534,10 @@ class TTSProviderBase(ABC): ): last_punct_pos = pos + if last_punct_pos != -1: + last_punct_pos = self._quote_safe_cut( + current_text, last_punct_pos, punctuations_to_use + ) if last_punct_pos != -1: segment_text_raw = current_text[: last_punct_pos + 1] segment_text = textUtils.get_string_no_punctuation_or_emoji( diff --git a/main/xiaozhi-server/tests/core/providers/__init__.py b/main/xiaozhi-server/tests/core/providers/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/main/xiaozhi-server/tests/core/providers/tts/__init__.py b/main/xiaozhi-server/tests/core/providers/tts/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/main/xiaozhi-server/tests/core/providers/tts/test_quote_safe_segment.py b/main/xiaozhi-server/tests/core/providers/tts/test_quote_safe_segment.py new file mode 100644 index 000000000..0e6927e7c --- /dev/null +++ b/main/xiaozhi-server/tests/core/providers/tts/test_quote_safe_segment.py @@ -0,0 +1,58 @@ +"""TTSProviderBase._get_segment_text must not cut sentences inside quotes.""" +import pytest + +from core.providers.tts.base import TTSProviderBase + + +class _TTS(TTSProviderBase): + async def text_to_speak(self, text, output_file): + return None + + +def _segments(text, step, first_sentence=False): + tts = object.__new__(_TTS) + tts.punctuations = ("。", "?", "?", "!", "!", ";", ";", ":") + tts.first_sentence_punctuations = (",", "~", "、", ",") + tts.punctuations + tts.tts_text_buff, tts.processed_chars = [], 0 + tts.is_first_sentence, tts.tts_stop_request = first_sentence, False + out = [] + for i in range(0, len(text), step): + tts.tts_text_buff.append(text[i:i + step]) + segment = tts._get_segment_text() + if segment: + out.append(segment) + tts.tts_stop_request = True + rest = tts._get_segment_text() + if rest: + out.append(rest) + return out + + +@pytest.mark.parametrize("step", [1, 3, 7]) +def test_japanese_quote_followed_by_tte_stays_whole(step): + assert _segments("思わず「わぁ!」って声が出たんだ。きみは?", step) == [ + "思わず「わぁ!」って声が出たんだ", + "きみは?", + ] + + +@pytest.mark.parametrize("step", [1, 4]) +def test_chinese_quote_followed_by_speaker_stays_whole(step): + segments = _segments("他大声说:“好!”然后笑了。", step) + assert not any(seg.startswith("”") for seg in segments) + # the leading “ is stripped by textUtils, the closing ” must stay with its sentence + assert any("好!”然后笑了" in seg for seg in segments) + + +@pytest.mark.parametrize("step", [1, 5]) +def test_closing_quote_then_period_is_not_left_alone(step): + assert _segments("「すごい!」。次はね、雨!", step) == ["「すごい!」", "次はね、雨"] + + +@pytest.mark.parametrize("step", [1, 5]) +def test_text_without_quotes_is_split_as_before(step): + assert _segments("普通の文。次の文!最後", step) == ["普通の文", "次の文", "最後"] + + +def test_first_sentence_still_splits_at_comma(): + assert _segments("そっか、実習で疲れたんだね。", 1, first_sentence=True)[0] == "そっか"