2013-03-23 27 views
39

私はPythonでwordnet lemmatizerを使いたいと思いました。デフォルトのposタグがNOUNで、posタグが明示的に指定されていない限り動詞に正しい補助語を出力しないことを学びました。動詞。Pythonでwordnet lemmatizationとposタグ付け

私の質問は、上記のlemmatizationを正確に実行するためのベストショットですか?

私はnltk.pos_tagを使用してposタグ付けを行いました。私は、wordnet互換posタグにツリーバンクposタグを組み込むことに迷っています。助けてください

NN、JJ、VB、RBで出力タグを取得します。これらをワードネット対応のタグに変更するにはどうすればよいですか?

また、nltk.pos_tag()にタグ付きコーパスを訓練する必要がありますか、評価するためにデータに直接使用することはできますか?

答えて

56

まず、nltk.pos_tag()をトレーニングなしで直接使用できます。 この関数は、ファイルから事前に割り当てられたタグを読み込みます。あなたはnltk.tag._POS_TAGGERとファイル名 を見ることができます:

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

それはツリーバンクコーパスで訓練を受けたとして、それはまたTreebank tag setを使用しています。

次の関数は、音声名の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 '' 

その後、lemmatizerと戻り値を使用することができますのソースコードのように

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

また、衛星形容詞も覚えておいてください。=== 'ADJ_SAT = 's'' http://wordnet.princeton.edu/wordnet/man/wngloss.7WN.html – alvas

+1

''I'のposタグそれを愛しています。」という文字列は「PRP」です。この関数は、lemmatizerが受け入れず、 'KeyError'をスローする空の文字列を返します。その場合、何ができるでしょうか? –

+0

文書全体を処理するときの効率性を知っている人はいますか? – Ksofiac

2
#{ Part-of-speech constants 
ADJ, ADJ_SAT, ADV, NOUN, VERB = 'a', 's', 'r', 'n', 'v' 
#} 
POS_LIST = [NOUN, VERB, ADJ, ADV] 

@Suzana_KはWでしたorking。しかし、私はKeyErrorに@ Clock Slaveの言及としていくつかのケースの結果があります。 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 

変換ツリーバンクタグに、我々は変換するタグ

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

ステップのWordNetした場合にのみlemmatize機能に我々だけ入力POS:Document->文を→トークン→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

あなたは1行で行うことができます。

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

その後(.lemmatizeに与えるPOSを取得するためにwnpos(nltk_pos)を使用します)。あなたの場合、lmtzr.lemmatize(word=tagged[0][0], pos=wnpos(tagged[0][1]))

関連する問題