From d61e7ebf091d6bd2d0a743dd2210dac55a1b3cd1 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Fri, 23 May 2025 17:41:32 +0800 Subject: [PATCH] bugfix --- appPublic/textsplit.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 appPublic/textsplit.py diff --git a/appPublic/textsplit.py b/appPublic/textsplit.py new file mode 100644 index 0000000..894f790 --- /dev/null +++ b/appPublic/textsplit.py @@ -0,0 +1,29 @@ +import re + +def split_text_with_dialog_preserved(text): + # 正则匹配中英文引号对话 + dialog_pattern = r'([“"](.*?)[”"])' + parts = [] + last_idx = 0 + + # 先提取所有对话,保证不被切割 + for match in re.finditer(dialog_pattern, text, flags=re.DOTALL): + start, end = match.span() + + # 前面的非对话部分按句子分割 + non_dialog = text[last_idx:start] + sentences = re.findall(r'[^。!?!?]*[。!?!?]', non_dialog, re.MULTILINE) + parts.extend([s.strip() for s in sentences if s.strip()]) + + # 加入整个对话 + parts.append(match.group(1).strip()) + + last_idx = end + + # 处理最后一个对话之后的部分 + remaining = text[last_idx:] + sentences = re.findall(r'[^。!?!?]*[。!?!?]', remaining, re.MULTILINE) + parts.extend([s.strip() for s in sentences if s.strip()]) + + return parts +