news 2026/4/22 12:25:27

Python段落分割并保留句子标点的示例

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Python段落分割并保留句子标点的示例

在Python中,将段落分割成句子并保留结尾标点符号有多种方法。

这里尝试示例以下是几种常用的方法,所用例子收集和修改自网络资料。

1 正则方案

纯中文文本可以使用正则表达式,以下是两个正则表达式分割示例。

1.1 基础版分割

正则表达式是最常用的句子分割手段,示例如下。

import re def split_paragraph_to_sentences(paragraph): """ 将段落分割成句子,保留结尾标点符号 支持中文和英文 """ # 正则表达式匹配句子结束符:。!?;.!?;(以及可能的后引号) pattern = r'(?<=[。!?;.!?;])\s*' sentences = re.split(pattern, paragraph.strip()) # 过滤空字符串 sentences = [s.strip() for s in sentences if s.strip()] return sentences # 示例 paragraph = "这是一个测试段落。这是第二句话!这是第三句话?让我们继续。结尾标点.最后一句。" sentences = split_paragraph_to_sentences(paragraph) for i, sentence in enumerate(sentences, 1): print(f"句子{i}: {sentence}")

输出如下:

句子1: 这是一个测试段落。
句子2: 这是第二句话!
句子3: 这是第三句话?
句子4: 让我们继续。
句子5: 结尾标点.
句子6: 最后一句。

1.2 更精细的正则分割

这里尝试更精细的正则分割,示例代码如下。

import re def split_sentences_advanced(text): """ 更精细的句子分割,处理特殊情况 """ # 处理缩写、小数点等特殊情况 pattern = r'(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?|!|。|!|?|;|;)\s' sentences = re.split(pattern, text) # 处理可能的分割后空白 sentences = [s.strip() for s in sentences if s.strip()] return sentences # 示例 paragraph = "Dr. Smith went to the store. He bought apples, oranges, etc. The total was $12.50. Was that expensive?" sentences = split_sentences_advanced(paragraph) for i, sentence in enumerate(sentences, 1): print(f"句子{i}: {sentence}")

输出如下

句子1: Dr. Smith went to the store.
句子2: He bought apples, oranges, etc.
句子3: The total was $12.50.
句子4: Was that expensive?

2 NLTK方案

NLTK库适合对英文文档进行分割,需要提前安装punkt资源,示例代码如下。

import nltk # 第一次使用时需要下载punkt资源 # nltk.download('punkt') def split_sentences_nltk(text): """使用NLTK进行句子分割(主要针对英文)""" from nltk.tokenize import sent_tokenize return sent_tokenize(text) # 示例 english_paragraph = "Hello world! This is a test. How are you? I'm fine, thank you." sentences = split_sentences_nltk(english_paragraph) for i, sentence in enumerate(sentences, 1): print(f"句子{i}: {sentence}")

输出示例如下

1. Dr. Smith met Mr. Jones at 5 p.m.
2. They discussed the project.
3. It was great!

3 综合方案

以下是多种综合方案,兼容中英文处理等多种特殊情况。

3.1 多级分割处理

如果段落混杂中文和英文,可以采用多级分割方式,示例如下。

import re def split_mixed_language_paragraph(paragraph): """ 处理混合中英文的段落分割 """ # 结合中文和英文的句子结束符 pattern = r'(?<=[。!?;.!?;])\s*(?![a-zA-Z0-9])' sentences = re.split(pattern, paragraph) # 二次处理:对于英文句子,使用更精确的模式 refined_sentences = [] for sentence in sentences: if sentence.strip(): # 如果句子中包含英文标点,进一步分割 if re.search(r'[.!?]', sentence) and len(sentence) > 50: sub_sentences = re.split(r'(?<=[.!?])\s+(?=[A-Z])', sentence) refined_sentences.extend([s.strip() for s in sub_sentences if s.strip()]) else: refined_sentences.append(sentence.strip()) return refined_sentences # 示例 mixed_paragraph = "这是一个测试。Hello world! 这是中文句子。How are you? 我很好!" sentences = split_mixed_language_paragraph(mixed_paragraph) for i, sentence in enumerate(sentences, 1): print(f"句子{i}: {sentence}")

输出示例如下

句子1: 这是一个测试。Hello world!
句子2: 这是中文句子。How are you?
句子3: 我很好!

3.2 特殊标记添加

进一步支持添加特殊标记,示例代码如下所示。

import re class SentenceSplitter: def __init__(self): # 常见缩写列表,防止错误分割 self.abbreviations = { 'mr.', 'mrs.', 'ms.', 'dr.', 'prof.', 'rev.', 'hon.', 'st.', 'ave.', 'blvd.', 'rd.', 'ln.', 'etc.', 'e.g.', 'i.e.', 'vs.', 'jan.', 'feb.', 'mar.', 'apr.', 'jun.', 'jul.', 'aug.', 'sep.', 'oct.', 'nov.', 'dec.' } def split(self, text): """主分割方法""" if not text.strip(): return [] # 预处理:在可能被错误分割的缩写后添加特殊标记 text = self._protect_abbreviations(text) # 分割句子 pattern = r'(?<=[。!?.!?])\s+' sentences = re.split(pattern, text) # 恢复被保护的缩写 sentences = [self._restore_abbreviations(s.strip()) for s in sentences if s.strip()] return sentences def _protect_abbreviations(self, text): """保护缩写不被错误分割""" import re def replace_abbr(match): abbr = match.group(0).lower() if abbr in self.abbreviations: return match.group(0).replace('.', '[DOT]') return match.group(0) # 匹配可能的小写缩写 pattern = r'\b[a-z]+\.' text = re.sub(pattern, replace_abbr, text, flags=re.IGNORECASE) return text def _restore_abbreviations(self, text): """恢复被保护的缩写""" return text.replace('[DOT]', '.') # 使用示例 splitter = SentenceSplitter() paragraph = "Dr. Smith met Mr. Jones at 5 p.m. They discussed the project. It was great!" sentences = splitter.split(paragraph) for i, sentence in enumerate(sentences, 1): print(f"{i}. {sentence}")

输出如下所示

1. Dr. Smith met Mr. Jones at 5 p.m.
2. They discussed the project.
3. It was great!

3.3 spaCy示例

另外,可以使用spacy进行句子分割,适合对纯英文文本进行分割。

# 需要先安装:pip install spacy # 下载模型:python -m spacy download en_core_web_sm import spacy def split_sentences_spacy(text, language='en'): """使用spaCy进行句子分割""" if language == 'en': nlp = spacy.load('en_core_web_sm') else: # 对于中文,需要中文模型 # pip install spacy zh_core_web_sm # python -m spacy download zh_core_web_sm nlp = spacy.load('zh_core_web_sm') doc = nlp(text) return [sent.text.strip() for sent in doc.sents] # 示例 text = "This is the first sentence. This is the second one! And here's the third?" sentences = split_sentences_spacy(text) for i, sent in enumerate(sentences, 1): print(f"句子{i}: {sent}")

3.4 综合示例

如果混合中英文,也可以采用如下的综合分割方法。

这是一个综合分割示例,可以选择分割方法,所支持的语言等。

def split_paragraph(paragraph, method='auto', language='mixed'): """ 综合句子分割函数 参数: paragraph: 输入的段落文本 method: 分割方法,可选 'auto', 'regex', 'nltk', 'spacy' language: 语言,可选 'zh', 'en', 'mixed' 返回: 句子列表 """ if not paragraph or not paragraph.strip(): return [] if method == 'auto': # 根据语言自动选择方法 if language == 'en': try: from nltk.tokenize import sent_tokenize return sent_tokenize(paragraph) except: method = 'regex' else: method = 'regex' if method == 'regex': if language == 'zh': pattern = r'(?<=[。!?;])\s*' elif language == 'en': pattern = r'(?<=[.!?])\s+(?=[A-Z])' else: # mixed pattern = r'(?<=[。!?.!?;])\s*' sentences = re.split(pattern, paragraph.strip()) return [s.strip() for s in sentences if s.strip()] elif method == 'nltk': from nltk.tokenize import sent_tokenize return sent_tokenize(paragraph) elif method == 'spacy': import spacy nlp = spacy.load('en_core_web_sm' if language == 'en' else 'zh_core_web_sm') doc = nlp(paragraph) return [sent.text.strip() for sent in doc.sents] return [] # 使用示例 paragraph = "这是一个测试。Hello world! 第二句话?结束。" sentences = split_paragraph(paragraph, method='regex', language='mixed') print("分割结果:", sentences)

reference

---

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/18 0:12:05

buck电路图及其原理:TPS5430典型应用电路分析

深入剖析TPS5430降压电路&#xff1a;从原理到实战设计你有没有遇到过这样的问题&#xff1f;系统需要将12V或24V的工业电源转换为稳定的5V或3.3V给MCU供电&#xff0c;但用LDO时芯片烫得像个小暖手宝&#xff1f;效率低、发热大、散热空间又有限——这正是开关电源登场的时刻。…

作者头像 李华
网站建设 2026/4/21 17:11:03

Markdown写技术博客:记录你的PyTorch模型实验过程

Markdown写技术博客&#xff1a;记录你的PyTorch模型实验过程 在深度学习的日常研发中&#xff0c;你是否曾因为“环境配了三天还跑不起来”而焦虑&#xff1f;是否遇到过论文复现时提示 CUDA out of memory 或 ImportError: libcudart.so 的经典难题&#xff1f;更别提团队协…

作者头像 李华
网站建设 2026/4/19 1:08:19

使用screen命令保持PyTorch训练任务持续运行

使用 screen 命令保持 PyTorch 训练任务持续运行 在深度学习的实际开发中&#xff0c;一个令人头疼的场景再熟悉不过&#xff1a;你启动了一个长达 24 小时的模型训练任务&#xff0c;满怀期待地离开电脑&#xff0c;结果半夜网络波动导致 SSH 断开——第二天打开终端一看&…

作者头像 李华
网站建设 2026/4/19 0:36:33

阿里云DataV 简介

阿里云DataV是一款专业的数据可视化产品&#xff0c;专注于构建企业级数据大屏。其核心能力与特点包括&#xff1a;核心能力低代码可视化开发通过拖拽式操作快速搭建动态数据看板&#xff0c;支持实时数据接入与动态更新。多源数据集成兼容主流数据库&#xff08;MySQL、MaxCom…

作者头像 李华
网站建设 2026/4/18 11:50:16

2025AI写论文软件排行榜:一键生成论文免费工具,查重率低至5%!

当你对着AI写博士论文、AI写硕士论文、AI写MBA论文的任务清单犯愁&#xff0c;选题卡壳、文献筛到眼花、降重改到词穷、排版调到手麻时&#xff0c;就会懂这种抓心挠肝的滋味。学术写作的每道关卡都藏着看不见的消耗&#xff0c;不是熬几个通宵就能轻松通关的。好在高效的AI论文…

作者头像 李华
网站建设 2026/4/20 18:25:44

Conda vs Pip:在PyTorch环境中应该用哪个?

Conda 与 Pip&#xff1a;如何为 PyTorch 环境选择最优包管理策略&#xff1f; 在深度学习项目中&#xff0c;环境配置常常比写模型代码更耗时。你是否曾遇到过这样的场景&#xff1a;明明安装了 PyTorch&#xff0c;torch.cuda.is_available() 却返回 False&#xff1f;或者切…

作者头像 李华