码迷,mamicode.com
首页 > 编程语言 > 详细

基于python的tagcloud

时间:2016-03-31 12:29:42      阅读:361      评论:0      收藏:0      [点我收藏+]

标签:

setp1: 安装jieba,pytagcloud

pip install jieba

apt-get install python-pygame

pip install simplejson

pip install pytagcloud

step2:下载中文字体文件比如simhei.ttf

  • 找到pytagcloud包的字体文件(/usr/local/lib/python2.7/dist-packages/pytagcloud/fonts)
  • 复制字体文件到pytagcloud中 cp simhei.ttf /usr/local/lib/python2.7/dist-packages/pytagcloud/fonts
  • 编辑fonts.json  vim fonts.json (如下图)
[
  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:爬取文本

step4:生成tagcloud

 1 # -*- coding:utf-8 -*-
  2 import jieba
  3 import jieba.analyse
  4 import pytagcloud
  5 from pytagcloud import create_tag_image,make_tags
  6 from pytagcloud.lang.counter import get_tag_counts
  7 fp=open(sent.txt,r)
  8 content = fp.read()
  9 words = jieba.cut(content)
 10 top = jieba.analyse.extract_tags(content,topK=100,withWeight=True)
 11 tagcloud={}
 12 for i in xrange(len(top)):
 13     tagcloud[top[i][0]]=int(top[i][1])
 14 print tagcloud
 15 from operator import itemgetter
 16 swd = sorted(tagcloud.iteritems(),key=itemgetter(1),reverse=True)
 17 tags = make_tags(swd, minsize=20,maxsize=60)
 18 #print tags
 19 create_tag_image(tags, cloud_large.png,background=(0,0,0,255),size=(900, 600),fontname=SimHei)
 20 import webbrowser
 21 webbrowser.open(cloud_large.png)

技术分享

 

基于python的tagcloud

标签:

原文地址:http://www.cnblogs.com/hee0624/p/5340396.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!