add emoji_count in analysis

This commit is contained in:
DzhiWang 2023-12-20 22:22:24 +08:00
parent 57645baf8a
commit 8fead399ba
3 changed files with 94 additions and 11 deletions

View File

@ -471,6 +471,32 @@ class Msg:
# result.sort(key=lambda x: x[5]) # result.sort(key=lambda x: x[5])
return result return result
def get_emoji_Img(self, username_, year_='all'):
sql = f"""
SELECT StrContent, count(StrContent)
from MSG
where StrTalker = ? and isSender = ?
{"and strftime('%Y', CreateTime, 'unixepoch', 'localtime') = ?" if year_ != "all" else ""} and Type=47
group by StrContent
order by Count(StrContent) desc
limit 3
"""
me_result = None
ta_result = None
if not self.open_flag:
return None, None
try:
lock.acquire(True)
self.cursor.execute(sql, [username_, 1, year_] if year_ != "all" else [username_, 1])
me_result = self.cursor.fetchall()
self.cursor.execute(sql, [username_, 0, year_] if year_ != "all" else [username_, 0])
ta_result = self.cursor.fetchall()
except sqlite3.DatabaseError:
logger.error(f'{traceback.format_exc()}\n数据库损坏请删除msg文件夹重试')
finally:
lock.release()
return me_result, ta_result
def get_first_time_of_message(self, username_): def get_first_time_of_message(self, username_):
if not self.open_flag: if not self.open_flag:
return None return None

View File

@ -1,5 +1,6 @@
from collections import Counter from collections import Counter
from datetime import datetime from datetime import datetime
import re
from PyQt5.QtCore import QFile, QTextStream, QIODevice from PyQt5.QtCore import QFile, QTextStream, QIODevice
@ -11,6 +12,7 @@ from app.DataBase import msg_db, MsgType
from pyecharts import options as opts from pyecharts import options as opts
from pyecharts.charts import WordCloud, Calendar, Bar, Line from pyecharts.charts import WordCloud, Calendar, Bar, Line
from app.resources import resource_rc from app.resources import resource_rc
from app.util.emoji import get_emoji
var = resource_rc.qt_resource_name var = resource_rc.qt_resource_name
charts_width = 800 charts_width = 800
@ -243,6 +245,50 @@ def hour_count(wxid, is_Annual_report=False, year='2023'):
} }
def emoji_count(wxid, is_Annual_report=False, year='2023'):
# 最常发的表情
txt_messages = msg_db.get_messages_by_type(wxid, MsgType.TEXT, is_Annual_report, year)
me_txt_messages = ''.join(map(lambda x: x[7] if x[4] == 1 else '', txt_messages))
ta_txt_messages = ''.join(map(lambda x: x[7] if x[4] == 0 else '', txt_messages))
pattern = re.compile(r"\[.+?\]")
MeEmoji = re.findall(pattern, me_txt_messages)
TaEmoji = re.findall(pattern, ta_txt_messages)
# 按照出现次数统计
MeEmoji_num = Counter(MeEmoji)
TaEmoji_num = Counter(TaEmoji)
# 打印统计结果
ta_total_emoji_num = len(TaEmoji)
me_total_emoji_num = len(MeEmoji)
ta_max_emoji = TaEmoji_num.most_common(10)
me_max_emoji = MeEmoji_num.most_common(10)
print("ta发的表情数", len(TaEmoji))
print("我发的表情数:", len(MeEmoji))
print("---"*10)
print("ta最常用的 10 个表情:\n", TaEmoji_num.most_common(10))
print("---"*10)
print("我最常用的 10 个表情:\n", MeEmoji_num.most_common(10))
# 最常发的表情包图片
MeImgList, TaImgList = msg_db.get_emoji_Img(wxid, year)
MeImgDict = {}
TaImgDict = {}
for xml, num in MeImgList:
MeImgDict[get_emoji(xml)] = num
for xml, num in TaImgList:
TaImgDict[get_emoji(xml)] = num
return {
'ta_total_emoji_num': ta_total_emoji_num,
'me_total_emoji_num': me_total_emoji_num,
'ta_max_emoji': ta_max_emoji,
'me_max_emoji': me_max_emoji,
'MeImgDict': MeImgDict, # 三张图片地址+数量字典格式path为key
'MeImgDict': MeImgDict
}
class Analysis: class Analysis:
pass pass
@ -250,13 +296,16 @@ class Analysis:
if __name__ == '__main__': if __name__ == '__main__':
msg_db.init_database(path='../DataBase/Msg/MSG.db') msg_db.init_database(path='../DataBase/Msg/MSG.db')
# w = wordcloud('wxid_0o18ef858vnu22') # w = wordcloud('wxid_0o18ef858vnu22')
w_data = wordcloud('wxid_27hqbq7vx5hf22', True, '2023') # w_data = wordcloud('wxid_27hqbq7vx5hf22', True, '2023')
# print(w_data) # # print(w_data)
# w['chart_data'].render("./data/聊天统计/wordcloud.html") # # w['chart_data'].render("./data/聊天统计/wordcloud.html")
c = calendar_chart('wxid_27hqbq7vx5hf22', False, '2023') # c = calendar_chart('wxid_27hqbq7vx5hf22', False, '2023')
c['chart_data'].render("./data/聊天统计/calendar.html") # c['chart_data'].render("./data/聊天统计/calendar.html")
# print('c:::', c) # # print('c:::', c)
m = month_count('wxid_27hqbq7vx5hf22', False, '2023') # m = month_count('wxid_27hqbq7vx5hf22', False, '2023')
m['chart_data'].render("./data/聊天统计/month_num.html") # m['chart_data'].render("./data/聊天统计/month_num.html")
h = hour_count('wxid_27hqbq7vx5hf22',is_Annual_report=False) # h = hour_count('wxid_27hqbq7vx5hf22',is_Annual_report=False)
h['chart_data'].render("./data/聊天统计/hour_count.html") # h['chart_data'].render("./data/聊天统计/hour_count.html")
h = emoji_count('wxid_27hqbq7vx5hf22',is_Annual_report=False)
# h['chart_data'].render("./data/聊天统计/hour_count.html")

View File

@ -89,11 +89,19 @@ def month():
@app.route('/hour_count') @app.route('/hour_count')
def hour(): def hour():
wxid = contact.wxid wxid = contact.wxid
world_cloud_data = analysis.month_count(wxid, is_Annual_report=True) world_cloud_data = analysis.hour_count(wxid, is_Annual_report=True)
with open('hour_count.html', 'w', encoding='utf-8') as f: with open('hour_count.html', 'w', encoding='utf-8') as f:
f.write(render_template('hour_count.html', **world_cloud_data)) f.write(render_template('hour_count.html', **world_cloud_data))
return render_template('hour_count.html', **world_cloud_data) return render_template('hour_count.html', **world_cloud_data)
@app.route('/emoji_count')
def emoji_count():
wxid = contact.wxid
world_cloud_data = analysis.emoji_count(wxid, is_Annual_report=True)
with open('emoji_count.html', 'w', encoding='utf-8') as f:
f.write(render_template('emoji_count.html', **world_cloud_data))
return render_template('emoji_count.html', **world_cloud_data)
@app.route('/wordcloud') @app.route('/wordcloud')
def cone(): def cone():