mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
去掉大模型输出中的特殊字符
This commit is contained in:
parent
1742346dab
commit
70e80418df
2 changed files with 9 additions and 1 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -9,6 +9,7 @@ __pycache__/
|
|||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
data/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
|
|
|
|||
|
|
@ -63,6 +63,13 @@ def is_punctuation_or_emoji(char):
|
|||
]
|
||||
return any(start <= code_point <= end for start, end in emoji_ranges)
|
||||
|
||||
def remove_special_symbols(text):
|
||||
""" 去除大模型输出内容中的特殊字符 """
|
||||
# 用正则匹配所有非字母、非数字、非空白字符
|
||||
cleaned = re.sub(r'[^\w\s]', '', text, flags=re.UNICODE)
|
||||
# 去除下划线(如果不希望保留)
|
||||
cleaned = cleaned.replace('_', '')
|
||||
return cleaned
|
||||
|
||||
def get_string_no_punctuation_or_emoji(s):
|
||||
"""去除字符串首尾的空格、标点符号和表情符号"""
|
||||
|
|
@ -75,7 +82,7 @@ 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 remove_special_symbols(''.join(chars[start:end + 1]))
|
||||
|
||||
|
||||
def remove_punctuation_and_length(text):
|
||||
|
|
|
|||
Loading…
Reference in a new issue