我遇到了一种情况,我必须从 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
如果我想获取单词“striker”的 pos_
和 tag_
,我是否需要再次将该句子赋予 nlp()
? ?
还有 doc[1].tag_ ,但我需要 doc['striker'].tag_ 之类的东西 ..
有没有可能?
最佳答案
您只需处理文本一次:
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])
如果您只想获取名词chunck中的特定单词,您可以通过将第二个打印语句更改为例如来进一步过滤它
print([(token.text, token.tag_, token.pos_) for token in nc if token.tag_ == 'NN'])
请注意,这可能会打印多个命中,具体取决于您的模型和输入句子。
关于python - Spacy 获取特定单词的位置和标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54784033/