2017-07-25 10 views
0

pythonにはまったく新しいです。私はテキストがいっぱいの列を持つpandasデータフレームで作業しています。私はそのコラムを取って、一般的なフレーズ(3つまたは4つの単語)を見つけるためにnltkを使用しようとしています。pandasとnltk:最も一般的なフレーズを取得

dat["text_clean"] = 
    dat["Description"].str.replace('[^\w\s]','').str.lower() 

dat["text_clean2"] = dat["text_clean"].apply(word_tokenize) 

finder = BigramCollocationFinder.from_words(dat["text_clean2"]) 
finder 
# only bigrams that appear 3+ times 
finder.apply_freq_filter(3) 
# return the 10 n-grams with the highest PMI 
print finder.nbest(bigram_measures.pmi, 10) 

最初のコメントは正常に機能しているようです。しかし、BigramCollocationを使用しようとすると、次のエラーがスローされます。

n [437]: finder = BigramCollocationFinder.from_words(dat["text_clean2"]) 
finder 

Traceback (most recent call last): 

    File "<ipython-input-437-635c3b3afaf4>", line 1, in <module> 
    finder = BigramCollocationFinder.from_words(dat["text_clean2"]) 

    File "/Users/abrahammathew/anaconda/lib/python2.7/site-packages/nltk/collocations.py", line 168, in from_words 
    wfd[w1] += 1 

TypeError: unhashable type: 'list' 

これが何を意味するか、または回避策があります。

次のコマンドでも同じエラーが発生します。

gg = dat["text_clean2"].tolist()  
finder = BigramCollocationFinder.from_words(gg) 
finder = BigramCollocationFinder.from_words(dat["text_clean2"].values.reshape(-1,)) 

次のように動作しますが、一般的なフレーズはありません。

gg = dat["Description"].str.replace('[^\w\s]','').str.lower() 
finder = BigramCollocationFinder.from_words(gg) 
finder 
# only bigrams that appear 3+ times 
finder.apply_freq_filter(2) 
# return the 10 n-grams with the highest PMI 
print finder.nbest(bigram_measures.pmi, 10) 

答えて

1

あなたのBigramCollocationFinderクラスはリストのリストではなく、単語のリストを望んでいるようです。これを試してください:

finder = BigramCollocationFinder.from_words(dat["text_clean2"].values.reshape(-1,)) 
1

リストのリストをタプルのリストに変換する必要があります。この作品がほしいと思っています

dat['text_clean2'] = [tuple(x) for x in dat['text_clean2']] 
finder = BigramCollocationFinder.from_words(dat["text_clean2"]) 
関連する問題