Promote the Red Revolutionary Spirit - Word Cloud Production


foreword

Hello everyone!
Red education is an indispensable content in the study and life of today's students. As a young pioneer, I also have the obligation to promote red education.
In my opinion, pictures are sometimes more powerful than text, but text plus pictures or pictures embedded with text may have better communication power, so I thought of word cloud.


Environmental preparation

Editor: pycharm64
Operating environment: python3.7
Required libraries:
wordcloudpip install wordcloud
jiebapip install jieba
numpypip install numpy
PILNo additional installation required


Word Cloud Maker

plain text word cloud

First of all, we start with the basic word cloud. First, we need to prepare a txt file for filling the text and a blank picture.
Run the following code to output a word cloud image:

import wordcloud
import numpy as np
from PIL import Image  # Image模块是在Python PIL图像处理常用的模块
import jieba
import matplotlib.pyplot as plt


def word_cloud():
    text = open(r'文字路径![请添加图片描述](https://img-blog.csdnimg.cn/c867a66f07c84fe8bcfeb4b0095a556b.png)
', "r").read()  # 读入txt文本数据,在字符串前面加上字符r或R之后表示原始字符串,字符串中的任意字符都不再进行转义,后一个r表示“只读”
    cut_text = jieba.cut(text)  # 结巴中文分词,生成字符串,默认精确模式,如果不通过分词,无法直接生成正确的中文词云
    result = " ".join(cut_text)  # 必须给个符号分隔开分词结果来形成字符串,否则不能绘制词云
    wc = wordcloud.WordCloud(
        # 设置字体,不指定就会出现乱码
        background_color='white',  # 设置背景色,默认为黑色
        width=500,  # 设置背景宽
        height=350,  # 设置背景高
        max_font_size=50,  # 最大字体
        min_font_size=10,  # 最小字体
        mode='RGBA'  # 当参数为“RGBA”并且background_color不为空时,背景为透明
    )
    wc.generate(result)  # 根据分词后的文本产生词云
    wc.to_file(r"图片路径")  # 保存绘制好的词云图
    plt.imshow(wc)  # 以图片的形式显示词云
    plt.axis("off")  # 关闭图像坐标系,即不显示坐标系
    plt.show()  # plt.imshow()函数负责对图像进行处理,并显示其格式,但是不能显示。其后必须有plt.show()才能显示

word_cloud()

Our txt file selects an English article:

Every single time you access a website, you leave tracks. Tracks that others can access. If you don't like the idea, find out what software can help you cover them.

Anti Tracks

Anti Tracks is a complete solution to protect your privacy and enhance your PC performance. With a simple click Anti Tracks securely erase your internet tracks, computer activities and programs history information stored in many hidden files on your computer.Anti Tracks support Internet Explorer, AOL, Netscape/Mozilla and Opera browsers. It also include more than 85 free plug-ins to extend erasing features to support popular programs such as ACDSee, Acrobat Reader, KaZaA, PowerDVD, WinZip, iMesh, Winamp and much more. Also you can easily schedule erasing tasks at specific time intervals or at Windows stat-up/ shutdown.To ensure maximum privacy protection Anti Tracks implements the US Department of Defense DOD 5220.22-M, Gutmann and NSA secure erasing methods, making any erased files unrecoverable even when using advanced recovery tools.

East-Tec Eraser

East-Tec Eraser goes beyond U.S. Department of Defense standards for the permanent erasure of digital information and easily removes every trace of sensitive data from your computer.

Completely destroy information stored without your knowledge or approval: Internet history, Web pages and pictures from sites visited on the Internet, unwanted cookies, chatroom conversations, deleted e-mail messages, temporary files, the Windows swap file, the Recycle Bin, previously deleted files, valuable corporate trade secrets, Business plans, personal files, photos or confidential letters, etc.East-Tec Eraser 2005 offers full support for popular browsers (Internet Explorer, Netscape Navigator, America Online, MSN Explorer, Opera), for Peer2Peer applications (Kazaa, Kazaa Lite, iMesh, Napster, Morpheus, Direct Connect, Limewire, Shareaza, etc.), and for other popular programs such as Windows Media Player, RealPlayer, Yahoo Messenger, ICQ, etc. Eraser has an intuitive interface and wizards that guide you through all the necessary steps needed to protect your privacy and sensitive information.Other features include support for custom privacy needs, user-defined erasure methods, command-line parameters, integration with Windows Explorer, and password protection.

Ghostsurf Platinum

GhostSurf Platinum ensures your safety online by providing an anonymous, encrypted Internet connection, and GhostSurf stops spyware, eliminates ads and erases your tracks. GhostSurf lets you customize your privacy level in real-time to suit your surfing needs. A variety of options enable you to block personal information, mask your IP address, route your data through anonymous hubs and even encrypt your Internet connection. GhostSurf's Privacy Control Center allows you to see and block every piece of data that your computer emits over the Internet, preventing even your Internet Service Provider (ISP) from creating a profile on you.

The resulting image looks something like this:
word cloud


word cloud with outline

A few more steps are needed here. We need to prepare a picture. Note that there needs to be a black closed outline and the background needs to be white. For example, the following picture:
sample image
When preparing our txt file, this time, we need a promotional slogan:

红色,中华,精神
爱国之心,国之命脉。
奋不顾身
传承红色圣火,共建绿色家园,
发扬革命传统,争取更大光荣,
畅游红色故土,饱览大好河山,
缅怀革命先烈,弘扬爱国精神,
畅游红色经典,发扬红色精神,
玩转五仙山,好运违你转,
红色体验经典,健康成长乐园,
染红快乐,畅享精彩,
红色乐园,绿色氧吧,金色童年,
红色乐园,金色年华,
感受红色文化,体验现时乐趣,
自古英雄出少年,少年乐园红色先,
红旗飘飘,我心飞扬,
红色乐园,赤子之行,
亲身重走长征路,身心历练不畏难,
领略红色文化,畅游激情海洋,
追寻红色记忆,体味幸福生活,

In the next code, we need to add a section to get the outline:

import wordcloud
import numpy as np
from PIL import Image  # Image模块是在Python PIL图像处理常用的模块
import jieba
import matplotlib.pyplot as plt


def word_cloud_shape():
    pic = Image.open("图片路径")  # 打开图片路径,形成轮廓
    shape = np.array(pic)  # 图像轮廓转换为数组
    wc = wordcloud.WordCloud(mask=shape, font_path="simkai.ttf", background_color="white",
                             max_font_size=100)  # mask为图片背景,font_path为字体,若不设置可能乱码

    text = open(r'文本路径', "r", encoding='gb2312').read()  # 对中文应该设置编码方式为gb2312
    cut_text = jieba.cut(text)
    result = " ".join(cut_text)
    wc.generate(result)
    wc.to_file("图片路径")


word_cloud_shape()

Generated picture:
renderings


epilogue

At the end of the article, I hope that those young pioneers and members of the Communist Youth League like me can actively participate in the promotion of red education, and I also wish that the great motherland of the motherland will become more and more prosperous and prosperous! ! !
That's all for today's sharing, 886!

Guess you like

Origin blog.csdn.net/m0_70457107/article/details/128811277