stackoverflow_users_taginfo.py 文件源码

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

项目:stackoverflow_tag_cloud 作者: droyed 项目源码 文件源码
def tag_cloud(link=22656, lim_num_tags=200, image_dims=(400, 200),
              out_filepath="TagCloud.png"):
    """ Generate tag cloud and save it as an image.

    Parameters
    ----------
    link : same as used for the function taginfo.

    num_tags : same as used for the function taginfo.

    image_dims : tuple of two elements.
        Image dimensions of the tag cloud image to be saved.

    out_filepath : string
        Output image filepath.

    Output
    ------
    None
    """

    W, H = image_dims    # Wordcloud image size (width, height)
    font_path = "fonts/ShortStack-Regular.ttf"  # Font path
    info = taginfo(link=link, lim_num_tags=lim_num_tags)
    if info is None:
        print("Error : No webpage found!")
    else:
        if len(info) == 0:
            print("Error : No tags found!")
        else:         # Successfully extracted tag info
            WC = WordCloud(font_path=font_path, width=W, height=H,
                           max_words=len(info)).generate_from_frequencies(info)
            WC.to_image().save(out_filepath)
            print("Tag Cloud Saved as " + out_filepath)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号