Spacy获取特定单词的pos&tag

2024-10-05 10:38:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我遇到了一种情况,我必须从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_u在那里,但我需要像doc['Stroker']这样的东西。。在

有没有可能?在


Tags: 对象textposfordocnlpspacytag
2条回答

您只需处理文本一次:

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语句改为例如

^{pr2}$

请注意,这可能会打印多个点击,具体取决于您的模型和输入语句。在

您可以执行以下操作:

text = "Australian striker John hits century"
x1 = "striker"
x2 = re.compile(x1,re.IGNORECASE | re.VERBOSE)
loc_indexes = [m.start(0) for m in re.finditer(x2, text )]
tag = [i.tag_ for i in nlp(text) if i.idx in loc_indexes ]
print(x1,tag[0])

输出: striker NN

如果需要x1作为变量,也可以很容易地使其成为动态的。在

相关问题 更多 >

    热门问题