【问题标题】:How to extract a certain sentence in a paragraph? Python如何提取段落中的某个句子? Python
【发布时间】:2021-04-30 15:48:22
【问题描述】:

我想从一个段落中提取某些句子,查看一组单词Object C Statement:。段落如下:

对象 A 陈述:有一只猫带着一袋肉。那是个 戴着蓝色帽子的红猫。对象 B 陈述:有一只狗 装满玩具的袋子。那是一只戴着绿帽子的蓝狗。对象 C 陈述:有一只海豚,袋子里装满了泡泡。那是个 戴着橙色帽子的紫色海豚。对象 D 陈述:有一个 满袋草的斑马。那是一匹戴着蓝色帽子的白色斑马。 对象 E 陈述:有一只熊带着一个装满木头的袋子。那是个 戴着黑帽子的棕熊。

我要提取Object C语句:如下:

有一只海豚,袋子里装满了泡泡。那是一个紫色 戴着橙色帽子的海豚。

我遇到的所有示例都是拆分特定单词等。

我试过了,但它对我不起作用:

word="Object A Statement: There was a cat with a bag full of meat. It was a red cat with a blue hat. Object B Statement: There was a dog with a bag full of toys. It was a blue dog with a green hat. Object C Statement: There was a dolphin with a bag full of bubbles. It was a purple dolphin with an orange hat. Object D Statement: There was a zebra with a bag full of grass. It was a white zebra with a blue hat. Object E Statement: There was a bear with a bag full of wood. It was a brown bear with a black hat."
a, b, c, d, e = re.split(r"\B\s(?=[^\s:]+:)", word)
regex = re.compile(r"""Object A Statement\s(.*?)Object B Statement\s(.*?)Object C Statement\s(.*?)Object D Statement\s(.*?)Object E Statement\s(.*)""", re.S|re.X)
a, b, c, d, e = regex.match(word).groups()

【问题讨论】:

    标签: python nlp re


    【解决方案1】:

    你可以用"\s*Object . Statement:\s*"分割字符串

    import re
    
    word="Object A Statement: There was a cat with a bag full of meat. It was a red cat with a blue hat. Object B Statement: There was a dog with a bag full of toys. It was a blue dog with a green hat. Object C Statement: There was a dolphin with a bag full of bubbles. It was a purple dolphin with an orange hat. Object D Statement: There was a zebra with a bag full of grass. It was a white zebra with a blue hat. Object E Statement: There was a bear with a bag full of wood. It was a brown bear with a black hat."
    result = re.split(r"\s*Object . Statement:\s*", word)
    result = [r for r in result if len(r) > 0]
    print("\n".join(result))
    

    我得到以下结果。

    There was a cat with a bag full of meat. It was a red cat with a blue hat.
    There was a dog with a bag full of toys. It was a blue dog with a green hat.
    There was a dolphin with a bag full of bubbles. It was a purple dolphin with an orange hat.
    There was a zebra with a bag full of grass. It was a white zebra with a blue hat.
    There was a bear with a bag full of wood. It was a brown bear with a black hat.
    

    【讨论】:

    • 但是如何得到那个特定的句子呢?有一只海豚,袋子里装满了泡泡。那是一只戴着橙色帽子的紫色海豚。
    • 我猜你可以用 "\.\s+" 再次拆分结果中的字符串
    • 答案很好。你的例句是 result[2]
    • 知道了。感谢您的提示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    • 2018-05-06
    • 2012-01-17
    • 1970-01-01
    • 2022-12-04
    • 2021-08-28
    相关资源
    最近更新 更多