【问题标题】:PYTHON - How to extract sentences containing citation mark from text file [duplicate]PYTHON - 如何从文本文件中提取包含引文标记的句子[重复]
【发布时间】:2017-08-13 08:30:11
【问题描述】:

例如,我有 3 个句子,如下所示,其中中间 1 个句子包含引用标记 (Warren and Pereira, 1982)。引用总是用这种格式放在括号中:(~string~comma(,)~space~number~)

他住在 Nidarvoll,今晚我必须在 6 点乘火车去奥斯陆。该系统称为 BusTUC,建立在经典系统 CHAT-80(Warren 和 Pereira,1982)之上。 CHAT-80 是最先进的自然语言系统 凭借其自身的优点令人印象深刻。

我使用正则表达式仅提取中间句子,但它会打印所有 3 个句子。 结果应该是这样的:

名为 BusTUC 的系统建立在经典系统 CHAT-80(Warren 和 Pereira,1982 年)的基础上。

【问题讨论】:

  • 总是中间句子还是引用总是在括号中?
  • 并不总是在中间句子,最重要的是引用总是在括号中,这种格式(~string~comma(,)~space~number~)

标签: python regex nlp text-extraction citations


【解决方案1】:

设置... 2句话代表感兴趣的案例:

text = "He lives in Nidarvoll and tonight i must reach a train to Oslo at 6 oclock. The system, called BusTUC is built upon the classical system CHAT-80 (Warren and Pereira, 1982). CHAT-80 was a state of the art natural language system that was impressive on its own merits."

t2 = "He lives in Nidarvoll and tonight i must reach a train to Oslo at 6 oclock. The system, called BusTUC is built upon the classical system CHAT-80 (Warren and Pereira, 1982) fgbhdr was a state of the art natural. CHAT-80 was a state of the art natural language system that was impressive on its own merits."

首先,在引用位于句尾的情况下进行匹配:

p1 = "\. (.*\([A-za-z]+ .* [0-9]+\)\.+?)"

当引文不在句尾时匹配:

p2 = "\. (.*\([A-za-z]+ .* [0-9]+\)[^\.]+\.+?)"

用 `|' 组合这两种情况正则表达式运算符:

p_main = re.compile("\. (.*\([A-za-z]+ .* [0-9]+\)\.+?)"
                "|\. (.*\([A-za-z]+ .* [0-9]+\)[^\.]+\.+?)")

跑步:

>>> print(re.findall(p_main, text))
[('The system, called BusTUC is built upon the classical system CHAT-80 (Warren and Pereira, 1982).', '')]

>>>print(re.findall(p_main, t2))
[('', 'The system, called BusTUC is built upon the classical system CHAT-80 (Warren and Pereira, 1982) fgbhdr was a state of the art natural.')]

在这两种情况下,您都会得到带有引用的句子。

一个很好的资源是 python 正则表达式 documentation 和随附的正则表达式 howto 页面。

干杯

【讨论】:

    【解决方案2】:
    text = "He lives in Nidarvoll and tonight i must reach a train to Oslo at 6 oclock. The system, called BusTUC is built upon the classical system CHAT-80 (Warren and Pereira, 1982). CHAT-80 was a state of the art natural language system that was impressive on its own merits."
    

    您可以将文本拆分成句子列表,然后选择以“)”结尾的句子。

    sentences = text.split(".")[:-1]
    
    for sentence in sentences:
        if sentence[-1] == ")":
            print sentence
    

    【讨论】:

    • 谢谢,所以我不必总是使用正则表达式。但是,如果引文不在句末怎么办?如果周围的句子有这样的字符串“约翰先生”(有点)所以我们不能用“。”分割每个句子
    猜你喜欢
    • 2018-01-21
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 2020-11-30
    • 1970-01-01
    相关资源
    最近更新 更多