Python NLTK:如何使用简化的词性标签集来标记句子?

时间:2011-04-26 08:19:15

标签: python tagging nltk

Python NLTK book的第5章给出了在一个句子中标记单词的示例:

>>> text = nltk.word_tokenize("And now for something completely different")
>>> nltk.pos_tag(text)
[('And', 'CC'), ('now', 'RB'), ('for', 'IN'), ('something', 'NN'), ('completely', 'RB'), ('different', 'JJ')]

nltk.pos_tag调用默认的标记器,它使用一整套标记。本章后面将介绍simplified set of tags

如何使用这组简化的词性标签来标记句子?

我也正确理解了标记器,即我可以更改标记器使用的标记集,或者我应该将它返回的标记映射到简化集,还是应该从中创建新的标记器一个新的,标记简单的语料库?

3 个答案:

答案 0 :(得分:27)

更新,以防有人遇到同样的问题。 NLTK已经升级为通用" tagset,source here。标记文字后,请使用map_tag简化标记。

import nltk
from nltk.tag import pos_tag, map_tag

text = nltk.word_tokenize("And now for something completely different")
posTagged = pos_tag(text)
simplifiedTags = [(word, map_tag('en-ptb', 'universal', tag)) for word, tag in posTagged]
print(simplifiedTags)
# [('And', u'CONJ'), ('now', u'ADV'), ('for', u'ADP'), ('something', u'NOUN'), ('completely', u'ADV'), ('different', u'ADJ')]

答案 1 :(得分:20)

要简化默认标记器中的标记,您可以使用nltk.tag.simplify.simplify_wsj_tag,如下所示:

>>> import nltk
>>> from nltk.tag.simplify import simplify_wsj_tag
>>> tagged_sent = nltk.pos_tag(tokens)
>>> simplified = [(word, simplify_wsj_tag(tag)) for word, tag in tagged_sent]

答案 2 :(得分:3)

您可以在pos_tag方法中将tagset属性设置为'universal'。

In [39]: from nltk import word_tokenize, pos_tag
...: 
...: text = word_tokenize("Here is a simple way of doing this")
...: tags = pos_tag(text, tagset='universal')
...: print(tags)
...: 
[('Here', 'ADV'), ('is', 'VERB'), ('a', 'DET'), ('simple', 'ADJ'), ('way', 'NOUN'), ('of', 'ADP'), ('doing', 'VERB'), ('this', 'DET')]