2016-11-12 10 views
2

私は一連の文書を持っています。これらの文書にその単語のtfidfを数えることができるように、 tfidf-numberのベクトルで表されます)。nltkのリーマイタイザーのタグの提供方法(または生成する方法)

WordNetLemmatizer.lemmatize(word)を呼び出してからPorterStemmerを呼び出すだけで十分だと思ったが、すべて 'have'、 'has'、 'had'などはlemmatizerによって 'have'それは他の言葉にも行きます。その後、名詞、動詞、形容詞などの単語の種類を表すレマタイザータグのヒントを提供するはずです。

私はこれらのタグをどのように取得しますか? ?これを得るために私はそれらの文書をexcecuteするはずですか?

私はpython3.4を使用していますが、私は一度に1語の語をlemmatizing + stemmingしています。私はWordNetLemmatizerとEnglishStemmerをnltkから、またstem()をstemming.porter2から試しました。

答えて

1

[OK]を私はより多くのグーグルでこれらのタグを取得する方法を見つけました。 まず、ファイルがトークン化されることを確認するために、いくつかの前処理をしなければなりません(私の場合は、pdfからtxtへの変換後に残されたものを削除することでした)。

これらのファイルは、文にトークン化され、次に各文が単語配列にトークン化され、nltkタグャーによってタグ付けされる必要があります。その字句化を行うことができ、その上にステミングを追加することができます。

from nltk.tokenize import sent_tokenize, word_tokenize 
# use sent_tokenize to split text into sentences, and word_tokenize to 
# to split sentences into words 
from nltk.tag import pos_tag 
# use this to generate array of tuples (word, tag) 
# it can be then translated into wordnet tag as in 
# [this response][1]. 
from nltk.stem.wordnet import WordNetLemmatizer 
from stemming.porter2 import stem 

# code from response mentioned above 
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 ''  


with open(myInput, 'r') as f: 
    data = f.read() 
    sentences = sent_tokenize(data) 
    ignoreTypes = ['TO', 'CD', '.', 'LS', ''] # my choice 
    lmtzr = WordNetLemmatizer() 
    for sent in sentences: 
     words = word_tokenize(sentence) 
     tags = pos_tag(words) 
     for (word, type) in tags: 
      if type in ignoreTypes: 
       continue 
      tag = get_wordnet_pos(type) 
      if tag == '': 
       continue 
      lema = lmtzr.lemmatize(word, tag) 
      stemW = stem(lema) 

そして私は、私は、ファイルへの書き込み、およびドキュメントあたりTFIDFベクトルをカウントするためにこれらを使用することができます単語stemWを食い止めます。この時点で

関連する問題