去掉大模型输出中的特殊字符

This commit is contained in:
谢博 2025-02-17 20:07:08 +08:00
parent 1742346dab
commit 70e80418df
2 changed files with 9 additions and 1 deletions

1
.gitignore vendored
View file

@ -9,6 +9,7 @@ __pycache__/
# Distribution / packaging # Distribution / packaging
.Python .Python
build/ build/
data/
develop-eggs/ develop-eggs/
dist/ dist/
downloads/ downloads/

View file

@ -63,6 +63,13 @@ def is_punctuation_or_emoji(char):
] ]
return any(start <= code_point <= end for start, end in emoji_ranges) 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): def get_string_no_punctuation_or_emoji(s):
"""去除字符串首尾的空格、标点符号和表情符号""" """去除字符串首尾的空格、标点符号和表情符号"""
@ -75,7 +82,7 @@ def get_string_no_punctuation_or_emoji(s):
end = len(chars) - 1 end = len(chars) - 1
while end >= start and is_punctuation_or_emoji(chars[end]): while end >= start and is_punctuation_or_emoji(chars[end]):
end -= 1 end -= 1
return ''.join(chars[start:end + 1]) return remove_special_symbols(''.join(chars[start:end + 1]))
def remove_punctuation_and_length(text): def remove_punctuation_and_length(text):