Spaces:
Sleeping
Sleeping
| 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)) | |