Merge pull request #1515 from xinnan-tech/hot-fix

update:优化带有方括号的字符语音转换
This commit is contained in:
欣南科技 2025-06-09 12:36:24 +08:00 committed by GitHub
commit f5c3c51fe2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 27 additions and 11 deletions

View file

@ -2,6 +2,7 @@ import time
import json
import random
import asyncio
from core.utils.dialogue import Message
from core.utils.util import audio_to_data
from core.handle.sendAudioHandle import sendAudioMessage, send_stt_message
from core.utils.util import remove_punctuation_and_length, opus_datas_to_wav_bytes
@ -79,6 +80,9 @@ async def checkWakeupWords(conn, text):
await sendAudioMessage(conn, SentenceType.FIRST, opus_packets, response["text"])
await sendAudioMessage(conn, SentenceType.LAST, [], None)
# 补充对话
conn.dialogue.put(Message(role="assistant", content=response["text"]))
# 检查是否需要更新唤醒词回复
if time.time() - response["time"] > WAKEUP_CONFIG["refresh_time"]:
if not _wakeup_response_lock.locked():

View file

@ -9,26 +9,38 @@ def get_string_no_punctuation_or_emoji(s):
end = len(chars) - 1
while end >= start and is_punctuation_or_emoji(chars[end]):
end -= 1
return ''.join(chars[start:end + 1])
return "".join(chars[start : end + 1])
def is_punctuation_or_emoji(char):
"""检查字符是否为空格、指定标点或表情符号"""
# 定义需要去除的中英文标点(包括全角/半角)
punctuation_set = {
',', ',', # 中文逗号 + 英文逗号
'。', '.', # 中文句号 + 英文句号
'!', '!', # 中文感叹号 + 英文感叹号
'-', '-', # 英文连字符 + 中文全角横线
'、' # 中文顿号
",",
",", # 中文逗号 + 英文逗号
"。",
".", # 中文句号 + 英文句号
"!",
"!", # 中文感叹号 + 英文感叹号
"-",
"-", # 英文连字符 + 中文全角横线
"、", # 中文顿号
"[",
"]", # 方括号
"【",
"】", # 中文方括号
}
if char.isspace() or char in punctuation_set:
return True
# 检查表情符号(保留原有逻辑)
code_point = ord(char)
emoji_ranges = [
(0x1F600, 0x1F64F), (0x1F300, 0x1F5FF),
(0x1F680, 0x1F6FF), (0x1F900, 0x1F9FF),
(0x1FA70, 0x1FAFF), (0x2600, 0x26FF),
(0x2700, 0x27BF)
(0x1F600, 0x1F64F),
(0x1F300, 0x1F5FF),
(0x1F680, 0x1F6FF),
(0x1F900, 0x1F9FF),
(0x1FA70, 0x1FAFF),
(0x2600, 0x26FF),
(0x2700, 0x27BF),
]
return any(start <= code_point <= end for start, end in emoji_ranges)
return any(start <= code_point <= end for start, end in emoji_ranges)