正则将段落分割成句子

这里分割段落不区分中英文标点,你可以根据需求改

分割后标点跟随句子后面

def split_sentences_keep_delimiter(text):
    pattern = r'[^。!!??::;;,,]+[。!!??::;;,,]'
    sentences = re.findall(pattern, text)
    last_sentence = re.sub(r'[。!!??::;;;,,]', '', text)
    if last_sentence and not re.search(pattern, last_sentence):
        sentences.append(last_sentence.strip())
    return sentences[:len(sentences)-1]

在这里插入图片描述

分割后去掉标点只保留文本

import re

def split_text_with_punctuation(text):
    split_sentences = re.split(r'[。.!!??::;;,,]', text)
    return split_sentences

text = "你好,世界!这是个测试。看看是否有效?当然,它会的。"
print(split_text_with_punctuation(text))

在这里插入图片描述

分割后标点和文本分开

import re

def split_text_with_punctuation(text):
    split_sentences = re.split(r'([。.!!??::;;,,])', text)
    return split_sentences

text = "你好,世界!这是个测试。看看是否有效?当然,它会的。"
print(split_text_with_punctuation(text))

在这里插入图片描述

相关推荐

  1. Bert 长段分成句子放在一个batch输入

    2024-05-13 04:40:05       43 阅读
  2. 后端返回的字符串转为对象

    2024-05-13 04:40:05       31 阅读
  3. python数据分析——

    2024-05-13 04:40:05       26 阅读
  4. 匹配/表达式

    2024-05-13 04:40:05       54 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-13 04:40:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-13 04:40:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-13 04:40:05       87 阅读
  4. Python语言-面向对象

    2024-05-13 04:40:05       96 阅读

热门阅读

  1. Qt 类的设计思路详解

    2024-05-13 04:40:05       35 阅读
  2. 8.Redis

    8.Redis

    2024-05-13 04:40:05      35 阅读
  3. 【面经】Linux

    2024-05-13 04:40:05       33 阅读
  4. LeetCode 每日一题 2024/5/6-2024/5/12

    2024-05-13 04:40:05       30 阅读
  5. XMl发展前景及相关领域

    2024-05-13 04:40:05       31 阅读
  6. 第1章 信息系统综合知识 1.4 IT战略

    2024-05-13 04:40:05       32 阅读
  7. HTML学习笔记汇总

    2024-05-13 04:40:05       35 阅读
  8. skynet - spinlock 简单的自旋锁

    2024-05-13 04:40:05       38 阅读