我正在尝试使用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)
text()
函数。你的corpus[]
数组本身就可以用于执行FreqDist。 - Adam_G