2014-12-01 9 views
5

Così stavo analizzando un corpus di testo e ho usato lo stemmer per tutte le parole tokenized. Ma devo anche trovare tutti i nomi nel corpus, quindi ho fatto di nuovo un nltk.pos_tag(stemmed_sentence) Ma la mia domanda è che sto facendo bene?Confuso circa la priorità tra stemmer e pos tagger

A.] tokenize->stem->pos_tagging 

O

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

Ive seguita metodo A, ma Im confuso è il modo giusto per raggiungere pos_tagging.

risposta

7

Perché non lo provi?

Ecco un esempio:

>>> 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." 

Questo è il risultato della creazione di token.

>>> [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', '.'] 

Questo è il risultato di tokenize -> staminali

>>> 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'.'] 

Questo è il risultato di tokenize -> staminali -> POS tag

>>> 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'.', '.')] 

Questo è il risultato di tokenize - > Tag 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'), ('.', '.')] 

Allora, qual è il modo giusto?

1

penso che non si vuole arginare prima di POS codifica

Vedi questo esempio here:

Come utilizzare POS Tagging in NLTK

Dopo l'importazione NLTK in python interprete, dovresti usare word_tokenize prima del tagging pos, che si riferiva al metodo 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’)] 
+1

awww, non rovinare il divertimento per l'OP;) – alvas

+0

Spero che rimanga ancora un divertimento;) – bpgergo

+0

@bpgergo grazie mille ;-) – rzach

Problemi correlati