带有空格的名词短语 [英] Noun phrases with spacy

查看:14
本文介绍了带有空格的名词短语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Spacy从文本中提取名词短语?
我指的不是词性标签。 在文档中,我找不到任何有关名词短语或常规语法分析树的内容。

推荐答案

如果需要基本NP,即没有并列的NP、介词短语或关系从句,可以在Doc和Span对象上使用noun_chunks迭代器:

>>> from spacy.en import English
>>> nlp = English()
>>> doc = nlp(u'The cat and the dog sleep in the basket near the door.')
>>> for np in doc.noun_chunks:
>>>     np.text
u'The cat'
u'the dog'
u'the basket'
u'the door'

如果您需要其他东西,最好的方法是迭代句子中的单词,并考虑句法上下文以确定单词是否支配您想要的短语类型。如果是,则生成其子树:

from spacy.symbols import *

np_labels = set([nsubj, nsubjpass, dobj, iobj, pobj]) # Probably others too
def iter_nps(doc):
    for word in doc:
        if word.dep in np_labels:
            yield word.subtree

这篇关于带有空格的名词短语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆