Python-based Tagcloud

Source: Internet
Author: User

SETP1: Installing Jieba,pytagcloud

Pip Install Jieba

Apt-get Install Python-pygame

Pip Install Simplejson

Pip Install Pytagcloud

Step2: Download Chinese font files such assimhei.ttf

    • Locate the font file for the Pytagcloud package (/usr/local/lib/python2.7/dist-packages/pytagcloud/fonts)
    • Copy font files to Pytagcloud CP simhei.ttf/usr/local/lib/python2.7/dist-packages/pytagcloud/fonts
    • Edit Fonts.json vim Fonts.json (e.g.)
[  2     {  3"name":"Simhei",  4"TTF":"Simhei.ttf",  5"Web":"None"6     },  7     {  8"name":"Nobile",  9"TTF":"Nobile.ttf", 10"Web":"Http://fonts.googleapis.com/css?family=Nobile"11     }, 12     { 13"name":"Old Standard TT", 14"TTF":"Oldstandard-regular.ttf", 15"Web":"Http://fonts.googleapis.com/css?family=Old+Standard+TT"16},

Step3: Crawling text

STEP4: Generate Tagcloud

1#-*-coding:utf-8-*-2ImportJieba3ImportJieba.analyse4ImportPytagcloud5 fromPytagcloudImportCreate_tag_image,make_tags6 fromPytagcloud.lang.counterImportget_tag_counts7 Fp=open ('Sent.txt','R')  8 content =Fp.read ()9 words =jieba.cut (content)Ten top = Jieba.analyse.extract_tags (content,topk=100,withweight=True)Tagcloud={} 12 forIinchxrange, Len (top):Tagcloud[top[i][0]]=int (top[i][1]) 14PrintTagcloud15 fromoperatorImportItemgetterSWD = sorted (Tagcloud.iteritems (), Key=itemgetter (1), reverse=True)tags = make_tags (swd, minsize=20,maxsize=60) 18#Print TagsCreate_tag_image (tags,'Cloud_large.png', background= (0,0,0,255), size= (+), fontname='Simhei') 20ImportWebBrowserWebbrowser.open ('Cloud_large.png')

Python-based Tagcloud

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.