如何在组合标记器中标记未知单词(标记为unk的标记)

wbgh16ku  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(227)

我正在使用nltk书籍第5章中描述的组合标记器
这是密码

t0 = nltk.DefaultTagger('NN')

t1 = nltk.UnigramTagger(train_sents, backoff=t0)

t2 = nltk.BigramTagger(train_sents, backoff=t1)

因为默认标记器将每个标记标记为 NN 前往t0的每个令牌都将被标记 NN 他们说这可以通过以下方法解决
我们标记未知单词的方法仍然使用回退到正则表达式标记器或默认标记器。它们无法利用上下文。因此,如果我们的标记者遇到单词blog,而不是在培训期间看到的,那么它将为它指定相同的标记,而不管这个单词是出现在blog的上下文中还是出现在blog的上下文中。我们怎样才能更好地处理这些未知的单词,或词汇表外的项目?
根据上下文标记未知单词的一种有用方法是将标记者的词汇量限制为最常见的n个单词,并使用3中所示的方法将其他每个单词替换为一个特殊单词unk。在培训过程中,单字标记者可能会了解到unk通常是一个名词。然而,n-gram标记器将检测其具有其他标记的上下文。例如,如果前面的单词是to(tagged to),那么unk可能会被标记为动词。
我编写了如3所示的方法,将每个单词Map到 UNK ```

alice = nltk.corpus.gutenberg.words('carroll-alice.txt')
vocab = nltk.FreqDist(alice)
v1000 = [word for (word, _) in vocab.most_common(1000)]
mapping = defaultdict(lambda: 'UNK')
for v in v1000:
... mapping[v] = v
...
alice2 = [mapping[v] for v in alice]
alice2[:100]
['UNK', 'Alice', "'", 's', 'UNK', 'in', 'UNK', 'by', 'UNK', 'UNK', 'UNK',
'UNK', 'CHAPTER', 'I', '.', 'UNK', 'the', 'Rabbit', '-', 'UNK', 'Alice',
'was', 'beginning', 'to', 'get', 'very', 'tired', 'of', 'sitting', 'by',
'her', 'sister', 'on', 'the', 'UNK', ',', 'and', 'of', 'having', 'nothing',
'to', 'do', ':', 'once', 'or', 'twice', 'she', 'had', 'UNK', 'into', 'the',
'book', 'her', 'sister', 'was', 'UNK', ',', 'but', 'it', 'had', 'no',
'pictures', 'or', 'UNK', 'in', 'it', ',', "'", 'and', 'what', 'is', 'the',
'use', 'of', 'a', 'book', ",'", 'thought', 'Alice', "'", 'without',
'pictures', 'or', 'conversation', "?'" ...]
len(set(alice2))
1001

我的问题是如何在组合标记器中实现此方法?我应该将新Map的字典放在哪里(在本例中) `mapping` )用组合标签?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题