tasks.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:QProb 作者: quant-trade 项目源码 文件源码
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)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号