python中的wordnet引理与词性标注

2024-06-01 01:47:34 发布

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

我想在python中使用wordnet lemmatizer,并且我了解到默认的pos标记是NOUN,并且它不会为动词输出正确的引理,除非pos标记显式指定为verb。

我的问题是,为了准确地执行上面的引理,最好的射击是什么?

我用nltk.pos_tag做了pos标记,我在把树形银行pos标记集成到wordnet兼容的pos标记中迷失了方向。请帮忙

from nltk.stem.wordnet import WordNetLemmatizer
lmtzr = WordNetLemmatizer()
tagged = nltk.pos_tag(tokens)

我得到了NN,JJ,VB,RB中的输出标签。如何将这些更改为与wordnet兼容的标记?

另外,我是否需要使用标记的语料库来训练nltk.pos_tag(),或者我是否可以直接使用它对我的数据进行评估?


Tags: from标记postag树形动词银行方向
3条回答

首先,您可以直接使用nltk.pos_tag(),而无需进行培训。 函数将从文件中加载一个预先训练的标记器。你可以看到文件名 使用nltk.tag._POS_TAGGER

nltk.tag._POS_TAGGER
>>> 'taggers/maxent_treebank_pos_tagger/english.pickle' 

由于它是用Treebank语料库训练的,所以它也使用Treebank tag set

以下函数将树库标记映射到WordNet部分语音名称:

from nltk.corpus import wordnet

def get_wordnet_pos(treebank_tag):

    if treebank_tag.startswith('J'):
        return wordnet.ADJ
    elif treebank_tag.startswith('V'):
        return wordnet.VERB
    elif treebank_tag.startswith('N'):
        return wordnet.NOUN
    elif treebank_tag.startswith('R'):
        return wordnet.ADV
    else:
        return ''

然后,您可以将返回值与lemmatizer一起使用:

from nltk.stem.wordnet import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
lemmatizer.lemmatize('going', wordnet.VERB)
>>> 'go'

在将返回值传递给Lemmatizer之前,请检查它,因为空字符串将给出KeyError

您可以使用python默认dict创建一个映射,并利用这样一个事实:对于lemmatizer,默认标记是Noun。

from nltk.corpus import wordnet as wn
from nltk.stem.wordnet import WordNetLemmatizer
from nltk import word_tokenize, pos_tag
from collections import defaultdict

tag_map = defaultdict(lambda : wn.NOUN)
tag_map['J'] = wn.ADJ
tag_map['V'] = wn.VERB
tag_map['R'] = wn.ADV

text = "Another way of achieving this task"
tokens = word_tokenize(text)
lmtzr = WordNetLemmatizer()

for token, tag in pos_tag(tokens):
    lemma = lmtzr.lemmatize(token, tag_map[tag[0]])
    print(token, "=>", lemma)

在nltk.corpus.reader.wordnet的源代码中(http://www.nltk.org/_modules/nltk/corpus/reader/wordnet.html

#{ Part-of-speech constants
 ADJ, ADJ_SAT, ADV, NOUN, VERB = 'a', 's', 'r', 'n', 'v'
#}
POS_LIST = [NOUN, VERB, ADJ, ADV]

相关问题 更多 >