Python中的NLP信息提取(spaCy)

9

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

 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工作比较新,希望能得到一些指导,了解从这些句子中提取表格信息的最佳方法。
如果只是识别是否有人在跑步或散步,我会使用sklearn来拟合分类模型,但我需要提取的信息显然更加细致(我正在尝试检索每个子类别和其值)。任何指导都将不胜感激。
1个回答

13
你需要使用依赖解析来完成这个任务。你可以通过displaCy可视化工具查看你的示例句子。
你可以用几种不同的方法来实现需要的规则,就像编写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))

对于您在文本中的示例,您应该获得:

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
1
XPath指定XML(HTML)文档中的位置。因此,XPath查询是在XML或HTML中查找特定元素的一种方法。请参见wikipedia。DOM选择器是HTML文档中任何CSS元素idclass(DOM是HTML / XML文档/树的数据结构,在javascript等中使用)。因此,您可以通过ID和类进行过滤以查找元素。在NLP中,依赖关系分析器将非结构化文本转换为类似于HTML的树形数据结构,并具有可以使用DOM选择器过滤器和XPath查询进行查询的标签。 - hobs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接