|
1 | 1 | from wxpy import * |
| 2 | +import jieba |
| 3 | +import re |
| 4 | +from snownlp import SnowNLP |
| 5 | +import jieba.analyse |
| 6 | +import matplotlib.pyplot as plt |
2 | 7 |
|
3 | | - |
4 | | -# 初始化机器人,扫码登陆 |
5 | | -# bot = Bot() |
6 | | -bot = Bot(console_qr=True, cache_path=True) # 保留缓存自动登录 |
| 8 | +bot = Bot(console_qr=True, cache_path=True) # 登陆一次后利用缓存登陆 |
| 9 | +# bot =Bot() # 每次都需要登陆 |
7 | 10 | friends = bot.friends() |
8 | | -for friend in friends: |
9 | | - print(friend.name,friend.sex,friend.signature,friend.province,friend.city) |
10 | | - # name |
11 | | - # sex (0:不清楚 1:男 2:女) |
12 | | - # signature |
13 | | - # province |
14 | | - # city |
15 | 11 |
|
| 12 | +# 总体分析 |
| 13 | +def analyseTotal(friends): |
| 14 | + result = friends.stats_text() |
| 15 | + print(result) |
| 16 | + |
| 17 | +# 具体分析每个好友 |
| 18 | +def analyseConcrete(friends): |
| 19 | + text = friends.stats() |
| 20 | + print('sex:',text['sex']) |
| 21 | + print('province:',text['province']) |
| 22 | + print('city:',text['city']) |
| 23 | + for friend in friends[1:]: |
| 24 | + print(friend.name,friend.sex,friend.province,friend.city,friend.signature) |
| 25 | + |
| 26 | +# 性别分析,饼状图显示 |
| 27 | +def analyseSex(friends): |
| 28 | + text = friends.stats() |
| 29 | + male = text['sex'][1] |
| 30 | + female = text['sex'][2] |
| 31 | + unknown = text['sex'][0] |
| 32 | + labels = 'male','female','unknown' |
| 33 | + sizes = [male,female,unknown] |
| 34 | + explode = (0, 0.1, 0) # only "explode" the 2nd slice (i.e. 'Hogs') |
| 35 | + |
| 36 | + fig1, ax1 = plt.subplots() |
| 37 | + ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', |
| 38 | + shadow=True, startangle=90) |
| 39 | + ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle. |
| 40 | + plt.show() |
| 41 | + |
| 42 | +# 分析个性签名 |
| 43 | +def analyseSignature(friends): |
| 44 | + signatures = '' |
| 45 | + emotions = [] |
| 46 | + pattern = re.compile("lf\d.+") |
| 47 | + for friend in friends[1:]: |
| 48 | + signature = friend.signature |
| 49 | + if signature != None: |
| 50 | + signature = signature.strip().replace('span','').replace('class','').replace('emoji','') |
| 51 | + signature = re.sub(r'lf(\d.+)','',signature) |
| 52 | + # print(signature) |
| 53 | + if len(signature) > 0: |
| 54 | + nlp = SnowNLP(signature) |
| 55 | + emotions.append(nlp.sentiments) |
| 56 | + signatures += ''.join(jieba.analyse.extract_tags(signature,5)) |
| 57 | + # with open('signatures.txt', 'wt', encoding='utf-8') as file: |
| 58 | + # file.write(signatures) |
| 59 | + # Signature Emotional Judgment |
| 60 | + count_good = len(list(filter(lambda x: x > 0.66, emotions))) |
| 61 | + count_normal = len(list(filter(lambda x: x >= 0.33 and x <= 0.66, emotions))) |
| 62 | + count_bad = len(list(filter(lambda x: x < 0.33, emotions))) |
| 63 | + labels = [u'负面消极', u'中性', u'正面积极'] |
| 64 | + values = (count_bad, count_normal, count_good) |
| 65 | + plt.rcParams['font.sans-serif'] = ['simHei'] |
| 66 | + plt.rcParams['axes.unicode_minus'] = False |
| 67 | + plt.xlabel(u'情感判断') |
| 68 | + plt.ylabel(u'频数') |
| 69 | + plt.xticks(range(3), labels) |
| 70 | + plt.legend(loc='upper right', ) |
| 71 | + plt.bar(range(3), values, color='rgb') |
| 72 | + plt.title(u'%s的微信好友签名信息情感分析' % friends[0]) |
| 73 | + plt.show() |
| 74 | + |
| 75 | +def main(): |
| 76 | + analyseTotal(friends=friends) |
| 77 | + # analyseConcrete(friends=friends) |
| 78 | + analyseSex(friends=friends) |
| 79 | + analyseSignature(friends=friends) |
| 80 | + x = input('输入任意字符退出') |
16 | 81 |
|
| 82 | +main() |
17 | 83 |
|
18 | 84 |
|
0 commit comments