使用 nltk 的没有上下文的词性标记



有没有一种简单的方法可以使用nltk确定给定单词最可能的词性标签。或者如果不使用任何其他工具/数据集。

我尝试使用wordnet,但似乎sysnet不是按可能性排序的。

>>> wn.synsets('says')
[Synset('say.n.01'), Synset('state.v.01'), ...]
如果您想

尝试在没有上下文的情况下进行标记,您正在寻找某种 unigram 标记器,又名 looup tagger .Unigram 标记器仅根据给定单词的标记频率来标记单词。因此,它避免了上下文启发式,但是对于任何标记任务,您必须具有数据。对于 unigram,您需要带注释的数据来训练它。请参阅 nltk 教程 http://nltk.googlecode.com/svn/trunk/doc/book/ch05.html 中的lookup tagger

以下是在NLTK中训练/测试unigram标记器的另一种方法

>>> from nltk.corpus import brown
>>> from nltk import UnigramTagger as ut
>>> brown_sents = brown.tagged_sents()
# Split the data into train and test sets.
>>> train = int(len(brown_sents)*90/100) # use 90% for training
# Trains the tagger
>>> uni_tag = ut(brown_sents[:train]) # this will take some time, ~1-2 mins
# Tags a random sentence
>>> uni_tag.tag ("this is a foo bar sentence .".split())
[('this', 'DT'), ('is', 'BEZ'), ('a', 'AT'), ('foo', None), ('bar', 'NN'), ('sentence', 'NN'), ('.', '.')]
# Test the taggers accuracy.
>>> uni_tag.evaluate(brown_sents[train+1:]) # evaluate on 10%, will also take ~1-2 mins
0.8851469586629643

我不建议使用WordNet进行pos标记,因为单词中仍然没有条目。但是你可以看看在wordnet中使用引理频率,请参阅如何在NLTK中获取语法集的wordnet感知频率?。这些频率基于SemCor语料库(http://www.cse.unt.edu/~rada/downloads.html)

最新更新