剖析微信谈天记录(2)——剖析微信谈天记录

文章目录

  • 剖析微信谈天记录(2)——剖析微信谈天记录
    • 挑选指定谈天记录
    • 正式进行剖析
    • 各自发送信息条数
    • 计算谈天时刻频率
    • 获取词云
    • 总结

上一篇提到获取到微信的谈天记录,这一篇说说对单人微信谈天记录的剖析。

挑选指定谈天记录

假定咱们现已获取到一个名为message.csv的谈天记录文件,咱们使用python来挑选出指定人的微信谈天记录,存储到chat.csv文件中:

import pandas as pd
chat = pd.read_csv('../message.csv', sep=',')
myGirl = 'wxid_xxxxxxxxx' # 指定人的微信id
chat = chat[chat['talker'] == myGirl]
chat.to_csv('../chat.csv', sep=',')

对于上述代码中的微信id,可以依据谈天内容和对应的talker来获取,相信我们都会获取。

分析微信聊天记录(2)——分析单人的微信聊天记录

正式进行剖析

首要导入几个必须的包

import pandas as pd
import time
import seaborn as sns
import numpy as np
from matplotlib.font_manager import *#如果想在图上显现中文,需导入这个包
import matplotlib.pyplot as plt
from tqdm import tqdm
import re, string
np.set_printoptions(linewidth=800, suppress=False)

再导入chat.csv文件,并提取出几个有用的列信息。msg['content']是微信中的首要的谈天信息。msg['type']表示该谈天信息属于哪种类型,语音、文字、图片、表情包仍是分享链接等等。msg['createTime']表示该谈天信息发送的时刻,单位是毫秒。msg['isSend']表示该谈天信息是否是你发送的,如果是,则值为1,否则为0。还有其他更多的有用信息,后续再弥补。

chat = pd.read_csv('chat.csv', sep=',')
myGirl = 'wxid_xxxxxxx'
lens = len(chat)
# lens = 100
msg_content = [None,]*lens
msg_type = [None,]*lens
msg_isSend = [None,]*lens
msg_time = [None,]*lens
for i in tqdm(range(lens)):
    msg = chat[i:i + 1]
    msg_content[i] = msg['content'].values[0]
    msg_type[i] = msg['type'].values[0]
    msg_time[i] = msg['createTime'].values[0]
    msg_isSend[i] = int(msg['isSend'].values[0]) if msg['isSend'].values[0] in [0., 1.] else -1

在此先总结一下msg['type']的几种类型,目前只总结出这么多类型,后续持续弥补:

编号 类型
1 文本音讯,包含小表情
3 图片音讯,相机中的相片和装备有不同,从相册中发送的音讯中会保存一个 MMAsset,如同 PAAset
34 语音音讯
42 手刺音讯,大众号手刺和普通手刺
47 大表情
48 方位音讯
49 分享音讯
10000 系统音讯
419430449 微信转账
-1879048186 方位共享

各自发送信息条数

首要简单计算一下收发两边的信息数量。同时计算各自宣布的文字总数。

msg_data = np.array([[0, 0], [0, 0]])
for i in tqdm(range(lens)):
    if msg_isSend[i] not in [0, 1]:
        pass
    msg_data[msg_isSend[i]][0] += 1
    if msg_type[i] == 1:
        msg_con = msg_content[i]
        msg_data[msg_isSend[i]][1] += len(msg_con)
print(msg_data)
print(msg_data.sum(0))
labels = ['接收到', '发送出']
sizes = msg_data[:, 0]
myfont = FontProperties(fname=r'../kaiti.TTF', size=22)  # 标题字体款式
p = plt.pie(sizes, labels=labels, autopct='%1.1f%%', colors = ['magenta', 'lightskyblue'],
        shadow=True, startangle=90)
for front in p[1]:
    front.set_fontproperties(myfont)
plt.axis('equal') 
plt.show()

作用展现如下:

分析微信聊天记录(2)——分析单人的微信聊天记录

计算谈天时刻频率

首要定义几个时刻转化函数

# tm_year=2016, tm_mon=11, tm_mday=27, tm_hour=10, tm_min=26, tm_sec=5, tm_wday=6, tm_yday=332, tm_isdst=0
def to_hour(t):
    struct_time = time.localtime(t//1000)#将时刻戳转化为struct_time元组
#     hour = round((struct_time[3] + struct_time[4] / 60), 2)
    hour = struct_time[3]
    return hour
def to_day(t):
    struct_time = time.localtime(t//1000)#将时刻戳转化为struct_time元组
    day = struct_time[2]
    return day
def to_mon(t):
    struct_time = time.localtime(t//1000)#将时刻戳转化为struct_time元组
    mon = struct_time[1]
    return mon
def to_wday(t):
    struct_time = time.localtime(t//1000)#将时刻戳转化为struct_time元组
    wday = struct_time[6]
    return wday
def to_formatday(t):
    struct_time = time.localtime(t//1000)#将时刻戳转化为struct_time元组
    fday = time.strftime("%Y-%m-%d", struct_time) 
    return fday

再获取出每周的谈天频率:

hour_set = [to_hour(i) for i in msg_time]
day_set = [to_day(i) for i in msg_time]
mon_set = [to_mon(i) for i in msg_time]
wday_set = [to_wday(i) for i in msg_time]
# print(hour_set)
week_hour = np.zeros([2, 7, 24]).astype(np.int)
for s, w, h in tqdm(zip(msg_isSend, wday_set, hour_set)):
    if s not in [0, 1]:
        pass
    week_hour[s, w, h] += 1
print(week_hour[0].T)
print(week_hour[1].T)
print(week_hour[0].sum(0))
print(week_hour[1].sum(0))

运行成果如下,别离显现出收发两边在每周每日的发送信息量:

[[1379 1354 1863  847 1301 1032 1320]
 [ 492  627 1028  330  507  422  884]
 [ 149  427  289    4  270   70  353]
...........
 [1083  844  630  698  806  434  799]
 [1061  506  491  565  659  235  727]
 [1424 1616  923 1226  630  624 1083]]
[[1659 1865 1864  995 1342 1150 1547]
 [ 947  852 1087  226  727  393 1028]
 [ 893  330  862  243  709  210  358]
 ........
 [1261  597  511  705  678  312  807]
 [1678 1484  948 1450  915  747 1311]]
[9096 4290 1562  841  227  203  380 1048 3069 7001 8221 8934 7393 6787 6220 5626 6816 7287 7691 6741 6395 5294 4244 7526]
[10422  5260  1926   954   324   219   509  1453  3605  8588  9240  9492  8470  7216  6930  6130  7206  7860  8566  7456  7106  6162  4871  8533]

将上述数据保存至csv中,导入在线计算网站,这里引荐图说,适当不错的(非广告),进行一丢丢装备,就取得下面的展现图啦。

分析微信聊天记录(2)——分析单人的微信聊天记录

获取词云

用python当然可以做出不错的词云,但是我很懒,在线词云网站也挺多,就是要收费,我使用微词云来进行制造,由于数据量太大,我还充了69块钱!不过导出的作用仍是不错的。
首要,使用python将谈天记录悉数导出,我这里别离导出两人的谈天记录。然后将记录悉数复制到微词云中,再加上一丢丢的装备,就完结啦。

fday_set = [to_formatday(i) for i in msg_time]
txtlist = [[], []]
alllist = []
reflist = []
for s, c, t, f in tqdm(zip(msg_isSend, msg_content, msg_type, fday_set)):
    if s not in [0, 1]:
        pass
    if t == 1:
        txtlist[s].append(c)
        alllist.append(c)
#         if f == '2020-07-10': # 导出特定一天的记录
#             print(f)
#             reflist.append(c)
print(txtlist[0][:10])
print(txtlist[1][:10])
print(alllist[:10])
with open('recv.txt', 'w') as f:
    for t in txtlist[0]:
        f.write(f'{t}\n')
with open('send.txt', 'w') as f:
    for t in txtlist[1]:
        f.write(f'{t}\n')
with open('all.txt', 'w') as f:
    for t in alllist:
        f.write(f'{t}\n')

最终的制造作用:

分析微信聊天记录(2)——分析单人的微信聊天记录

总结

好了,今天就提到这了,祝我们学业有成,爱情事业双丰收。对了,别忘记点赞关注,后续还会更新。