Wechat friends' personal tag words cloud -- wechat data analysis (4)

Sketch

Code

Background map used when building word cloud

The resulting effect is:

It can be found that although my wechat friends seem to be funny one by one, their personality tags seem to be slowly positive energy~

  • When I use the following, I use the data file that I have packed before
  • If you want to use it, you need to download the data first according to the connection I gave above
  • Or just call that function directly according to the following code
import itchat
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import re
import jieba
from wordcloud import WordCloud, ImageColorGenerator
import numpy as np
import PIL.Image as Image


def login_and_getData(columns=[]):
    itchat.auto_login(hotReload=True)
    # The first is myself
    friendList = itchat.get_friends(update=True)[1:]

    if len(columns) == 0:
        columns = friendList[0].keys()

    df = pd.DataFrame(columns=columns)
    val = [0] * len(friendList)

    for c in columns:
        for i in range(len(friendList)):
            val[i] = friendList[i][c]
        df[c] = val
    return df


def read(filename):
    try:
        return pd.read_excel(filename)
    except Exception:
        return pd.read_csv(filename)


if __name__ == '__main__':
    sns.set_palette('deep', desat=.6)
    # df = login_and_getData()
    # df.to_excel('wechat-1.xlsx')
    df = read('wechat-1.xlsx')
    plt.rcParams['font.sans-serif'] = ['SimHei']

    siglist = df['Signature'].dropna()

    # Some of the tags include expressions that need to be loved
    ser = pd.Series(map(lambda x: re.sub('<span(.*?)/span>', '', x), siglist))
    # Put all the labels together
    text = ''.join(ser)
    # participle
    word_list = jieba.cut(text, cut_all=True)
    # Put the separated thesaurus together
    word_space_split = ' '.join(word_list)
    # Read pictures
    coloring = np.array(Image.open("wechat.jpg"))
    # Use this picture as a boundary
    # Set font path to set font, and display Chinese
    my_wordcloud = WordCloud(background_color="white", max_words=2000,
                             mask=coloring, max_font_size=100, random_state=42, scale=2, font_path="C:/windows/Fonts/FZSTK.TTF").generate(word_space_split)

    # Get the color distribution of this picture
    image_colors = ImageColorGenerator(coloring)
    plt.imshow(my_wordcloud.recolor(color_func=image_colors))
    # Close abscissa
    plt.axis("off")
    # display picture
    plt.show()

Keywords: Lambda Windows

Added by teddirez on Fri, 20 Dec 2019 17:54:10 +0200