2013-03-23 44 views
39

Tôi muốn sử dụng wordem lemmatizer trong python và tôi đã biết rằng thẻ pos mặc định là NOUN và nó không xuất ra bổ đề đúng cho động từ, trừ khi thẻ pos được chỉ định rõ ràng ĐỘNG TỪ.wordem lemmatization và pos gắn thẻ trong python

Câu hỏi của tôi là cách sắp xếp tốt nhất để thực hiện việc lemmatization ở trên một cách chính xác là gì?

Tôi đã gắn thẻ pos bằng cách sử dụng nltk.pos_tag và tôi bị mất tích hợp các thẻ pos ngân hàng cây vào thẻ pos tương thích với wordnet. Vui lòng giúp

from nltk.stem.wordnet import WordNetLemmatizer 
lmtzr = WordNetLemmatizer() 
tagged = nltk.pos_tag(tokens) 

Tôi nhận được thẻ đầu ra trong NN, JJ, VB, RB. Làm thế nào để thay đổi chúng thành các thẻ tương thích wordnet?

Ngoài ra, tôi có phải đào tạo nltk.pos_tag() với kho dữ liệu được gắn thẻ hoặc tôi có thể sử dụng trực tiếp trên dữ liệu của mình để đánh giá không?

Trả lời

56

Trước hết, bạn có thể sử dụng trực tiếp nltk.pos_tag() mà không cần đào tạo. Hàm sẽ tải một trình gắn thẻ giả mạo từ một tệp. Bạn có thể xem tên tập tin với nltk.tag._POS_TAGGER:

nltk.tag._POS_TAGGER 
>>> 'taggers/maxent_treebank_pos_tagger/english.pickle' 

Vì nó được huấn luyện với corpus Treebank, nó cũng sử dụng Treebank tag set.

Chức năng sau đây sẽ lập bản đồ các thẻ Treebank để WordNet một phần của tên bài phát biểu:

from nltk.corpus import wordnet 

def get_wordnet_pos(treebank_tag): 

    if treebank_tag.startswith('J'): 
     return wordnet.ADJ 
    elif treebank_tag.startswith('V'): 
     return wordnet.VERB 
    elif treebank_tag.startswith('N'): 
     return wordnet.NOUN 
    elif treebank_tag.startswith('R'): 
     return wordnet.ADV 
    else: 
     return '' 

Sau đó bạn có thể sử dụng giá trị trả về với lemmatizer:

from nltk.stem.wordnet import WordNetLemmatizer 
lemmatizer = WordNetLemmatizer() 
lemmatizer.lemmatize('going', wordnet.VERB) 
>>> 'go' 
+9

cũng nhớ tính từ vệ tinh =) 'ADJ_SAT = 's'' http://wordnet.princeton.edu/wordnet/man/wngloss.7WN.html – alvas

+1

thẻ pos cho'' it'' trong '" I 'yêu nó.' 'chuỗi là '' PRP''.Hàm trả về một chuỗi rỗng mà lemmatizer không chấp nhận và ném một 'KeyError'. Điều gì có thể được thực hiện trong trường hợp đó? –

+0

Có ai biết hiệu quả của việc này khi xử lý toàn bộ tài liệu không? – Ksofiac

2

@Suzana_K là w orking. Nhưng tôi có một số trường hợp kết quả trong KeyError như @ Clock Slave đề cập đến.

Chuyển đổi Treebank thẻ để thẻ Wordnet

from nltk.corpus import wordnet 

def get_wordnet_pos(treebank_tag): 

    if treebank_tag.startswith('J'): 
     return wordnet.ADJ 
    elif treebank_tag.startswith('V'): 
     return wordnet.VERB 
    elif treebank_tag.startswith('N'): 
     return wordnet.NOUN 
    elif treebank_tag.startswith('R'): 
     return wordnet.ADV 
    else: 
     return None # for easy if-statement 

Bây giờ, chúng tôi chỉ pos đầu vào chức năng lemmatize chỉ khi chúng tôi đã WordNet thẻ

from nltk.stem.wordnet import WordNetLemmatizer 
lemmatizer = WordNetLemmatizer() 
tagged = nltk.pos_tag(tokens) 
for word, tag in tagged: 
    wntag = get_wordnet_pos(tag) 
    if wntag is None:# not supply tag in case of None 
     lemma = lemmatizer.lemmatize(word) 
    else: 
     lemma = lemmatizer.lemmatize(word, pos=wntag) 
3

Bước chuyển đổi: Document-> Câu -> Tokens-> POS-> Lemmas

import nltk 
from nltk.stem import WordNetLemmatizer 
from nltk.corpus import wordnet 

#example text text = 'What can I say about this place. The staff of these restaurants is nice and the eggplant is not bad' 

class Splitter(object): 
    """ 
    split the document into sentences and tokenize each sentence 
    """ 
    def __init__(self): 
     self.splitter = nltk.data.load('tokenizers/punkt/english.pickle') 
     self.tokenizer = nltk.tokenize.TreebankWordTokenizer() 

    def split(self,text): 
     """ 
     out : ['What', 'can', 'I', 'say', 'about', 'this', 'place', '.'] 
     """ 
     # split into single sentence 
     sentences = self.splitter.tokenize(text) 
     # tokenization in each sentences 
     tokens = [self.tokenizer.tokenize(sent) for sent in sentences] 
     return tokens 


class LemmatizationWithPOSTagger(object): 
    def __init__(self): 
     pass 
    def get_wordnet_pos(self,treebank_tag): 
     """ 
     return WORDNET POS compliance to WORDENT lemmatization (a,n,r,v) 
     """ 
     if treebank_tag.startswith('J'): 
      return wordnet.ADJ 
     elif treebank_tag.startswith('V'): 
      return wordnet.VERB 
     elif treebank_tag.startswith('N'): 
      return wordnet.NOUN 
     elif treebank_tag.startswith('R'): 
      return wordnet.ADV 
     else: 
      # As default pos in lemmatization is Noun 
      return wordnet.NOUN 

    def pos_tag(self,tokens): 
     # find the pos tagginf for each tokens [('What', 'WP'), ('can', 'MD'), ('I', 'PRP') .... 
     pos_tokens = [nltk.pos_tag(token) for token in tokens] 

     # lemmatization using pos tagg 
     # convert into feature set of [('What', 'What', ['WP']), ('can', 'can', ['MD']), ... ie [original WORD, Lemmatized word, POS tag] 
     pos_tokens = [ [(word, lemmatizer.lemmatize(word,self.get_wordnet_pos(pos_tag)), [pos_tag]) for (word,pos_tag) in pos] for pos in pos_tokens] 
     return pos_tokens 

lemmatizer = WordNetLemmatizer() 
splitter = Splitter() 
lemmatization_using_pos_tagger = LemmatizationWithPOSTagger() 

#step 1 split document into sentence followed by tokenization 
tokens = splitter.split(text) 

#step 2 lemmatization using pos tagger 
lemma_pos_token = lemmatization_using_pos_tagger.pos_tag(tokens) 
print(lemma_pos_token) 
0

Bạn có thể làm điều này trong một dòng:

wnpos = lambda e: ('a' if e[0].lower() == 'j' else e[0].lower()) if e[0].lower() in ['n', 'r', 'v'] else 'n' 

Sau đó sử dụng wnpos(nltk_pos) để có được những POS để cung cấp cho .lemmatize(). Trong trường hợp của bạn, lmtzr.lemmatize(word=tagged[0][0], pos=wnpos(tagged[0][1])).

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