我可以在python中使用spacy来查找特定邻居的NP吗?我希望我的文本中的名词短语在其之前和之后都有动词。
答案 0 :(得分:6)
分析依赖关系解析树,并查看相邻令牌的POS。
>>> import spacy
>>> nlp = spacy.load('en')
>>> sent = u'run python program run, to make this work'
>>> parsed = nlp(sent)
>>> list(parsed.noun_chunks)
[python program]
>>> for noun_phrase in list(parsed.noun_chunks):
... noun_phrase.merge(noun_phrase.root.tag_, noun_phrase.root.lemma_, noun_phrase.root.ent_type_)
...
python program
>>> [(token.text,token.pos_) for token in parsed]
[(u'run', u'VERB'), (u'python program', u'NOUN'), (u'run', u'VERB'), (u',', u'PUNCT'), (u'to', u'PART'), (u'make', u'VERB'), (u'this', u'DET'), (u'work', u'NOUN')]
通过分析相邻令牌的POS,您可以获得所需的名词短语。
答案 1 :(得分:1)
如果您想使用合并短语来重新标记,我更喜欢这个(而不是名词块):
hello world LOL
,输出将是:
Hello World LOL
我之所以选择这种方式,是因为每个令牌都具有用于进一步处理的属性:)
答案 2 :(得分:0)
来自https://spacy.io/usage/linguistic-features#dependency-parse
您可以使用Noun chunks
。
名词块是“基础名词短语” –以名词为首的扁平短语。您可以将名词块看作一个名词,再加上描述该名词的单词,例如“豪华的绿草”或“世界上最大的科技基金”。要获取文档中的名词块,只需对Doc.noun_chunks
进行迭代。
In:
import spacy
nlp = spacy.load('en_core_web_sm')
doc = nlp(u"Autonomous cars shift insurance liability toward manufacturers")
for chunk in doc.noun_chunks:
print(chunk.text)
Out:
Autonomous cars
insurance liability
manufacturers