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

如何使用Python按and/or拆分文本得到目标分句列表

Python 分句实现方案

实现逻辑

  • 先做基础文本预处理:将所有字符转小写,移除句末句号,去除首尾多余空格
  • 按句号切割文本得到独立的语句块,过滤掉空块
  • 对每个语句块用正则拆分:匹配不跟在between后面的and以及所有or作为分隔符,避免把between A and B这类固定结构拆断
  • 最后过滤掉拆分得到的分隔符本身和空字符串,整理得到最终列表

代码实现

import re

def split_into_clauses(raw_text):
    # 文本预处理
    processed_text = raw_text.lower().replace('.', '').strip()
    # 按句号拆分独立语句块
    sentence_blocks = [block.strip() for block in processed_text.split('.') if block.strip()]
    clauses = []
    for block in sentence_blocks:
        # 按符合要求的and/or拆分,负向断言排除between后面的and
        split_parts = re.split(r'(?<!between)\s+(and|or)\s+', block)
        for part in split_parts:
            # 过滤掉分隔符and/or和空内容
            if part not in ('and', 'or') and part.strip():
                clauses.append(part.strip())
    return clauses

# 测试
text = 'Turn left and take the door between stairs and elevator. Turn right to the corridor.'
splitted_sentences = split_into_clauses(text)
print(splitted_sentences)

输出结果

['turn left', 'take the door between stairs and elevator', 'turn right to the corridor']

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 06:57:02