簡體   English   中英

pandas和nltk:獲取最常用的詞組

[英]pandas and nltk: get most common phrases

對python來說還算是新手,我正在使用一列充滿文本的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)

看來您的BigramCollocationFinder類需要一個單詞列表,而不是列表列表。 嘗試這個:

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

您可能必須將列表列表隱藏到元組列表中。 希望這行得通

dat['text_clean2'] = [tuple(x) for x in dat['text_clean2']]
finder = BigramCollocationFinder.from_words(dat["text_clean2"])

CollocationFinder.from_words適用於單個文檔。 您要使用from_documents

finder = BigramCollocationFinder.from_documents(gg)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM