def posts_wordcloud():
"""
Generates wordcloud foeach post.
"""
posts = Post.objects.filter().exclude(content="")
for post in posts:
try:
image_file = join(settings.STATIC_ROOT, "wordcloud", "{0}.png".format(post.slug))
if not isfile(image_file):
text = words_wo_stopwords(text=post.content)
if len(text) > 100:
word_cloud = WordCloud(max_font_size=40, background_color="rgba(255, 255, 255, 0)", width=800, height=350, mode="RGBA").generate(text)
fig = plt.figure(frameon=False)
fig.patch.set_visible(False)
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')
ax.imshow(word_cloud, interpolation='bilinear')
plt.savefig(image_file)
plt.close()
post.wordcloud = "static/wordcloud/{0}.png".format(post.slug)
post.save()
except Exception as err:
print(err)
评论列表
文章目录