mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
parent
92227098b7
commit
98a71b38ec
2 changed files with 37 additions and 11 deletions
|
|
@ -4,6 +4,7 @@ import os
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from core.utils.tts import MarkdownCleaner
|
from core.utils.tts import MarkdownCleaner
|
||||||
from core.utils.util import audio_to_opus_data
|
from core.utils.util import audio_to_opus_data
|
||||||
|
from core.utils.tts import TextFormater
|
||||||
|
|
||||||
TAG = __name__
|
TAG = __name__
|
||||||
logger = setup_logging()
|
logger = setup_logging()
|
||||||
|
|
@ -22,7 +23,10 @@ class TTSProviderBase(ABC):
|
||||||
tmp_file = self.generate_filename()
|
tmp_file = self.generate_filename()
|
||||||
try:
|
try:
|
||||||
max_repeat_time = 5
|
max_repeat_time = 5
|
||||||
text = MarkdownCleaner.clean_markdown(text)
|
# 判断清理markdown标记并且判断文本是否是关键字,否则不生成tts
|
||||||
|
text = TextFormater.format_text(text)
|
||||||
|
if text is None or text == "":
|
||||||
|
return None
|
||||||
while not os.path.exists(tmp_file) and max_repeat_time > 0:
|
while not os.path.exists(tmp_file) and max_repeat_time > 0:
|
||||||
try:
|
try:
|
||||||
asyncio.run(self.text_to_speak(text, tmp_file))
|
asyncio.run(self.text_to_speak(text, tmp_file))
|
||||||
|
|
|
||||||
|
|
@ -18,12 +18,22 @@ def create_instance(class_name, *args, **kwargs):
|
||||||
raise ValueError(f"不支持的TTS类型: {class_name},请检查该配置的type是否设置正确")
|
raise ValueError(f"不支持的TTS类型: {class_name},请检查该配置的type是否设置正确")
|
||||||
|
|
||||||
|
|
||||||
class MarkdownCleaner:
|
class TextFormater:
|
||||||
"""
|
"""
|
||||||
封装 Markdown 清理逻辑:直接用 MarkdownCleaner.clean_markdown(text) 即可
|
文本格式化类,用于封装Markdown清理逻辑,直接用 TextFormater.format_text(text)即可
|
||||||
"""
|
"""
|
||||||
# 公式字符
|
# 公式字符
|
||||||
NORMAL_FORMULA_CHARS = re.compile(r'[a-zA-Z\\^_{}\+\-\(\)\[\]=]')
|
__NORMAL_FORMULA_CHARS = re.compile(r'[a-zA-Z\\^_{}\+\-\(\)\[\]=]')
|
||||||
|
# 需要排除的关键字列表
|
||||||
|
__EXCLUDED_KEYWORDS = {'<think>', '</think>'}
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def NORMAL_FORMULA_CHARS(cls):
|
||||||
|
return cls.__NORMAL_FORMULA_CHARS
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def EXCLUDED_KEYWORDS(cls):
|
||||||
|
return cls.__EXCLUDED_KEYWORDS
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _replace_inline_dollar(m: re.Match) -> str:
|
def _replace_inline_dollar(m: re.Match) -> str:
|
||||||
|
|
@ -33,7 +43,7 @@ class MarkdownCleaner:
|
||||||
- 否则 (纯数字/货币等) => 保留 "$...$"
|
- 否则 (纯数字/货币等) => 保留 "$...$"
|
||||||
"""
|
"""
|
||||||
content = m.group(1)
|
content = m.group(1)
|
||||||
if MarkdownCleaner.NORMAL_FORMULA_CHARS.search(content):
|
if TextFormater.NORMAL_FORMULA_CHARS().search(content):
|
||||||
return content
|
return content
|
||||||
else:
|
else:
|
||||||
return m.group(0)
|
return m.group(0)
|
||||||
|
|
@ -79,8 +89,7 @@ class MarkdownCleaner:
|
||||||
|
|
||||||
return "\n".join(lines_for_tts) + "\n"
|
return "\n".join(lines_for_tts) + "\n"
|
||||||
|
|
||||||
# 预编译所有正则表达式(按执行频率排序)
|
# 预编译所有markdown正则表达式(按执行频率排序)
|
||||||
# 这里要把 replace_xxx 的静态方法放在最前定义,以便在列表里能正确引用它们。
|
|
||||||
REGEXES = [
|
REGEXES = [
|
||||||
(re.compile(r'```.*?```', re.DOTALL), ''), # 代码块
|
(re.compile(r'```.*?```', re.DOTALL), ''), # 代码块
|
||||||
(re.compile(r'^#+\s*', re.MULTILINE), ''), # 标题
|
(re.compile(r'^#+\s*', re.MULTILINE), ''), # 标题
|
||||||
|
|
@ -100,13 +109,26 @@ class MarkdownCleaner:
|
||||||
_replace_inline_dollar
|
_replace_inline_dollar
|
||||||
),
|
),
|
||||||
(re.compile(r'\n{2,}'), '\n'), # 多余空行
|
(re.compile(r'\n{2,}'), '\n'), # 多余空行
|
||||||
|
# 排除<think>标签
|
||||||
|
(re.compile(r'<think>.*?</think>', re.DOTALL), ''),
|
||||||
]
|
]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def clean_markdown(text: str) -> str:
|
def _clean_markdown(text: str) -> str:
|
||||||
"""
|
"""
|
||||||
主入口方法:依序执行所有正则,移除或替换 Markdown 元素
|
依序执行所有正则,移除或替换 Markdown 元素
|
||||||
"""
|
"""
|
||||||
for regex, replacement in MarkdownCleaner.REGEXES:
|
for regex, replacement in TextFormater.REGEXES:
|
||||||
text = regex.sub(replacement, text)
|
text = regex.sub(replacement, text)
|
||||||
return text.strip()
|
return text.strip()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def format_text(text: str) -> str | None:
|
||||||
|
"""
|
||||||
|
格式化文本,清理markdown标记
|
||||||
|
:param text: 待格式化的文本
|
||||||
|
:return: 格式化后的文本,如果文本为空或包含关键字,则返回None, 上层逻辑需要处理None的情况
|
||||||
|
"""
|
||||||
|
if not text or text in TextFormater.EXCLUDED_KEYWORDS():
|
||||||
|
return None
|
||||||
|
return TextFormater._clean_markdown(text)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue