【问题标题】:Splitting text into sentences using regex in Python [duplicate]在 Python 中使用正则表达式将文本拆分为句子 [重复]
【发布时间】: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ᴘᴇᴇᴅ 我知道 :)

标签: python regex split


【解决方案1】:

nltk's sent_tokenize

如果您从事 NLP 业务,我强烈推荐 sent_tokenize 来自 nltk 包。

>>> from nltk.tokenize import sent_tokenize
>>> sent_tokenize(text)
[
    '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?'
] 

它比正则表达式强大得多,并且提供了很多选项来完成工作。更多信息请访问official documentation

如果您对尾随分隔符很挑剔,您可以使用 nltk.tokenize.RegexpTokenizer 与稍有不同的模式:

>>> from nltk.tokenize import RegexpTokenizer
>>> tokenizer = RegexpTokenizer(r'[^.?!]+')
>>> list(map(str.strip, tokenizer.tokenize(text)))    
[
    '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'
]

基于正则表达式的re.split

如果您必须使用regex,那么您需要通过添加否定前瞻来修改您的模式 -

>>> list(map(str.strip, re.split(r"[.!?](?!$)", text)))
[
    '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?'
]

添加的(?!$) 指定仅在尚未到达行尾时才拆分。不幸的是,我不确定是否可以在不执行result[-1] = result[-1][:-1] 之类的操作的情况下合理地删除最后一句的尾随分隔符。

【讨论】:

  • “无分隔符”。查看所需的输出。
  • @ᴡʜᴀᴄᴋᴀᴍᴀᴅᴏᴏᴅʟᴇ3000 imo,这是一个小细节。不过我会看看我能做些什么。
  • @ᴡʜᴀᴄᴋᴀᴍᴀᴅᴏᴏᴅʟᴇ3000 我在 RegexpTokenizer 中添加了一个选项来解决这个问题。希望现在没事!
  • 我必须使用正则表达式,无法访问 nltk 包。正则表达式答案有效但留下了'?在最后一句话的结尾。
  • (?<!$) 是一个后视(参见选项 2 描述)。您可能想要使用前瞻,但 (?<!$) = (?!$) 因为 $ 是零宽度断言。
【解决方案2】:

您可以使用过滤器来删除空元素

例如:

import re
text = """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 filter(None, sentences)

print sent_tokenize(text)

【讨论】:

    【解决方案3】:

    关于如何删除当前结尾处多余的 '' 的任何想法 输出?

    你可以这样做删除它:

    sentences[:-1]
    

    或更快(由ᴄᴏʟᴅsᴘᴇᴇᴅ)

    del result[-1]
    

    输出:

    ['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']
    

    【讨论】:

      【解决方案4】:

      您可以先strip您的段落,然后再拆分它,或者过滤掉结果中的空字符串。

      【讨论】:

        猜你喜欢
        • 2014-11-02
        • 2016-08-04
        • 2014-03-24
        • 2013-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-31
        • 1970-01-01
        相关资源
        最近更新 更多