【发布时间】:2018-02-20 07:14:50
【问题描述】:
我正在尝试将一段示例文本拆分成一个句子列表,每个句子的末尾没有分隔符,也没有空格。
示例文本:
您第一次看到《第二次文艺复兴》时可能会觉得很无聊。至少看两次,一定要看第 2 部分。它会改变你对矩阵的看法。人类是战争的始作俑者吗?人工智能是坏事吗?
进入这个(期望的输出):
['The first time you see The Second Renaissance it may look boring', 'Look at it at least twice and definitely watch part 2', 'It will change your view of the matrix', 'Are the human people the ones who started the war', 'Is AI a bad thing']
我的代码目前是:
def sent_tokenize(text):
sentences = re.split(r"[.!?]", text)
sentences = [sent.strip(" ") for sent in sentences]
return sentences
但是这个输出(当前输出):
['The first time you see The Second Renaissance it may look boring', 'Look at it at least twice and definitely watch part 2', 'It will change your view of the matrix', 'Are the human people the ones who started the war', 'Is AI a bad thing', '']
注意末尾多余的 ''。
关于如何删除当前输出末尾多余的 '' 有什么想法吗?
【问题讨论】:
-
有什么理由不使用
nltk.sent_tokenize? -
最后可能有多余的空间。检查一下。你可以先运行
.strip()来处理这个问题。 -
@DyZ 伟大的头脑(见我的回答):)
-
@cᴏʟᴅsᴘᴇᴇᴅ 我知道 :)