程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python special effects word cloud production

編輯:Python

What is?   The word cloud

The word cloud   In fact, it is a high frequency of network text 〝 key word 〞 Give visual prominence , formation 〝 Key word cloud 〞 or 〝 Keyword rendering 〞 To filter out a lot of text information

The word cloud   It is also a form of data visualization . Give a text , An image generated according to the occurrence frequency of keywords , People can understand the main idea of their article at a glance .

One 、 Special effects preview

Clouds of words

Two 、 Principle of procedure

  • From the text given , Do word segmentation , Then count the frequency of each word
  • From the background picture given , Read out picture information
  • Draw the text according to the frequency of occurrence , Higher frequency of occurrence , The larger the font setting

3、 ... and 、 Program source code

  • jieba modular : Used for word segmentation
  • PIL modular : Used for image processing
  • wordcloud modular : Used to generate word clouds
#!/usr/bin/env python
# encoding: utf-8
import jieba
import numpy as np
import PIL.Image as Image
from wordcloud import WordCloud
class wordCloud:
'''
This is a main Class, the file contains all documents.
One document contains paragraphs that have several sentences
It loads the original file and converts the original file to new content
Then the new content will be saved by this class
'''
def __init__(self):
self.bg_img = 'assets/picture.jpeg'
self.word_path = 'assets/word.txt'
def hello(self):
'''
This is a welcome speech
:return: self
'''
print('*' * 50)
print(' ' * 20 + ' Ci cloud production ')
print(' ' * 5 + 'Author: autofelix Date: 2022-01-15 13:14')
print('*' * 50)
return self
def run(self):
'''
The program entry
'''
with open(self.word_path, 'r') as f:
word = f.read()
cut_word = ' '.join(jieba.cut(word))
color_mask = np.array(Image.open(self.bg_img))
word_cloud = WordCloud(
# Set the font , If you don't specify it, there will be garbled code
font_path='/System/Library/Fonts/PingFang.ttc',
# Set background color
background_color='white',
# The shape of the word cloud
mask=color_mask,
# Maximum vocabulary allowed
max_words=120,
# Maximum font size
max_font_size=2000
).generate(cut_word)
word_cloud.to_file('word_cloud.jpg')
im = word_cloud.to_image()
im.show()
if __name__ == '__main__':
wordCloud().hello().run()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved