Spacy获取特定单词的pos和标签

问题描述 投票:1回答:1

我遇到了一种情况,我必须从spacy doc对象获取pos_&tag_。

例如,

text = "Australian striker John hits century"
doc = nlp(text)
for nc in doc.noun_chunks:
    print(nc) #Australian striker John
doc[1].tag_ # gives for striker

如果我想获得pos_tag_的单词'前锋'我是否需要再次给nlp()这句话?

另外doc [1] .tag_就在那里,但我需要像doc ['striker']这样的东西.tag_ ..

有可能吗?

python nlp spacy tagging pos
1个回答
1
投票

您只需要处理一次文本:

text = "Australian striker John hits century"
doc = nlp(text)
for nc in doc.noun_chunks:
    print(nc)  
    print([(token.text, token.tag_, token.pos_) for token in nc])

如果您只想在名词块中获取特定单词,则可以通过将第二个print语句更改为例如来进一步对此进行过滤。

print([(token.text, token.tag_, token.pos_) for token in nc if token.tag_ == 'NN'])

请注意,这可能会打印多个匹配,具体取决于您的型号和输入句子。

© www.soinside.com 2019 - 2024. All rights reserved.