This commit is contained in:
yumoqing 2025-05-23 17:41:32 +08:00
parent 81fc51f2e1
commit d61e7ebf09

29
appPublic/textsplit.py Normal file
View File

@ -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