diff --git a/appPublic/textsplit.py b/appPublic/textsplit.py index d2116d0..adb979b 100644 --- a/appPublic/textsplit.py +++ b/appPublic/textsplit.py @@ -17,6 +17,7 @@ def split_english_sentences(text): def split_text_with_dialog_preserved(text): # 正则匹配中英文引号对话 + text = ''.join(text.split('\r')) text = ' '.join(text.split('\n')) dialog_pattern = r'([“"](.*?)[”"])' parts = [] @@ -29,23 +30,19 @@ def split_text_with_dialog_preserved(text): # 前面的非对话部分按句子分割 non_dialog = text[last_idx:start] sentences = re.findall(r'[^。!?!?]*[。!?!?]', non_dialog, re.MULTILINE) - for s in sentences: - txt = s.strip() - if txt: - 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()]) - + print(f'{non_dialog=}, {sentences=}') + if len(sentences) == 0: + sentences = split_english_sentences(non_dialog) + 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) + if len(sentences) == 0: + sentences = split_english_sentences(remaining) parts.extend([s.strip() for s in sentences if s.strip()]) - return parts