创建词云报错“NLTK python error: “TypeError: 'dict_keys' object is not subscriptable””

正确如下

%python
from nltk.corpus import movie_reviews
from nltk.corpus import stopwords
from nltk import FreqDist
import string
sw = set(stopwords.words('english'))
punctuation = set(string.punctuation)
def isStopWord(word):
    return word in sw or word in punctuation
review_words = movie_reviews.words()
filtered = [w.lower() for w in review_words if not isStopWord(w.lower())]
words = FreqDist(filtered)
vob = list(words.keys())
N = int(.01*len(vob))
tags = vob[:N]
for tag in tags:
    print(tag,':',words[tag])
将 words.keys变成list类型,就可以了。

你可能感兴趣的:(python自然语言处理)