3

Python を使用して一連のドキュメントの度数分布を取得しようとしています。何らかの理由でコードが機能せず、次のエラーが発生しています。

Traceback (most recent call last):
  File "C:\Documents and Settings\aschein\Desktop\freqdist", line 32, in <module>
    fd = FreqDist(corpus_text)
  File "C:\Python26\lib\site-packages\nltk\probability.py", line 104, in __init__
    self.update(samples)
  File "C:\Python26\lib\site-packages\nltk\probability.py", line 472, in update
    self.inc(sample, count=count)
  File "C:\Python26\lib\site-packages\nltk\probability.py", line 120, in inc
    self[sample] = self.get(sample,0) + count
TypeError: unhashable type: 'list'

手伝ってくれますか?

これまでのコードは次のとおりです。

import os
import nltk
from nltk.probability import FreqDist


#The stop=words list
stopwords_doc = open("C:\\Documents and Settings\\aschein\\My Documents\\stopwords.txt").read()
stopwords_list = stopwords_doc.split()
stopwords = nltk.Text(stopwords_list)

corpus = []

#Directory of documents
directory = "C:\\Documents and Settings\\aschein\\My Documents\\comments"
listing = os.listdir(directory)

#Append all documents in directory into a single 'document' (list)
for doc in listing:
    doc_name = "C:\\Documents and Settings\\aschein\\My Documents\\comments\\" + doc
    input = open(doc_name).read() 
    input = input.split()
    corpus.append(input)

#Turn list into Text form for NLTK
corpus_text = nltk.Text(corpus)

#Remove stop-words
for w in corpus_text:
    if w in stopwords:
        corpus_text.remove(w)

fd = FreqDist(corpus_text)
4

2 に答える 2

2

少なくとも答えに貢献することを願っている2つの考え。

まず、 nltk.text.Text() メソッドのドキュメントには次のように記載されています(強調は私のものです):

一連の単純な (文字列) トークンのラッパー。 (対話型コンソールを介して) テキストの初期調査をサポートすることを目的としています。そのメソッドは、テキストのコンテキストに対してさまざまな分析を実行し (カウント、コンコーダンシング、コロケーションの発見など)、結果を表示します。これらの分析を利用するプログラムを作成する場合は、Text クラスをバイパスし、代わりに適切な分析関数またはクラスを直接使用する必要があります。

したがって、 Text() がこのデータを処理する方法であるかどうかはわかりません。リストを使用しても問題ないように思えます。

次に、ここで NLTK に実行を求めている計算について考えるように注意してください。頻度分布を決定する前にストップワードを削除すると、頻度が偏ることになります。ストップワードが、事後の分布の調査で単に無視されるのではなく、集計の前に削除される理由がわかりません。(この2番目の点は、回答の一部よりも優れたクエリ/コメントになると思いますが、割合が歪むことを指摘する価値があると感じました。)頻度分布を何に使用するかによって、これは場合によっては可能性がありますそれ自体は問題になりません。

于 2011-06-09T06:45:51.413 に答える
1

エラーは、リストをハッシュキーとして使用しようとしていることを示しています。タプルに変換できますか?

于 2011-06-08T20:56:00.757 に答える