【问题标题】:Python - How to Extract sentences that contains Citation mark?Python - 如何提取包含引文标记的句子?
【发布时间】:2018-01-21 12:13:39
【问题描述】:
text = "Trondheim is a small city with a university and 140000 inhabitants. Its central bus systems has 42 bus lines, serving 590 stations, with 1900 (departures per) day in average. T h a t gives approximately 60000 scheduled bus station passings per day, which is somehow represented in the route data base. The starting point is to automate the function (Garry Weber, 2005) of a route information agent."
print re.findall(r"([^.]*?\(.+ [0-9]+\)[^.]*\.)",text)

我正在使用上面的代码来提取带有引用的句子。如您所见,最后一句包含引用(Garry Weber,2005)。

但我得到了这个结果:

[' Its central bus systems has 42 bus lines, serving 590 stations, with 1900 (departures per) day in average. T h a t gives approximately 60000 scheduled bus station passings per day, which is somehow represented in the route data base. The starting point is to automate the function (Garry Weber, 2005) of a route information agent.']

结果应该是只包含引用的句子,像这样:
起点是自动化路由信息代理的功能(Garry Weber,2005)。

我猜问题是由括号内的文本引起的,正如您在它包含的第二行(出发)中看到的那样,我的代码有什么解决方案吗?

【问题讨论】:

  • 你可能想在这里使用\([^)]+,而不是\(.+
  • 谢谢你解决了我的问题

标签: python regex text-segmentation citations


【解决方案1】:

我的尝试。 Live demo.

\b[^.]+\([^()]+\b(\d{2}|\d{4})\s*\)[^.]*\.

它准确地捕捉了句子,并且比你的年份更具体。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-08
    • 2020-01-19
    • 2013-09-02
    • 2020-12-02
    • 2021-08-13
    • 1970-01-01
    • 2014-07-11
    • 1970-01-01
    相关资源
    最近更新 更多