我有以下代码可以在movie_review语料库中打印15种最常见的事件。
import nltk
import random
from nltk.corpus import movie_reviews
documents =[]
for category in movie_reviews.categories():
for fileid in movie_reviews.fileids(category):
documents.append((list(movie_reviews.words(fileid)), category))
random.shuffle(documents)
all_words =[]
for w in movie_reviews.words():
all_words.append(w.lower())
all_words =nltk.FreqDist(all_words)
print(all_words.most_common(15))
我得到以下输出:
[(u',', 77717), (u'the', 76529), (u'.', 65876), (u'a', 38106), (u'and', 35576), (u'of', 34123), (u'to', 31937), (u"'", 30585), (u'is', 25195), (u'in', 21822), (u's', 18513), (u'"', 17612), (u'it', 16107), (u'that', 15924), (u'-', 15595)]
为什么字母'u'会出现在标签中?我该如何解决这个问题?
答案 0 :(得分:1)
您会看到引号,逗号和(在Python 2.7上)u
前缀,因为您要将对列表传递给print
。打印单个字符串按预期工作。例如:
for word, cnt in all_words.most_common(15):
print word, cnt
答案 1 :(得分:0)
它只是unicode strings in Python 2.7,没有特定的NLTK。