python: segmentation of English paragraphs (segmentation of a paragraph of English sentence, segmentation of sentences)

1. Sample code:

Use the split function to split and get a new list

split_sentence.py

# -*- coding: utf-8 -*-
"""
Created on Thu Jan 21 21:57:45 2021
@author: dell
"""

str_centence=input("input:  ")            #input函数获取输入
list_ret = list()

for s_str in str_centence.split('.'):   #对输入进行处理  (用英文结尾句号.来划分句子)
    s_str = s_str.replace('\n','')      #去掉句子中的\n换行
    
    if '?' in s_str:
        list_ret.extend(s_str.split('?'))
    elif '!' in s_str:
        list_ret.extend(s_str.split('!'))
    else:
        list_ret.append(s_str)

for s_str in list_ret:
    #print(s_str+".\n")
    s_str=s_str+".\n"         #每一个完整英语句子加上句号“.”,然后加个换行
    print(s_str)              #输出

 

2. Operation result:

Select a paragraph of English in the pdf document, as follows:

Copy and paste, as input, the output result is as follows:

 

reference:

https://zhuanlan.zhihu.com/p/41804488   teaches you how to divide English paragraphs

 

 

Guess you like

Origin blog.csdn.net/weixin_39450145/article/details/112973381