通过结束字符来分割句子 [英] Splitting a sentence by ending characters
问题描述
最近的一个项目需要我将传入的短语(作为字符串)拆分为它们的组成句子.例如,此字符串:
A recent project has me needing to split incoming phrases (as strings) into their component sentences. For instance, this string:
"Your mother was a hamster, and your father smelt of elderberries! Now go away, or I shall taunt you a second time. You know what, never mind. This entire sentence is far too silly. Wouldn't you agree? I think it is."
需要将其转换为由以下元素组成的列表:
Would need to be turned into a list composed of the following elements:
["Your mother was a hamster, and your father smelt of elderberries",
"Now go away, or I shall taunt you a second time",
"You know what, never mind",
"This entire sentence is far too silly",
"Wouldn't you agree",
"I think it is"]
就此功能而言,句子"是以!
,?
或.
结尾的字符串.请注意,如上所示,应从输出中删除标点符号.
For the purposes of this function, a "sentence" is a string terminated by !
, ?
, or .
Note that punctuation should be removed from the output as shown above.
我有一个工作版本,但是它很丑陋,在开头和结尾留有空格,我不禁想到有更好的方法:
I've got a working version, but it's quite ugly, leaves leading and trailing spaces, and I can't help but think there's a better way:
from functools import reduce
def split_sentences(st):
if type(st) is not str:
raise TypeError("Cannot split non-strings")
sl = st.split('.')
sl = [s.split('?') for s in sl]
sl = reduce(lambda x, y: x+y, sl) #Flatten the list
sl = [s.split('!') for s in sl]
return reduce(lambda x, y: x+y, sl)
推荐答案
使用re.split
代替,可以指定与任何句子结尾字符(以及以下任何空格)匹配的正则表达式.
Use re.split
instead to specify a regular expression matching any sentence-ending character (and any following whitespace).
def split_sentences(st):
sentences = re.split(r'[.?!]\s*', st)
if sentences[-1]:
return sentences
else:
return sentences[:-1]
这篇关于通过结束字符来分割句子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!