GREEK_DENG / test_clean.py
陈浩然
模型架构重大升级:全面接入 Meta NLLB-200-600M 现代多语言大模型,废除旧版 MarianMT 与全部生硬正则,实现标准全角标点与地道流畅中文
6e77d42
Raw
History Blame Contribute Delete
1.28 kB
import re
def clean_chinese(text: str) -> str:
cleaned = re.sub(r'[a-zA-Z\'\-]{3,}', '', text)
cleaned = re.sub(r'[^\u4e00-\u9fff\u3000-\u303f\uff00-\uffef,。!?、;:""''()—…\s]', '', cleaned)
cleaned = re.sub(r'\s{2,}', ' ', cleaned)
cleaned = cleaned.strip()
cleaned = re.sub(r',\s*(并且|而且|但是|可是|然而|而|因此|所以|于是|因为|由于|虽然)', r'。\1', cleaned)
segments = cleaned.split(',')
result = []
current_len = 0
for i, seg in enumerate(segments):
result.append(seg)
current_len += len(seg)
if i < len(segments) - 1:
if current_len > 12:
result.append('。')
current_len = 0
else:
result.append(',')
cleaned = "".join(result)
cleaned = re.sub(r'。+', '。', cleaned)
cleaned = cleaned.replace('。,', '。').replace(',。', '。')
if cleaned and cleaned[-1] not in ['。', '!', '?', '”']:
cleaned += '。'
if len(cleaned) < 5:
return ""
return cleaned
test_str = "这是一个测试句子,看看长句能不能被正确打断,并且这里的逻辑会不会导致空字符串。"
print("Result:", clean_chinese(test_str))