“
Reading the text takes about 3.2 minutes
What is it we can’t live without these days? The answer is mobile phones. According to statistics, the average Chinese spends three hours and three minutes a day using mobile phones. Which APP is opened most frequently on your phone every day? Today is the end of the month, I looked at my July 4G traffic ranking, ranking the first is wechat.
Now, new friends, add a wechat, watch friends, brush friends circle bar, shopping and payment, wechat scan. If you want to learn, join a technical networking group. Wechat has become part of our social life.
If you want to be the kind of person, reach out to those people.
A good circle can make you progress quickly. As your cognition improves, your posts on moments become more reflective. At this moment the possibility of your so-called “friend” in the following message said you again so I will screen your circle of friends! Why do you say that? Because their world only eat chicken, only king, only eat and drink, other they will choose to ignore or even hate. That’s when it’s time to change your wechat circle. It is often these friends who prevent you from making progress. I believe that your goals can be achieved faster with the encouragement of positive friends.
Collect your wechat friends’ signatures and make a word cloud using Python today. Your signature can tell you a lot about your attitude towards life. Today, let’s take a look at your wechat friends’ attitudes.
Project environment
Language: Python 3.6 tool: Pycharm
Guide package
Itchat: Used to collect wechat related information. Jieba: is a powerful participle database, perfect to support Chinese word segmentation. Matplotlib: Python’s 2D drawing library that generates publish-quality graphics in a variety of hard copy formats and cross-platform interactive environments. Wordcloud: a small wordcloud generator in Python that supports Python versions 2.7, 3.4, 3.5, 3.6, and 3.7.
import itchat
import re
import jieba
import matplotlib.pyplot as plt
from wordcloud import WordCloud
from wordcloud import ImageColorGenerator
import numpy as np
import PIL.Image as Image
Copy the code
Obtaining signature Information
After logging in wechat with itchat library, use get_friends method to obtain all the information of wechat friends, including nickname, remarks name, gender, region, personality signature, etc.
An article I shared earlier talked about making wechat friends’ gender statistics into a bar graph, just by stripping out the information needed from the total information. Personality signatures use the Signature field. Stripped out, there are a lot of characters that we don’t need, such as emoji tags, Spaces, garbage characters, etc. Get rid of the useless characters and generate the signature information we need.
signlist = []
itchat.auto_login() # Login to wechat
friends = itchat.get_friends(update=True)[0:]
for i in friends:
signature = i['Signature'].strip().replace('span'.' ').replace('class'.' ').replace('emoji'.' ').replace('\n'.' ').replace('\ "'.' ') # Remove unwanted characters
rep = re.compile("1f\d+\w*|[<>/=]")
signature = rep.sub(' ',signature)
signlist.append(signature)
text = "".join(signlist)
Copy the code
participles
We use jieba library to divide the signature information processed above into several Chinese words or words as much as possible.
# participle
cut = jieba.cut(text,cut_all=True)
word = ",".join(cut)
print(word)
Copy the code
For example, my signature is “strive to be the best I can be.” After it’s broken down: “Strive to be the best I can be.”
Draw the word cloud
Make a word cloud by processing all the Chinese word segmentation above, and choose a word cloud picture, you can choose a picture with a solid color background, so that the word cloud will be displayed as the outline in the picture. Define various properties of the word cloud, including background color, maximum number of words, canvas picture, maximum number of fonts, font path, canvas scale, and so on. The Font path is the computer with, mine is in C:\Windows\Font directory, there are common fonts, your word cloud is Chinese, just choose any kind of Chinese Font.
Then use the smatplotlib library to write and display the color and word segmentation content of the graph.
coloring = np.array(Image.open("F:\\photo.jpg")) # Picture of custom word cloud in computer
my_wordcloud = WordCloud(background_color="white", max_words=2000,mask=coloring, max_font_size=70, random_state=48,font_path='./font/msyh.ttf',scale=2).generate(word) # define word cloud background color, size, font size, computer font selection, random_state for each word returns a PIL color, font path, canvas scale
image_colors = ImageColorGenerator(coloring)
plt.imshow(my_wordcloud.recolor(color_func=image_colors)) # Drawing color
plt.imshow(my_wordcloud) # Drawing content
plt.axis("off")
plt.show() # display images
Copy the code
Finally, you can save the word cloud if you want. Save to the current directory of your project.
d = path.dirname(__file__) # project Current directory
my_wordcloud.to_file(path.join(d, 'cloud.png'))
Copy the code
The results
In this way, the word cloud of wechat friends’ signature is completed. It seems that my friends are quite positive energy. Hurry to try their own hands, sent to the circle of friends loaded B, public number reply “wechat word cloud” to obtain the source code.
Recommended reading:
Play wechat with Python, robot chat with you
30 lines of code to draw statistics of your wechat friends
Python Knowledge Circle
1. Share Python technology
2. Python crawler sharing
3. Sharing of materials and tools
Welcome to pay attention to us, grow together!