mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-23 03:19:41 +00:00
fix: TTS 分句在引号内切开句子 (#3413)
_get_segment_text 会在引号内的标点处切分(如「わぁ!」って、“好!”然后), 把引号拆到两段里,或留下只有「。」的片段。新增 _quote_safe_cut:切分点后的 右引号与句末标点一并带走;位于未闭合引号内或右引号后句子仍在继续时,顺延到 下一个标点。不含引号的文本切分结果不变。
This commit is contained in:
parent
4576e391bb
commit
70ab19d23f
4 changed files with 93 additions and 0 deletions
|
|
@ -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(
|
||||
|
|
|
|||
0
main/xiaozhi-server/tests/core/providers/__init__.py
Normal file
0
main/xiaozhi-server/tests/core/providers/__init__.py
Normal file
0
main/xiaozhi-server/tests/core/providers/tts/__init__.py
Normal file
0
main/xiaozhi-server/tests/core/providers/tts/__init__.py
Normal file
|
|
@ -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] == "そっか"
|
||||
Loading…
Reference in a new issue