python - Spacy 获取特定单词的位置和标签

标签 python nlp spacy tagging part-of-speech

我遇到了一种情况,我必须从 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/

相关文章:

python - 在多类分类问题中将 class_weights 参数与 Keras 一起使用时出错

python - 在动词标签计数中使用defaultdict函数

python - Python OpenCV检测棋盘

python - 使用 Python selenium 选择一个元素(文本)

python - 用最少的内存连接 Numpy 数组

lucene - 使用免费工具进行实体提取/识别,同时提供 Lucene 索引

sql-server - 在位置索引中查找二元组

nlp - 如何让 spaCy 使用通用依赖项

python - 计算区别: speeding up an operation on all combinations of rows in a matrix