好吧,现在我有一个代码可以使用 nltk 进行自定义标记。我使用 NLTK 的词性标注器作为 trigram 标注器的退避,在其中我使用自定义标签训练我自己的标记句子。这工作得很好,但我希望能够用 spacy 的 POS 标记器做同样的事情。有办法做到这一点吗?
这是我的代码:
import string
import nltk
from nltk.tokenize import RegexpTokenizer
from nltk.corpus import stopwords
import nltk.tag, nltk.data
tagger = nltk.TrigramTagger(train_sents, backoff=nltk.data.load(nltk.tag._POS_TAGGER))
def tagSentence(sentence):
# Method to tag sentence according to the tagger that is trained.
sentence = sentence.lower()
tokens = nltk.word_tokenize(sentence)
filtered_words = [w for w in tokens if not w in stopwords.words('english')]
" ".join(filtered_words)
return tagger.tag(filtered_words)
请您参考如下方法:
from spacy.en import English
oNlp = English()
oDoc = oNlp(sUnicodeInputText)
loTokens = [o for o in oDoc]
loTokens 这里包含了 spacy 提取的所有标记的列表。每个 token 都有您可以使用的属性。要获取 POS,请使用 .pos_ 属性。例如,要在一个元组中查看所有词形还原的标记名称和与其关联的 POS 标记:
print([ (o.lemma_, o.pos_) for o in loTokens ])
spacy documentation很棒。检查一下。
