【发布时间】:2015-03-11 17:19:10
【问题描述】:
corpus = PlaintextCorpusReader("path",'.*',encoding="latin1")
docs = [corpus.words(f)for f in corpus.fileids()]
docs2 = [[w.lower()for w in doc]for doc in docs]
docs3 = [[w for w in doc if re.search('^[a-z]+$', w)]for doc in docs2]
from nltk.corpus import stopwords
stop_list = stopwords.words('english')
docs4 = [[w for w in doc if w not in stop_list]for doc in docs3]
我编写了以下代码,它读取文件的语料库。接下来我做了一些预处理步骤,删除了标点符号、停用词等。我现在想进行字数统计并找到文本中最常用的词。我使用下面的代码来做到这一点。 对于 docs4 中的单词:
if word in word_counter:
word_counter[word] += 1
else:
word_counter[word] = 1
popular_words = sorted(word_counter, key = word_counter.get, reverse = True)
但是我收到以下错误。 --
Traceback (most recent call last):
File "C:/Users/rohanhm.2014/PycharmProjects/untitled1/bp.py", line 18, in <module>
if word in word_counter:
TypeError: unhashable type: 'list'
有什么建议吗?
【问题讨论】:
-
您是否使用
list作为您在word_counter字典中的键?
标签: python regex nltk word-count gensim