2014-12-01 24 views
5

Vì vậy, tôi đã phân tích một văn bản gốc và tôi đã sử dụng phần tử gốc cho tất cả các từ được mã hóa. Nhưng tôi cũng phải tìm tất cả các danh từ trong kho để tôi lại làm nltk.pos_tag(stemmed_sentence) Nhưng câu hỏi của tôi là tôi đang làm đúng không?Bối rối về mức độ ưu tiên giữa trình tạo thẻ và trình gắn thẻ pos

A.] tokenize->stem->pos_tagging 

HOẶC

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

Ive theo phương pháp A, nhưng Im nhầm lẫn như một cách đúng đắn của nó để đạt được pos_tagging.

Trả lời

7

Tại sao bạn không dùng thử?

Dưới đây là một ví dụ:

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

Đây là kết quả của tokenizing.

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

Đây là kết quả của tokenize -> gốc

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

Đây là kết quả của tokenize -> gốc -> 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'.', '.')] 

Đây là kết quả của tokenize - > Thẻ 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'), ('.', '.')] 

Vậy cách nào đúng?

1

Tôi nghĩ rằng bạn không muốn để ngăn chặn trước khi POS tagging

Xem ví dụ này here:

Làm thế nào để sử dụng POS Tagging trong NLTK

Sau NLTK nhập khẩu thông dịch viên python, bạn nên sử dụng word_tokenize trước khi gắn thẻ pos, được gọi là phương thức 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, đừng làm hỏng niềm vui cho OP;) – alvas

+0

Tôi hy vọng một số niềm vui vẫn còn;) – bpgergo

+0

@bpgergo cảm ơn rất nhiều ;-) – rzach

Các vấn đề liên quan