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

我希望至少有两个想法有助于回答。

首先, nltk.text.Text() 方法的文档说明(强调我的):

围绕一系列简单(字符串)标记的包装器,旨在支持文本的初始探索(通过交互式控制台)。它的方法对文本的上下文进行各种分析(例如,计数、检索、搭配发现),并显示结果。如果您想编写一个利用这些分析的程序,那么您应该绕过 Text 类,而直接使用适当的分析函数或类。

所以我不确定 Text() 是你想要处理这些数据的方式。在我看来,使用列表就可以了。

其次,我会提醒您考虑您要求 NLTK 在这里执行的计算。在确定频率分布之前删除停用词意味着您的频率会出现偏差;我不明白为什么停用词在制表之前被删除,而不是在事后检查分布时被忽略。(我想第二点会比答案的一部分做出更好的查询/评论,但我觉得值得指出的是比例会出现偏差。)根据您打算使用频率分布的目的,这可能会或可能会本身不是问题。

于 2011-06-09T06:45:51.413 回答
1

该错误表明您尝试使用列表作为哈希键。你能把它转换成一个元组吗?

于 2011-06-08T20:56:00.757 回答