对stemmer和pos tagger之间的优先级感到困惑

时间:2014-12-01 11:11:34

标签: python nltk stemming part-of-speech

所以我正在分析文本语料库,我使用词干分析器来表示所有标记化的单词。 但我还必须找到语料库中的所有名词,所以我再次做了nltk.pos_tag(stemmed_sentence) 但我的问题是我做得对吗?

A.] tokenize->stem->pos_tagging

OR

B.] tokenize->stem       #stemming and pos_tagging done seperately
    tokeinze->pos_tagging

我已经按照方法A,但我对它实现pos_tagging的正确方法感到困惑。

2 个答案:

答案 0 :(得分:8)

为什么不尝试一下?

以下是一个例子:

>>> from nltk.stem import PorterStemmer
>>> from nltk import word_tokenize, pos_tag
>>> sent = "This is a messed up sentence from the president's Orama and it's going to be sooo good, you're gonna laugh."

这是令牌化的结果。

>>> [word for word in word_tokenize(sent)]
['This', 'is', 'a', 'messed', 'up', 'sentence', 'from', 'the', 'president', "'s", 'Orama', 'and', 'it', "'s", 'going', 'to', 'be', 'sooo', 'good', ',', 'you', "'re", 'gon', 'na', 'laugh', '.']

这是令牌化的结果 - >干

>>> porter = PorterStemmer()
>>> [porter.stem(word) for word in word_tokenize(sent)]
[u'Thi', u'is', u'a', u'mess', u'up', u'sentenc', u'from', u'the', u'presid', u"'s", u'Orama', u'and', u'it', u"'s", u'go', u'to', u'be', u'sooo', u'good', u',', u'you', u"'re", u'gon', u'na', u'laugh', u'.']

这是令牌化的结果 - >干 - > POS标签

>>> pos_tag([porter.stem(word) for word in word_tokenize(sent)])
[(u'Thi', 'NNP'), (u'is', 'VBZ'), (u'a', 'DT'), (u'mess', 'NN'), (u'up', 'RP'), (u'sentenc', 'NN'), (u'from', 'IN'), (u'the', 'DT'), (u'presid', 'JJ'), (u"'s", 'POS'), (u'Orama', 'NNP'), (u'and', 'CC'), (u'it', 'PRP'), (u"'s", 'VBZ'), (u'go', 'RB'), (u'to', 'TO'), (u'be', 'VB'), (u'sooo', 'RB'), (u'good', 'JJ'), (u',', ','), (u'you', 'PRP'), (u"'re", 'VBP'), (u'gon', 'JJ'), (u'na', 'NN'), (u'laugh', 'IN'), (u'.', '.')]

这是令牌化的结果 - > POS标签

>>> pos_tag([word for word in word_tokenize(sent)])
[('This', 'DT'), ('is', 'VBZ'), ('a', 'DT'), ('messed', 'VBN'), ('up', 'RP'), ('sentence', 'NN'), ('from', 'IN'), ('the', 'DT'), ('president', 'NN'), ("'s", 'POS'), ('Orama', 'NNP'), ('and', 'CC'), ('it', 'PRP'), ("'s", 'VBZ'), ('going', 'VBG'), ('to', 'TO'), ('be', 'VB'), ('sooo', 'RB'), ('good', 'JJ'), (',', ','), ('you', 'PRP'), ("'re", 'VBP'), ('gon', 'JJ'), ('na', 'NN'), ('laugh', 'IN'), ('.', '.')]

那么正确的方法是什么?

答案 1 :(得分:2)

我认为你不想在POS标记之前阻止

请参阅此示例here

如何在NLTK中使用POS标记

在python解释器中导入NLTK之后,你应该在pos标记之前使用word_tokenize,它被称为pos_tag方法:

>>> import nltk
>>> text = nltk.word_tokenize(“Dive into NLTK: Part-of-speech tagging and POS Tagger”)
>>> text
[‘Dive’, ‘into’, ‘NLTK’, ‘:’, ‘Part-of-speech’, ‘tagging’, ‘and’, ‘POS’, ‘Tagger’]
>>> nltk.pos_tag(text)
[(‘Dive’, ‘JJ’), (‘into’, ‘IN’), (‘NLTK’, ‘NNP’), (‘:’, ‘:’), (‘Part-of-speech’, ‘JJ’), (‘tagging’, ‘NN’), (‘and’, ‘CC’), (‘POS’, ‘NNP’), (‘Tagger’, ‘NNP’)]