[英]Spacy to extract specific noun phrase
Can I use spacy in python to find NP with specific neighbors?我可以在 python 中使用 spacy 找到特定邻居的 NP 吗? I want Noun phrases from my text that has verb before and after it.
我想要我的文本中前后有动词的名词短语。
Analyse the dependency parse tree, and see the POS of neighbouring tokens.分析依赖解析树,查看相邻token的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')]
By analysing the POS of adjacent tokens, you can get your desired noun phrases.通过分析相邻标记的 POS,您可以获得您想要的名词短语。
From https://spacy.io/usage/linguistic-features#dependency-parse来自https://spacy.io/usage/linguistic-features#dependency-parse
You can use Noun chunks
.您可以使用
Noun chunks
。 Noun chunks are "base noun phrases" – flat phrases that have a noun as their head.名词块是“基本名词短语”——以名词为中心的扁平短语。 You can think of noun chunks as a noun plus the words describing the noun – for example, "the lavish green grass" or "the world's largest tech fund".
您可以将名词块视为名词加上描述该名词的词 - 例如,“繁茂的绿草”或“世界上最大的科技基金”。 To get the noun chunks in a document, simply iterate over
Doc.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
If you want to re-tokenize using merge phrases, I prefer this (rather than noun chunks) :如果你想使用合并短语重新标记,我更喜欢这个(而不是名词块):
import spacy
nlp = spacy.load('en_core_web_sm')
nlp.add_pipe(nlp.create_pipe('merge_noun_chunks'))
doc = nlp(u"Autonomous cars shift insurance liability toward manufacturers")
for token in doc:
print(token.text)
and the output will be :输出将是:
Autonomous cars
shift
insurance liability
toward
manufacturers
I choose this way because each token has property for further process :)我选择这种方式是因为每个令牌都有进一步处理的属性:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.