【问题标题】:Splitting text into sentences using Python [duplicate]使用 Python 将文本拆分为句子 [重复]
【发布时间】:2017-03-28 16:45:26
【问题描述】:

每当出现终端标点符号('.'、'!'、'?')时,我都会尝试将文本拆分为句子。例如,如果我有以下文字:

认识到耶路撒冷风险投资伙伴开启的不断增加的机会 建立他们的 Cyber​​ Labs 孵化器,为这座城市的许多人提供一个家 有前途的年轻公司。像 EM​​C 这样的国际公司也 在园区内建立主要中心,为他人带路 关注! 去年六月参观时,公园已经发展到两个 为建造更多建筑而破土的建筑物 不远的未来。这真的是有趣! 你怎么看?

这应该分成5个句子(见上面的粗体字,因为这些词以标点符号结尾)。

这是我的代码:

# split on: '.+'
    splitted_article_content = []
    # article_content contains all the article's paragraphs
    for element in article_content:
        splitted_article_content = splitted_article_content +re.split(".(?='.'+)", element)

    # split on: '?+'
    splitted_article_content_2 = []
    for element in splitted_article_content:
        splitted_article_content_2 = splitted_article_content_2 + re.split(".(?='?'+)", element)

    # split on: '!+'
    splitted_article_content_3 = []
    for element in splitted_article_content_2:
            splitted_article_content_3 = splitted_article_content_3 + re.split(".(?='!'+)", element)

我的问题是,是否有任何其他有效的方法来执行以下操作,使用任何外部库?

感谢大家的帮助。

【问题讨论】:

  • 但是...您没有使用任何外部库,因为re 是 Python 标准库的一部分。
  • 你就不能re.split(r'[\.!?] ', article)吗?
  • @RocketHazmat article_content 是段落列表.. split 将在列表中起作用?
  • 然后试试:splitted_article_content = [re.split(r'[\.!?] ', element) for element in article_content]。重点是,您只需要 一个 正则表达式,而不是三个。或者是否有任何特定原因需要将其分成 3 个列表?就像句子以.?! 结尾有关系吗?

标签: python


【解决方案1】:

我想我认为这更像是向后看而不是向前看:

import re

# article_content contains all the article's paragraphs
# in this case, a single paragraph.

article_content = ["""Recognizing the rising opportunity Jerusalem Venture Partners opened up their Cyber Labs incubator, giving a home to many of the city’s promising young companies. International corporates like EMC have also established major centers in the park, leading the way for others to follow! On a visit last June, the park had already grown to two buildings with the ground being broken for the construction of more in the near future. This is really interesting! What do you think?"""]

split_article_content = []

for element in article_content:
    split_article_content += re.split("(?<=[.!?])\s+", element)

print(*split_article_content, sep='\n\n')

输出

% python3 test.py
Recognizing the rising opportunity Jerusalem Venture Partners opened up their Cyber Labs incubator, giving a home to many of the city’s promising young companies.

International corporates like EMC have also established major centers in the park, leading the way for others to follow!

On a visit last June, the park had already grown to two buildings with the ground being broken for the construction of more in the near future.

This is really interesting!

What do you think?
% 

【讨论】:

  • 我需要将此输入写入文件,我该怎么做?
  • @JayMar,请说得更具体一点,您是想从文件中输入段落还是将句子输出到文件中?如果这是与您提出的问题不同的问题,您可以考虑使用累积的代码发布一个新问题。
  • 没关系,我已经设法将句子输出到文件中。感谢您的帮助。
  • 我可以排除数字之间的点,例如“Pi 是 3.14”。这不应该分成两句话,而是一个。
  • 你试过了吗?我提供的代码适用于本示例,因为它不会在句点上拆分,而是在空格前的句点上拆分。所以没问题。然而,一些奇怪的形式,“我向后走了5。英尺”。是个问题。你的问题。
猜你喜欢
  • 1970-01-01
  • 2011-11-03
  • 1970-01-01
  • 2014-11-02
  • 2013-04-28
  • 1970-01-01
  • 1970-01-01
  • 2014-02-11
  • 1970-01-01
相关资源
最近更新 更多