spaCy 中的 POS 标记单个单词

use*_*774 1 nlp pos-tagger spacy

spaCy 词性标注器通常用于整个句子。有没有一种方法可以有效地将一元词性标记应用于单个单词(或单个单词列表)?

像这样的东西:

words = ["apple", "eat", good"]
tags = get_tags(words) 
print(tags)
> ["NNP", "VB", "JJ"]
Run Code Online (Sandbox Code Playgroud)

谢谢。

aab*_*aab 5

英语一元词通常很难很好地标记,因此请考虑一下为什么要这样做以及您期望的输出是什么。apple(为什么你的例子中的 POS 是NNP? 的 POS 是什么can?)

spacy 并不是真正用于此类任务,但如果您想使用 spacy,一种有效的方法是:

import spacy
nlp = spacy.load('en')

# disable everything except the tagger
other_pipes = [pipe for pipe in nlp.pipe_names if pipe != "tagger"]
nlp.disable_pipes(*other_pipes)

# use nlp.pipe() instead of nlp() to process multiple texts more efficiently
for doc in nlp.pipe(words):
    if len(doc) > 0:
        print(doc[0].text, doc[0].tag_)
Run Code Online (Sandbox Code Playgroud)

请参阅文档nlp.pipe(): https: //spacy.io/api/language#pipe