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 .
#!/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()