2017-12-10 15 views
0

nltkを使用して、度数に従って語句の度数分布を取得しようとしています。 「TypeError:unhashable type: 'list'」と表示されています。問題が何であるか理解していない。助けてください。nltk.FreqDist()関数で "TypeError:unhashable type: 'list'"を使用しています。

P.S:コードには多数のバグがありますので、気にしないでください。私はpythonにnoobとして、私は多くの多くのプログラムからのコードスニペットを使用してキーワード抽出プログラムを構築しようとしています。誰かが他のバグを見せてくれることを歓迎しています。

コード:

from __future__ import division 
import operator 
import nltk 
import string 

def isPunct(word): 
    return len(word) == 1 and word in string.punctuation 

def isNumeric(word): 
    try: 
    float(word) if '.' in word else int(word) 
    return True 
    except ValueError: 
    return False 

class KeyExt: 

    def __init__(self): 
    self.stopwords = set(nltk.corpus.stopwords.words()) 
    self.top_fraction = 1 

    def _generate_candidate_keywords(self, sentences): 
    phrase_list = [] 
    for sentence in sentences: 
     words = map(lambda x: "|" if x in self.stopwords else x, nltk.word_tokenize(sentence.lower())) 
     phrase = [] 
     for word in words: 
     if word == "|" or isPunct(word): 
      if len(phrase) > 0: 
      phrase_list.append(phrase) 
      phrase = [] 
     else: 
      phrase.append(word) 
    return phrase_list 

    def _calculate_word_scores(self, phrase_list): 
    word_freq = nltk.FreqDist() 
    word_degree = nltk.FreqDist() 
    for phrase in phrase_list: 
     degree = [x for x in phrase if not isNumeric(x)] 
     for word in phrase: 
     word_freq[word]=word_freq[word]+1 
     word_degree[word, degree]=word_degree[word, degree]+1 
    for word in word_freq.keys(): 
     word_degree[word] = word_degree[word] + word_freq[word] 
    word_scores = {} 
    for word in word_freq.keys(): 
     word_scores[word] = word_degree[word]/word_freq[word] 
    return word_scores 

    def _calculate_phrase_scores(self, phrase_list, word_scores): 
    phrase_scores = {} 
    for phrase in phrase_list: 
     phrase_score = 0 
     for word in phrase: 
     phrase_score += word_scores[word] 
     phrase_scores[" ".join(phrase)] = phrase_score 
    return phrase_scores 

    def extract(self, text, incl_scores=False): 
    sentences = nltk.sent_tokenize(text) 
    phrase_list = self._generate_candidate_keywords(sentences) 
    word_scores = self._calculate_word_scores(phrase_list) 
    phrase_scores = self._calculate_phrase_scores(phrase_list, word_scores) 
    sorted_phrase_scores = sorted(phrase_scores.items(), key=operator.itemgetter(1), reverse=True) 
    n_phrases = len(sorted_phrase_scores) 
    if incl_scores: 
     return sorted_phrase_scores[0:int(n_phrases/self.top_fraction)] 
    else: 
     return map(lambda x: x[0], 
     sorted_phrase_scores[0:int(n_phrases/self.top_fraction)]) 

def test(): 
    search=input("Enter Text: ") 
    ke = KeyExt() 
    keywords = ke.extract(search, incl_scores=True) 
    print (keywords) 

if __name__ == "__main__": 
    test() 

完全トレースバック:あなたはdictキーとしてlistを使用しようとしているとき

Traceback (most recent call last): File "C:\Users\SAURAV 
DAS\AppData\Local\Programs\Python\Python35\projects\nlpproj.py", line 
81, in <module> 
    test() File "C:\Users\SAURAV DAS\AppData\Local\Programs\Python\Python35\projects\nlpproj.py", line 
77, in test 
    keywords = ke.extract(search, incl_scores=True) File "C:\Users\SAURAV 
DAS\AppData\Local\Programs\Python\Python35\projects\nlpproj.py", line 
64, in extract 
    word_scores = self._calculate_word_scores(phrase_list) File "C:\Users\SAURAV 
DAS\AppData\Local\Programs\Python\Python35\projects\nlpproj.py", line 
44, in _calculate_word_scores 
    word_degree[word, degree]=word_degree[word, degree]+1 TypeError: unhashable type: 'list' 
+1

完全なトレースバックを表示する(行番号と原因コード行を含む) –

+0

[エラーをキャッチ](https://docs.python.org/3/tutorial/errors.html#handling-exceptions)とinspect /例外的なスーツの関連データを印刷して、何が起こっているのかを確認します。また、戦略的な場所にデータを印刷して、デバッグ作業に役立てることもできます。 – wwii

+0

okは1秒で実行します –

答えて

0

このエラーが発生します。 listはハッシュ可能ではなく、キーには使用できません(例:tupleを使用できます)。あなたはセンス原因度自体はlistであることはありませんword_degreeの指標として学位を使用しているこのライン

word_degree[word, degree]=word_degree[word, degree]+1 

で、あなたのケースで

+0

あなたは私に解決策を提案することができます –

+0

@IncognitoPossum私はちょうどエラーが何であるか答えました。 'word_degree'と' degree'のような、あなたの質問にさらに詳しい情報を加え、このコード行が何をすべきかを書き加えてください。 –

+0

'word_degree'は単語の度合いまたは優先度を示します。少なくともそれは私がしたいことです。しかし、遅れて返信して申し訳ありません。熱に苦しんでいた。 –

関連する問題