You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于预定义句子列表实现文本的自定义拆分?

自定义句子拆分文本的简洁实现

需求:实现一个函数,按照预定义的自定义句子列表拆分文本,这些句子可包含任意特殊字符。

示例输入

text = "My name. is A. His name is B. Her name is C. That's why..."
delims = ["My name. is", "His name is", "Her name is"]

期望函数效果

def custom_sentence_split(text, delims):
     # 实现逻辑
     return result

custom_sentence_split(text, delims)
# 返回结果:["My name. is", "  A. ", "His name is", "  B. ", "Her name is", " C. That's why..."]

初始非简洁实现

之前有一个可行但不够简洁的实现:

def collect_output(text, finds):
    text_copy = text[:]
    retn = []
    for found in finds:
        part1, part2 = text_copy.split(found, 1)
        retn += [part1, found]
        text_copy = part2
    return retn


def custom_sentence_split(text, splitters):
    pattern = "("+"|".join(splitters)+"|)"
    finds = list(filter(bool, re.findall(pattern, text)))
    output = collect_output(text, finds)
    return output

简洁最优实现

最终找到更简洁的可行方案:利用正则的re.split方法,通过re.escape处理分隔符中的特殊字符(避免正则语法冲突),同时用分组保留分隔符本身:

import re

def custom_sentence_split(text, delims):
    pattern = "(" + "|".join(map(re.escape, delims)) + ")"
    # 过滤掉空字符串,匹配结果可能开头出现空值
    return list(filter(bool, re.split(pattern, text)))

# 测试
text = "My name. is A. His name is B. Her name is C. That's why..."
delims = ["My name. is", "His name is", "Her name is"]
print(custom_sentence_split(text, delims))
# 输出:["My name. is", "  A. ", "His name is", "  B. ", "Her name is", " C. That's why..."]

内容的提问来源于stack exchange,提问作者Vova

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 16:52:15