Implementation (recommended) of the word cloud generated by python and generation by python

Source: Internet
Author: User

Implementation (recommended) of the word cloud generated by python and generation by python

The end of the review is too busy to write the scrapy framework for some time. Today we will introduce how to use python to generate word cloud. Although there are many word cloud generation tools on the internet, but is it a sense of accomplishment to write in python.

Today, we are going to generate the word cloud of inspirational songs. Baidu Library has found 20 songs, such as stubborn and familiar to everyone.

The required python libraries include jieba (a Chinese Dictionary), wordcocould, matplotlib, PIL, and numpy.

The first thing we need to do is read the lyrics. I saved the lyrics to the inspirational song text in the file directory.

Read him now

# Encoding = gbklyric = ''f = open ('./Lizhi songs. txt', 'R') for I in f: lyric + = f. read ()

Add # encoding = gbk to prevent the following Operation errors SyntaxError: Non-UTF-8 code starting with '\ xc0'

Then, we use the jieba word segmentation to extract words with high word frequencies from songs.

import jieba.analyseresult=jieba.analyse.textrank(lyric,topK=50,withWeight=True)keywords = dict()for i in result:  keywords[i[0]]=i[1]print(keywords)

Expected result:

Then we can use libraries such as wrodcloud to generate word clouds.

First, find an image to generate the word cloud shape.

from PIL import Image,ImageSequenceimport numpy as npimport matplotlib.pyplot as pltfrom wordcloud import WordCloud,ImageColorGeneratorimage= Image.open('./tim.jpg')graph = np.array(image)wc = WordCloud(font_path='./fonts/simhei.ttf',background_color='White',max_words=50,mask=graph)wc.generate_from_frequencies(keywords)image_color = ImageColorGenerator(graph)plt.imshow(wc)plt.imshow(wc.recolor(color_func=image_color))plt.axis("off")plt.show()

Save the generated image

 wc.to_file('dream.png')

Complete code:

# Encoding = gbkimport jieba. analysefrom PIL import Image, ImageSequenceimport numpy as npimport matplotlib. pyplot as pltfrom wordcloud import WordCloud, ImageColorGeneratorlyric = ''f = open ('. /Lizhi songs. txt ', 'R') for I in f: lyric + = f. read () result = jieba. analyze. textrank (lyric, topK = 50, withWeight = True) keywords = dict () for I in result: keywords [I [0] = I [1] print (keywords) image = Image. open ('. /tim.jpg ') graph = np. array (image) wc = WordCloud (font_path = '. /fonts/simhei. ttf', background_color = 'white', max_words = 50, mask = graph) wc. generate_from_frequencies (keywords) image_color = ImageColorGenerator (graph) plt. imshow (wc) plt. imshow (wc. recolor (color_func = image_color) plt. axis ("off" )plt.show(%wc.to_file('dream.png ')

The implementation method (recommended) of the above python generation word cloud is all the content shared by Alibaba Cloud. I hope to give you a reference and support for the customer's house.

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.