BigramTagger是否需要最小数据量?

时间:2017-08-22 12:38:01

标签: python nlp nltk

我正在学习nltk库中的BigramTagger类。我训练一个句子的部分' tagger使用nltk附带的棕色语料库。

我注意到如果我在这个语料库上训练,然后在语料库的第一句中标记几个单词,那就很有效。

from nltk.corpus import brown
from nltk.tag import BigramTagger 
from nltk import word_tokenize

# Works completely fine:
brown_train = brown.tagged_sents(categories='news')
bigram_tagger = BigramTagger(brown_train)
print(bigram_tagger.tag(word_tokenize("that any irregularities took place")))

我们得到预期的输出:

  

[('',' CS'),('任何',' DTI'),('违规行为& #39;,' NNS'),('采取',' VBD'),('地点',' NN&# 39)]

但如果我只训练100个句子,那就失败了。

# Fails to work: 
brown_train = brown.tagged_sents(categories='news')[:100]
bigram_tagger = BigramTagger(brown_train)
print(bigram_tagger.tag(word_tokenize("that any irregularities took place")))

它无法标记这些单词,因此它为它们提供了无标记:

  

[('那',无),('任何',无),('违规行为',无),'采取&# 39;,无),('地点',无)]

班级是否需要最低语料库?或者是否有其他一些我忘记的参数会导致模型在第二种情况下失败?

我在这里查看了文档:http://www.nltk.org/api/nltk.tag.html#nltk.tag.sequential.BigramTagger,看起来有一个cutoff参数,但默认设置为0。

1 个答案:

答案 0 :(得分:1)

这是一个有趣的问题。它看起来就像您正在使用培训数据测试您的标记器一样,但是有一个至关重要的区别:因为您正在使用第一个句子的_a片段*,所以它的第一个单词出现在不同于在培训中使用的环境。对你的问题的简短回答是,它不是语料库大小,只是在培训中是否看到了相关的背景。 使用较短的训练数据,第一个单词从未出现在相同的(句子 - 初始)位置; 但是对于长数据集,它具有。

现在了解详细信息:ngram标记器根据当前单词和n-1以前的POS标记(" context")选择一个POS标记。在一个句子的开头,一个单词有空" context&#34 ;;要标记测试短语的第一个单词,标记器需要在训练数据的句子开头看到它。 测试用语中的第一个单词是"that",没有大小写。它真的发生在训练数据中吗?是的,它可以:

>>> for n, s in enumerate(brown.sents(categories="news")):
        if s[0] == 'that':
            print(n, " ".join(s))

3322 that its persistent use by ballet companies of the Soviet regime indicates that that old spirit is just as stultifying alive today as it ever was ; ;
3323 that its presentation in this country is part of a capitalist plot to boobify the American people ; ;
3324 that its choreography is undistinguished and its score a shapeless assemblage of self-plagiarisms .

就布朗语料库而言,这些是完整的句子。这可能是一个原因,但它确实无关紧要,现在就是这样。只要您的培训数据至少包含其中一个,您就可以在标记器的内部表格中查找上下文:

>>> bigram_tager._context_to_tag[(tuple(), "that")]
'CS'

在使用前100个句子训练的标记器上(或者使用前3000个句子训练的标记器,因为那里仍然没有小写"在正确的地方#34;)尝试同样的事情,你会得到KeyError。尚未看到上下文,标记器返回None作为第一个单词的标记。 一旦失败,标记第二个单词将失败(错误的上下文),等等。

实用建议:始终使用退避标记器(将使用单词的所有实例来选择标记),标记具有适当大小写的完整句子,或两者兼而有之。