-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL6_Fb_semantic.py
34 lines (31 loc) · 1.18 KB
/
L6_Fb_semantic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import pymysql.cursors
import jieba
from snownlp import SnowNLP
import pprint
pp = pprint.PrettyPrinter(indent=4)
connection = pymysql.connect(host='localhost',
user='root',
password='password',
db='fb_social_data',
cursorclass=pymysql.cursors.DictCursor)
with connection.cursor() as cursor:
sql = ''' SELECT * FROM fb_social_data.replies where post_id = '{}' '''.format(15)
cursor.execute(sql)
replies = cursor.fetchall()
text_dict = {}
for replie in replies:
seg_list = jieba.cut(replie['comment'])
for seg in seg_list:
if seg in text_dict:
text_dict[seg] += 1
else:
text_dict[seg] = 1
sentiments_dict = {}
for replie in replies:
s = SnowNLP(replie['comment'])
sentiments_dict[replie['comment']] = s.sentiments
seg_data = sorted(text_dict.items(), key=lambda d:d[1], reverse=True)
pp.pprint(seg_data)
sentiments_data = sorted(sentiments_dict.items(), key=lambda d:d[1], reverse=True)
pp.pprint(sentiments_data)
connection.close()