2016-07-05 29 views
4

私はpythonとnltkの両方が初めてです。私はコードをhttps://gist.github.com/alexbowe/879414から以下のコードに変換して、多くのドキュメント/テキストチャンクに対して実行させるようにしました。しかし、私は次のエラーを受けましたPythonを使用してNLTKから名詞句を抽出する

Traceback (most recent call last): 
File "E:/NLP/PythonProgrames/NPExtractor/AdvanceMain.py", line 16, in <module> 
    result = np_extractor.extract() 
File "E:\NLP\PythonProgrames\NPExtractor\NPExtractorAdvanced.py", line 67, in extract 
for term in terms: 
File "E:\NLP\PythonProgrames\NPExtractor\NPExtractorAdvanced.py", line 60, in get_terms 
for leaf in self.leaves(tree): 
TypeError: leaves() takes 1 positional argument but 2 were given 

この問題を解決するのに役立つものはありますか?何百万という製品レビューから名詞句を抽出する必要があります。私はJavaを使ってStandford NLPキットを使用しましたが、非常に遅いので、Pythonでnltkを使う方が良いと思いました。よりよい解決策がある場合は、お勧めします。

import nltk 
from nltk.corpus import stopwords 
stopwords = stopwords.words('english') 
grammar = r""" 
NBAR: 
    {<NN.*|JJ>*<NN.*>} # Nouns and Adjectives, terminated with Nouns 
NP: 
    {<NBAR>} 
    {<NBAR><IN><NBAR>} # Above, connected with in/of/etc... 
""" 
    lemmatizer = nltk.WordNetLemmatizer() 
    stemmer = nltk.stem.porter.PorterStemmer() 

class NounPhraseExtractor(object): 

    def __init__(self, sentence): 
     self.sentence = sentence 

    def execute(self): 
     # Taken from Su Nam Kim Paper... 
     chunker = nltk.RegexpParser(grammar) 
     #toks = nltk.regexp_tokenize(text, sentence_re) 
     # #postoks = nltk.tag.pos_tag(toks) 
     toks = nltk.word_tokenize(self.sentence) 
     postoks = nltk.tag.pos_tag(toks) 
     tree = chunker.parse(postoks) 
     return tree 

    def leaves(tree): 
     """Finds NP (nounphrase) leaf nodes of a chunk tree.""" 
     for subtree in tree.subtrees(filter=lambda t: t.label() == 'NP'): 
      yield subtree.leaves() 

    def normalise(word): 
     """Normalises words to lowercase and stems and lemmatizes it.""" 
     word = word.lower() 
     word = stemmer.stem_word(word) 
     word = lemmatizer.lemmatize(word) 
     return word 

    def acceptable_word(word): 
     """Checks conditions for acceptable word: length, stopword.""" 
     accepted = bool(2 <= len(word) <= 40 
        and word.lower() not in stopwords) 
     return accepted 

    def get_terms(self,tree): 
     for leaf in self.leaves(tree): 
      term = [self.normalise(w) for w, t in leaf if self.acceptable_word(w)] 
     yield term 

    def extract(self): 
     terms = self.get_terms(self.execute()) 
     matches = [] 
     for term in terms: 
      for word in term: 
       matches.append(word) 
     return matches 

答えて

4

あなたがいずれかを行う必要があります。

  • は、normalizeのそれぞれを飾るacceptable_word、および@staticmethodとleaves、または
  • は、これらのメソッドの最初のパラメータとしてselfパラメータを追加します。

あなたはleavesメソッドに暗黙の最初のパラメータとしてselfを渡しなるself.leavesを呼んでいる(しかし、あなたの方法は、単一のパラメータを取ります)。これらの静的メソッドを作成するか、selfパラメータを追加すると、この問題が解決されます。

消化しやすいかもしれexternal siteから、おそらく彼らのdocsにあなたがPythonの静的メソッドについて読むことができる

self.acceptable_wordにあなたの後に呼び出し、およびself.normalizeは同じ問題を持っています)、または。

+0

ありがとうございました。その作業 – Zubair

+0

コードは現在動作していますが、期待どおりの結果が得られません。私がそのままコードを実行すると、https://gist.github.com/alexbowe/879414から、私は変更されたものとは異なる結果をもたらします(上で示した)。私の変更によってロジックが変更されましたか? – Zubair

+0

@Zubair:あなたのロジックに 'sentence_re'がありません:' toks = nltk.word_tokenize(self.sentence'。あなたの 'self.sentence'は' text'です) – Gerrat

関連する問題