【问题标题】:NLP - information extraction in Python (spaCy)NLP - Python 中的信息提取 (spaCy)
【发布时间】:2016-11-06 19:21:20
【问题描述】:

我正在尝试从以下段落结构中提取此类信息:

 women_ran men_ran kids_ran walked
         1       2        1      3
         2       4        3      1
         3       6        5      2

text = ["On Tuesday, one women ran on the street while 2 men ran and 1 child ran on the sidewalk. Also, there were 3 people walking.", "One person was walking yesterday, but there were 2 women running as well as 4 men and 3 kids running.", "The other day, there were three women running and also 6 men and 5 kids running on the sidewalk. Also, there were 2 people walking in the park."]

我使用 Python 的 spaCy 作为我的 NLP 库。我是 NLP 工作的新手,希望获得一些指导,了解从此类句子中提取此表格信息的最佳方法。

如果只是识别是否有人在跑步或走路,我会使用sklearn 来拟合分类模型,但我需要提取的信息显然比这更细化(我正在尝试检索每个子类别和值)。任何指导将不胜感激。

【问题讨论】:

    标签: python nlp information-extraction spacy


    【解决方案1】:

    您需要为此使用依赖项解析。您可以使用the displaCy visualiser 看到您的例句的可视化。

    您可以通过几种不同的方式实现您需要的规则——就像总是有多种方式来编写 XPath 查询、DOM 选择器等。

    这样的事情应该可以工作:

    nlp = spacy.load('en')
    docs = [nlp(t) for t in text]
    for i, doc in enumerate(docs):
        for j, sent in enumerate(doc.sents):
            subjects = [w for w in sent if w.dep_ == 'nsubj']
            for subject in subjects:
                numbers = [w for w in subject.lefts if w.dep_ == 'nummod']
                if len(numbers) == 1:
                    print('document.sentence: {}.{}, subject: {}, action: {}, numbers: {}'.format(i, j, subject.text, subject.head.text, numbers[0].text))
    

    对于text 中的示例,您应该得到:

    document.sentence: 0.0, subject: men, action: ran, numbers: 2
    document.sentence: 0.0, subject: child, action: ran, numbers: 1
    document.sentence: 0.1, subject: people, action: walking, numbers: 3
    document.sentence: 1.0, subject: person, action: walking, numbers: One
    

    【讨论】:

    • 我从未编写过 XPath 查询或 DOM 选择器。你能解释一下吗?
    • @kathystehl XPath 指定 XML (HTML) 文档中的位置。因此,XPath 查询是一种在 XML 或 HTML 中查找特定元素的方法。见wikpedia。 DOM 选择器是 HTML 文档中的任何 CSS 元素 idclass(DOM 是您在 javascript 等中使用的 HTML/XML 文档/树的数据结构)。所以你可以通过 id 和 class 过滤来查找元素。在 NLP 中,依赖解析器将非结构化文本转换为类似于 HTML 的树形数据结构,其标签可以像这样使用 DOM 选择器过滤器和 XPath 查询进行查询。
    猜你喜欢
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 2021-12-28
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多