diff --git a/main/xiaozhi-server/core/providers/tts/base.py b/main/xiaozhi-server/core/providers/tts/base.py index bc0ac4e24..5ae9ab870 100644 --- a/main/xiaozhi-server/core/providers/tts/base.py +++ b/main/xiaozhi-server/core/providers/tts/base.py @@ -569,7 +569,6 @@ class TTSProviderBase(ABC): # 合并当前全部文本并处理未分割部分 full_text = "".join(self.tts_text_buff) current_text = full_text[self.processed_chars :] # 从未处理的位置开始 - last_punct_pos = -1 # 根据是否是第一句话选择不同的标点符号集合 punctuations_to_use = ( @@ -578,19 +577,19 @@ class TTSProviderBase(ABC): else self.punctuations ) - # 取每种标点最后出现位置的最早者作为切分点(与逐个 rfind 再取最小值等价) - last_seen = {} + # 取最早的合法句末作为切分点 + first_punct_pos = -1 for i, ch in enumerate(current_text): if ch in punctuations_to_use and not self._is_numeric_separator( current_text, i ): - last_seen[ch] = i - last_punct_pos = min(last_seen.values(), default=-1) + first_punct_pos = i + break cut_pos = -1 - if last_punct_pos != -1: + if first_punct_pos != -1: cut_pos = self._quote_safe_cut( - current_text, last_punct_pos, punctuations_to_use + current_text, first_punct_pos, punctuations_to_use ) if cut_pos != -1: segment_text_raw = current_text[: cut_pos + 1] diff --git a/main/xiaozhi-server/core/utils/tts.py b/main/xiaozhi-server/core/utils/tts.py index bfe90e3b8..7460d7a85 100644 --- a/main/xiaozhi-server/core/utils/tts.py +++ b/main/xiaozhi-server/core/utils/tts.py @@ -102,28 +102,66 @@ class MarkdownCleaner: return "\n".join(lines_for_tts) + "\n" + @staticmethod + def _replace_emphasis(m: re.Match) -> str: + """* 斜体:内容是 ASCII 词字符/数字/运算符且左侧紧贴 ASCII 词字符时保留标记。 + + 内容判定区分乘法与强调,左侧判定区分词内与独立强调。 + """ + before = m.string[m.start() - 1] if m.start() > 0 else "" + if not (before.isascii() and before.isalnum()): + return m.group(m.re.groups) + if not re.fullmatch(r"[A-Za-z0-9_+\-*/().\s]+", m.group(1)): + return m.group(m.re.groups) + return m.group(0) + + @staticmethod + def _replace_underscore(m: re.Match) -> str: + """_ 斜体:词内不生效(CommonMark),任一侧紧贴 ASCII 词字符即保留标记。""" + before = m.string[m.start() - 1] if m.start() > 0 else "" + after = m.string[m.end()] if m.end() < len(m.string) else "" + before_word = before.isascii() and before.isalnum() + after_word = after.isascii() and after.isalnum() + if before_word or after_word: + return m.group(0) + return m.group(m.re.groups) + + @staticmethod + def _replace_lone_marker(m: re.Match) -> str: + """落单的 ** :左侧是词字符或参数列表分隔符时保留(幂运算/解包),否则剥掉。""" + before = m.string[m.start() - 1] if m.start() > 0 else "" + if before and (before.isascii() and before.isalnum() or before in "(,"): + return m.group(0) + return "" + # 预编译所有正则表达式(按执行频率排序) - # 这里要把 replace_xxx 的静态方法放在最前定义,以便在列表里能正确引用它们。 + # replace_xxx 的静态方法必须定义在此列表之前,列表里统一取 __func__ REGEXES = [ (re.compile(r'```.*?```', re.DOTALL), ''), # 代码块 (re.compile(r'^#+\s*', re.MULTILINE), ''), # 标题 - (re.compile(r'(\*\*|__)(.*?)\1'), r'\2'), # 粗体 - # 斜体只认成对的分隔符,且不能从字母或数字中间切开。 - # 否则 get_current_time 会被当成 _current_,2*3*4 会被当成 *3*。 - (re.compile(r'(? 1000000),整段数字须独立于标识符 + (re.compile(r'(?+\s*', re.MULTILINE), ''), # 引用 ( re.compile(r'(?P(?:^[^\n]*\|[^\n]*\n)+)', re.MULTILINE), - _replace_table_block + _replace_table_block.__func__ ), - (re.compile(r'^\s*[*+-]\s*', re.MULTILINE), '- '), # 列表 + # 列表,排除 ** 形态 + (re.compile(r'^\s*(?!\*\*)[*+-]\s*', re.MULTILINE), '- '), + # 落单的 ** + (re.compile(r'\*\*'), _replace_lone_marker.__func__), (re.compile(r'\$\$.*?\$\$', re.DOTALL), ''), # 块级公式 ( re.compile(r'(?