POS 在 spaCy 中标记单个单词
POS tagging a single word in spaCy
spaCy 词性标注器通常用于整个句子。有没有一种方法可以有效地将 unigram POS 标记应用于单个单词(或单个单词列表)?
像这样:
words = ["apple", "eat", good"]
tags = get_tags(words)
print(tags)
> ["NNP", "VB", "JJ"]
谢谢。
你可以这样做:
import spacy
nlp = spacy.load("en_core_web_sm")
word_list = ["apple", "eat", "good"]
for word in word_list:
doc = nlp(word)
print(doc[0].text, doc[0].pos_)
或者,您可以这样做
import spacy
nlp = spacy.load("en_core_web_sm")
doc = spacy.tokens.doc.Doc(nlp.vocab, words=word_list)
for name, proc in nlp.pipeline:
doc = proc(doc)
pos_tags = [x.pos_ for x in doc]
英语 unigrams 通常很难很好地标记,因此请考虑您为什么要这样做以及您期望输出的内容。 (为什么你的例子中apple
的POS是NNP
?can
的POS是什么?)
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_)
请参阅 nlp.pipe()
的文档:https://spacy.io/api/language#pipe
spaCy 词性标注器通常用于整个句子。有没有一种方法可以有效地将 unigram POS 标记应用于单个单词(或单个单词列表)?
像这样:
words = ["apple", "eat", good"]
tags = get_tags(words)
print(tags)
> ["NNP", "VB", "JJ"]
谢谢。
你可以这样做:
import spacy
nlp = spacy.load("en_core_web_sm")
word_list = ["apple", "eat", "good"]
for word in word_list:
doc = nlp(word)
print(doc[0].text, doc[0].pos_)
或者,您可以这样做
import spacy
nlp = spacy.load("en_core_web_sm")
doc = spacy.tokens.doc.Doc(nlp.vocab, words=word_list)
for name, proc in nlp.pipeline:
doc = proc(doc)
pos_tags = [x.pos_ for x in doc]
英语 unigrams 通常很难很好地标记,因此请考虑您为什么要这样做以及您期望输出的内容。 (为什么你的例子中apple
的POS是NNP
?can
的POS是什么?)
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_)
请参阅 nlp.pipe()
的文档:https://spacy.io/api/language#pipe