正则将段落分割成句子

文章介绍了三个Python函数,用于处理文本:split_sentences_keep_delimiter用于保留标点的句子分割,split_text_with_punctuation用于去除标点并分割文本,以及split_text_with_punctuation更细致地将标点与文本分开。这些函数在内容社区和IT技术处理文本数据时非常实用。
摘要由CSDN通过智能技术生成

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

分割后标点跟随句子后面

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))

在这里插入图片描述

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 在 Python 中,您可以使用如下代码来抽取 txt 格式文件中包含关键词的句子段落,并将这些段落保存到另一个 txt 格式文件中。 首先,您需要打开输入文件和输出文件,并读取输入文件的内容: ``` # 打开输入文件 with open('input.txt', 'r') as input_file: # 读取输入文件的内容 input_content = input_file.read() # 打开输出文件 with open('output.txt', 'w') as output_file: # 待会儿会用到输出文件 pass ``` 然后,您可以使用正则表达式来查找包含关键词的句子段落: ``` import re # 定义关键词 keyword = '关键词' # 使用正则表达式查找包含关键词的句子段落 matches = re.findall(r'[^。]+%s[^。]+。' % keyword, input_content) # 打印找到的句子段落 for match in matches: print(match) ``` 最后,您可以将找到的句子段落写入输出文件中: ``` # 写入输出文件 with open('output.txt', 'w') as output_file: for match in matches: output_file.write(match) output_file.write('\n') # 注意换行 ``` 完整代码如下: ``` import re # 打开输入文件 with open('input.txt', 'r') as input_file: # 读取输入文件的内容 input_content = input_file.read() # 定义关键词 keyword = '关键词' ### 回答2: 你好,以下是一个可以用于抽取txt格式文件中包含关键词的句子段落,并将这些段落保存到txt格式文件中的Python代码: ```python import re def extract_sentences_with_keywords(input_file, output_file, keywords): with open(input_file, 'r') as file: content = file.read() sentences = re.split(r'[.!?。!?]', content) filtered_sentences = [sentence.strip() for sentence in sentences if any(keyword in sentence for keyword in keywords)] with open(output_file, 'w') as file: for sentence in filtered_sentences: file.write(sentence + '\n') # 测试代码 input_file = 'input.txt' # 输入文件名 output_file = 'output.txt' # 输出文件名 keywords = ['关键词1', '关键词2', '关键词3'] # 关键词列表 extract_sentences_with_keywords(input_file, output_file, keywords) ``` 在代码中,我们首先打开输入文件并读取其内容。使用正则表达式将内容分割句子,并存储在一个句子列表中。然后,我们筛选出包含任何关键词的句子,并去除每个句子两端的空格。最后,我们打开输出文件并将过滤后的句子逐行写入文件中。 你需要将代码中的`input.txt`替换为包含待处理文本的实际输入文件名,将`output.txt`替换为保存结果的输出文件名,并替换`['关键词1', '关键词2', '关键词3']`为你希望提取的关键词列表。 注意:该代码使用Python正则表达式和文件处理功能来实现上述功能。 ### 回答3: 下面是一个示例Python代码,用于从包含关键词的txt文件中提取句子段落并将其保存到txt文件中: ```python import re def extract_sentences_with_keyword(keyword, input_file, output_file): # 读取输入文件 with open(input_file, 'r', encoding='utf-8') as file: text = file.read() # 按句子分割文本 sentences = re.split('[。!?]', text) # 提取包含关键词的句子段落 output_text = "" for sentence in sentences: if keyword in sentence: output_text += sentence.strip() + "。" # 添加句号 # 保存输出文本到文件 with open(output_file, 'w', encoding='utf-8') as file: file.write(output_text) # 示例用法 keyword = "关键词" input_file = "input.txt" output_file = "output.txt" extract_sentences_with_keyword(keyword, input_file, output_file) ``` 你可以将上述代码保存为一个Python文件,并将`keyword`、`input_file`和`output_file`变量修改为你自己的值。输入文件`input.txt`应包含需要检索的文本。提取出的段落将保存在输出文件`output.txt`中。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值