Word Cloud Python

來源:互聯網
上載者:User

Word Cloud is popular in many web pages. 

Today, I find one simple way to generate word cloud by python. 

Here, we use pytagcloud package. It is can be find by Google.

We used :

python setup.py install

to install this package in python2.7

In the next, we may find this package also need pygame and jsonpickle the two packages. 

The same way to install these two packages.

Then we can generate the word cloud for one string by the follow:

tags = make_tags(get_tag_counts(contenct),maxsize=120)                imagename = 'H:/Project/NextBuildData/Imageresult/'+venue['Venue_id']+'.png'        print imagename        create_tag_image(tags, imagename, background=(0, 0, 0), fontname='Lobster')

Here, The contenct is the text or str data

get_tag_counts is used to count the number of word in the string data. 

Make_tag is used to generate the struct for the create word cloud

In this function, maxsize is the word size, also have one parameter minsize is the min size for each word. 

Finally, creat_tag_image is used to generate the word cloud by the "tags".

imagename is the output image name.

background is the background color.

fontname is the word font in the output image.

My code is :

'''Created on Mar 24, 2013@author: Yang'''import pytagcloudimport nltkimport nltk.bookimport sys, os, statfrom nltk import FreqDistfrom nltk.corpus import wordnet#from nltk.corpus import wordnet.synsetsfrom nltk.stem.lancaster import LancasterStemmerimport enchantfrom nltk.tag.simplify import simplify_wsj_tagimport pymongoimport sys, os, statfrom pymongo import Connectionfrom pytagcloud import create_tag_image, make_tagsfrom pytagcloud.lang.counter import get_tag_countsimport urllib2import jsonimport pymongoimport sys, os, statfrom pymongo import Connectionc = Connection('localhost', 27017)db = c.FourS2finf = db.FourInformationfpho = db.FourPhotosftip = db.FourTipsVenueList = db.Venuelistcount = 1for venue in VenueList.find():    try:        if count<=1991:            count = count+1            continue        contenct = ''        vneuetips = venue['Tips']        for tip in vneuetips:            contenct = contenct+' '+vneuetips[tip]['Tip']                    tokens = nltk.word_tokenize(contenct)         contenct = ''        tempword = []        for i in tokens:            temp = i.lower()            st = LancasterStemmer()            temp = st.stem(temp)            tempword.append(temp)                #print tempword    #    fdist = FreqDist(tempword)    #    v = fdist.keys()        d = enchant.Dict("en_US")    #    vtemp = v    #    print         for sample in tempword:            texttemp = nltk.word_tokenize(sample)            tags = nltk.pos_tag(texttemp)            s = [(word, simplify_wsj_tag(tag)) for word, tag in tags]            atrr = s[0][1]            tempnum = len(sample)            if tempnum>2:                if sample not in nltk.corpus.stopwords.words('english'):                    if sample.isalpha():                        if d.check(sample):                            #if atrr=='N' or atrr=='NP':                            contenct = contenct+' '+sample                                                    #YOUR_TEXT = "A tag cloud is a visual representation for text data, typically\    #used to depict keyword metadata on websites, or to visualize free form text."            tags = make_tags(get_tag_counts(contenct),maxsize=120)                imagename = 'H:/Project/NextBuildData/Imageresult/'+venue['Venue_id']+'.png'        print imagename        create_tag_image(tags, imagename, background=(0, 0, 0), fontname='Lobster')    except:        pass

I read text data from Mongodb. and also to remove the stop word and alpha. 

Friends can inference this code. 

The demo image is follow:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.