This commit is contained in:
yumoqing 2025-05-25 15:57:45 +00:00
parent ec539b0534
commit 2b2d458877

View File

@ -17,6 +17,7 @@ def split_english_sentences(text):
def split_text_with_dialog_preserved(text): def split_text_with_dialog_preserved(text):
# 正则匹配中英文引号对话 # 正则匹配中英文引号对话
text = ''.join(text.split('\r'))
text = ' '.join(text.split('\n')) text = ' '.join(text.split('\n'))
dialog_pattern = r'([“"](.*?)[”"])' dialog_pattern = r'([“"](.*?)[”"])'
parts = [] parts = []
@ -29,23 +30,19 @@ def split_text_with_dialog_preserved(text):
# 前面的非对话部分按句子分割 # 前面的非对话部分按句子分割
non_dialog = text[last_idx:start] non_dialog = text[last_idx:start]
sentences = re.findall(r'[^。!?!?]*[。!?!?]', non_dialog, re.MULTILINE) sentences = re.findall(r'[^。!?!?]*[。!?!?]', non_dialog, re.MULTILINE)
for s in sentences: print(f'{non_dialog=}, {sentences=}')
txt = s.strip() if len(sentences) == 0:
if txt: sentences = split_english_sentences(non_dialog)
subsentences = split_engish_sentences(txt)
parts.extend([s.strip() for s in sentences if s.strip()]) parts.extend([s.strip() for s in sentences if s.strip()])
# parts.extend([s.strip() for s in sentences if s.strip()])
# 加入整个对话 # 加入整个对话
parts.append(match.group(1).strip()) parts.append(match.group(1).strip())
last_idx = end last_idx = end
# 处理最后一个对话之后的部分 # 处理最后一个对话之后的部分
remaining = text[last_idx:] remaining = text[last_idx:]
sentences = re.findall(r'[^。!?!?]*[。!?!?]', remaining, re.MULTILINE) sentences = re.findall(r'[^。!?!?]*[。!?!?]', remaining, re.MULTILINE)
if len(sentences) == 0:
sentences = split_english_sentences(remaining)
parts.extend([s.strip() for s in sentences if s.strip()]) parts.extend([s.strip() for s in sentences if s.strip()])
return parts return parts