From 163b2ef2695ef09eb3d0c26872b7de42a85c3ee5 Mon Sep 17 00:00:00 2001 From: SiYuan <863909694@qq.com> Date: Fri, 9 Feb 2024 15:23:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=B9=B4=E5=BA=A6=E5=85=B3?= =?UTF-8?q?=E9=94=AE=E8=AF=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/analysis/analysis.py | 55 ++++++++++++++++++++++++- app/ui/chat/ai_chat.py | 8 +++- app/web_ui/templates/index.html | 72 ++++++++++++++++++++++++++++----- 3 files changed, 123 insertions(+), 12 deletions(-) diff --git a/app/analysis/analysis.py b/app/analysis/analysis.py index b4cad9e..f6d1a79 100644 --- a/app/analysis/analysis.py +++ b/app/analysis/analysis.py @@ -3,6 +3,8 @@ from collections import Counter import sys from datetime import datetime +import jieba + from app.DataBase import msg_db, MsgType from pyecharts import options as opts from pyecharts.charts import WordCloud, Calendar, Bar, Line, Pie @@ -64,6 +66,46 @@ def wordcloud_(wxid, time_range=None): } +def get_wordcloud(text): + total_msg_len = len(text) + # 使用jieba进行分词,并加入停用词 + words = jieba.cut(text) + # 统计词频 + word_count = Counter(words) + # 过滤停用词 + stopwords_file = './app/data/stopwords.txt' + with open(stopwords_file, "r", encoding="utf-8") as stopword_file: + stopwords1 = set(stopword_file.read().splitlines()) + # 构建 FFmpeg 可执行文件的路径 + stopwords = set() + stopwords_file = './app/resources/data/stopwords.txt' + if not os.path.exists(stopwords_file): + resource_dir = getattr(sys, '_MEIPASS', os.path.abspath(os.path.dirname(__file__))) + stopwords_file = os.path.join(resource_dir, 'app', 'resources', 'data', 'stopwords.txt') + with open(stopwords_file, "r", encoding="utf-8") as stopword_file: + stopwords = set(stopword_file.read().splitlines()) + stopwords = stopwords.union(stopwords1) + + filtered_word_count = {word: count for word, count in word_count.items() if len(word) > 1 and word not in stopwords} + # 转换为词云数据格式 + data = [(word, count) for word, count in filtered_word_count.items()] + # text_data = data + data.sort(key=lambda x: x[1], reverse=True) + + text_data = data[:100] if len(data) > 100 else data + # 创建词云图 + keyword, max_num = text_data[0] + w = ( + WordCloud() + .add(series_name="聊天文字", data_pair=text_data, word_size_range=[5, 40]) + ) + return { + 'chart_data_wordcloud': w.dump_options_with_quotes(), + 'keyword': keyword, + 'keyword_max_num': max_num, + } + + def wordcloud_christmas(wxid, year='2023'): import jieba txt_messages = msg_db.get_messages_by_type(wxid, MsgType.TEXT, year) @@ -375,6 +417,8 @@ def my_message_counter(time_range, my_name=''): types_count = {} send_num = 0 # 发送消息的数量 weekday_count = {} + str_content = '' + total_text_num = 0 for message in msg_data: type_ = message[2] is_sender = message[4] @@ -393,6 +437,10 @@ def my_message_counter(time_range, my_name=''): weekday_count[weekday] += 1 else: weekday_count[weekday] = 1 + if type_ == 1: + total_text_num += len(message[7]) + if is_sender == 1: + str_content += message[7] receive_num = len(msg_data) - send_num data = [[types_.get(key), value] for key, value in types_count.items() if key in types_] if not data: @@ -418,7 +466,7 @@ def my_message_counter(time_range, my_name=''): Pie() .add( "", - [['发送', send_num], ['接收',receive_num ]], + [['发送', send_num], ['接收', receive_num]], center=["40%", "50%"], ) .set_global_opts( @@ -428,9 +476,14 @@ def my_message_counter(time_range, my_name=''): .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}\n{d}%", position='inside')) # .render("./data/聊天统计/pie_scroll_legend.html") ) + w = get_wordcloud(str_content) return { 'chart_data_sender': p2.dump_options_with_quotes(), 'chart_data_types': p1.dump_options_with_quotes(), + 'chart_data_wordcloud': w.get('chart_data_wordcloud'), + 'keyword': w.get('keyword'), + 'keyword_max_num': w.get('keyword_max_num'), + 'total_text_num':total_text_num, } diff --git a/app/ui/chat/ai_chat.py b/app/ui/chat/ai_chat.py index d48d377..32c77ec 100644 --- a/app/ui/chat/ai_chat.py +++ b/app/ui/chat/ai_chat.py @@ -5,7 +5,7 @@ import traceback import requests from PyQt5.QtCore import QThread, pyqtSignal, QSize, Qt from PyQt5.QtGui import QPixmap -from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QHBoxLayout, QApplication, QTextBrowser +from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QHBoxLayout, QApplication, QTextBrowser, QMessageBox from app.log import logger from app.ui.Icon import Icon @@ -61,10 +61,14 @@ class AIChat(QWidget, Ui_Form): self.init_ui() self.show_chats() self.pushButton.clicked.connect(self.send_msg) + self.toolButton.clicked.connect(self.tool) def init_ui(self): self.textEdit.installEventFilter(self) + def tool(self): + QMessageBox.information(self, "温馨提示", "暂未接入聊天数据,您可进行基础的AI对话,后续更新敬请期待") + def chat(self, text): self.now_message.append(text) self.scrollArea.verticalScrollBar().setValue(self.scrollArea.verticalScrollBar().maximum()) @@ -143,7 +147,7 @@ class AIChatThread(QThread): } try: resp = requests.post(url, json=data, stream=True) - if resp.status_code==200: + if resp.status_code == 200: for line in resp.iter_lines(): if line: trunk = line.decode('utf-8') diff --git a/app/web_ui/templates/index.html b/app/web_ui/templates/index.html index fc248d5..f58d500 100644 --- a/app/web_ui/templates/index.html +++ b/app/web_ui/templates/index.html @@ -252,15 +252,48 @@ p { } .chart{ width: 800px; - height: 400px; + height: 300px; } - -@media screen and (max-width:600px){ +.chart-container{ + display: flex; +} +#word_cloud{ + width: 800px; + height: 800px; +} +@media screen and (max-width:480px){ .chart{ + width: 300px; + height: 250px; + } + .chart-container{ + display:flex; + flex-direction: column; + } + #word_cloud{ width: 400px; - height: 220px; + height: 400px; } } + +#keywords { + font-size: 2.5em; + color: #003366; + animation: slideIn 1.5s ease-out; + transform: skew(-15deg); +} + +@keyframes slideIn { + from { + opacity: 0; + transform: translateX(-50px) skew(-15deg); + } + to { + opacity: 1; + transform: translateX(0) skew(-15deg); + } +} +
@@ -270,18 +303,25 @@ p {统计、分析、回顾,记录聊天时光
+可掌控的才真正属于你
你一共给{{contact_num}}个联系人
发送了{{send_msg_num}}条消息
收到了{{receive_msg_num}}条消息
你一共给{{contact_num}}个联系人
发送了{{send_msg_num}}条消息
收到了{{receive_msg_num}}条消息
总计{{total_text_num}}字