1+ # -*- coding: utf-8 -*-
2+ """
3+ Created on Tue Mar 13 21:34:29 2018
4+
5+ @author: Administrator
6+ """
7+ import re ,jieba ,itchat
8+ import numpy as np
9+ from PIL import Image
10+ from snownlp import SnowNLP
11+ from wordcloud import WordCloud
12+ import matplotlib .pyplot as plt
13+ itchat .auto_login (hotReload = True )
14+ friends = itchat .get_friends (update = True )
15+ def analyseSignature (friends ):
16+ signatures = ''
17+ emotions = []
18+ pattern = re .compile ("1f\d.+" )
19+ for friend in friends :
20+ signature = friend ['Signature' ]
21+ if (signature != None ):
22+ signature = signature .strip ().replace ('span' , '' ).replace ('class' , '' ).replace ('emoji' , '' )
23+ signature = re .sub (r'1f(\d.+)' ,'' ,signature )
24+ if (len (signature )> 0 ):
25+ nlp = SnowNLP (signature )
26+ emotions .append (nlp .sentiments )
27+ signatures += ' ' .join (jieba .analyse .extract_tags (signature ,5 ))
28+ with open ('signatures.txt' ,'wt' ,encoding = 'utf-8' ) as file :
29+ file .write (signatures )
30+
31+ # Sinature WordCloud
32+ back_coloring = np .array (Image .open ('flower.jpg' ))
33+ wordcloud = WordCloud (
34+ font_path = 'simfang.ttf' ,
35+ background_color = "white" ,
36+ max_words = 1200 ,
37+ mask = back_coloring ,
38+ max_font_size = 75 ,
39+ random_state = 45 ,
40+ width = 960 ,
41+ height = 720 ,
42+ margin = 15
43+ )
44+
45+ wordcloud .generate (signatures )
46+ plt .imshow (wordcloud )
47+ plt .axis ("off" )
48+ plt .show ()
49+ wordcloud .to_file ('signatures.jpg' )
50+
51+ # Signature Emotional Judgment
52+ count_good = len (list (filter (lambda x :x > 0.66 ,emotions )))
53+ count_normal = len (list (filter (lambda x :x >= 0.33 and x <= 0.66 ,emotions )))
54+ count_bad = len (list (filter (lambda x :x < 0.33 ,emotions )))
55+ labels = [u'负面消极' ,u'中性' ,u'正面积极' ]
56+ values = (count_bad ,count_normal ,count_good )
57+ plt .rcParams ['font.sans-serif' ] = ['simHei' ]
58+ plt .rcParams ['axes.unicode_minus' ] = False
59+ plt .xlabel (u'情感判断' )
60+ plt .ylabel (u'频数' )
61+ plt .xticks (range (3 ),labels )
62+ plt .legend (loc = 'upper right' ,)
63+ plt .bar (range (3 ), values , color = 'rgb' )
64+ plt .title (u'%s的微信好友签名信息情感分析' % friends [0 ]['NickName' ])
65+ plt .show ()
66+ analyseSignature (friends )
0 commit comments