尝试进行总结优化

This commit is contained in:
liuwei
2026-01-06 16:09:10 +08:00
parent 491c0d16fb
commit 93fb180f14

View File

@@ -500,10 +500,15 @@ class MessageStorage:
"""优化的消息格式化方法,减少冗余
格式示例:
【08:00-09:00
【01-05
【08:30】
张三消息1
消息2
李四消息3
【01-06】
【09:00】
王五消息4
"""
if not messages:
return ""
@@ -511,9 +516,9 @@ class MessageStorage:
from collections import defaultdict
import xml.etree.ElementTree as ET
# 按时间分组每30分钟一组
# 按日期和时间分组
time_groups = defaultdict(lambda: defaultdict(list))
current_hour = None
dates_included = set() # 记录出现的日期
for msg in messages:
timestamp, sender, content, message_type = msg['timestamp'], msg['sender'], msg['content'], msg['message_type']
@@ -528,16 +533,20 @@ class MessageStorage:
except Exception as e:
logger.error(f"解析消息类型49出错: {e}")
# 解析时间并按30分钟分组
# 解析时间并按小时分组
try:
dt = datetime.strptime(str(timestamp), '%Y-%m-%d %H:%M:%S')
time_key = dt.strftime('%Y-%m-%d %H:%M') # 按小时分组
date_key = dt.strftime('%Y-%m-%d') # 完整日期
time_key = dt.strftime('%H:%M') # 只有时分
display_date = dt.strftime('%m-%d') # 显示的日期(月-日)
# 获取发送者名称
sender_name = all_contacts.get(sender, sender)
# 添加到分组
time_groups[time_key][sender_name].append(content)
# 添加到分组date_key -> time_key -> sender_name -> [contents]
time_groups[date_key][time_key][sender_name].append(content)
dates_included.add(display_date)
except Exception as e:
logger.warning(f"解析时间戳失败: {timestamp}, 错误: {e}")
continue
@@ -545,26 +554,33 @@ class MessageStorage:
# 构建结果字符串
result_lines = []
for time_key in sorted(time_groups.keys()):
# 提取时分部分
time_display = time_key.split(' ')[1] # 只显示 HH:MM
# 按日期排序
for date_key in sorted(time_groups.keys()):
# 添加日期标题(月-日格式)
display_date = datetime.strptime(date_key, '%Y-%m-%d').strftime('%m-%d')
result_lines.append(f"\n{display_date}")
# 获取该时间段的所有发言
senders = time_groups[time_key]
# 获取该日期的所有时间段
time_slots = time_groups[date_key]
# 添加时间标题
result_lines.append(f"\n{time_display}")
# 按时间排序
for time_key in sorted(time_slots.keys()):
# 添加时间标题
result_lines.append(f"{time_key}")
# 按发送者组织消息
for sender_name, contents in senders.items():
# 如果一个人有多条消息,缩进显示
for idx, content in enumerate(contents):
if idx == 0:
# 第一条消息显示发送者名
result_lines.append(f"{sender_name}{content}")
else:
# 后续消息缩进
result_lines.append(f" {content}")
# 获取该时间段的所有发言者
senders = time_slots[time_key]
# 按发送者组织消息
for sender_name, contents in senders.items():
# 如果一个人有多条消息,缩进显示
for idx, content in enumerate(contents):
if idx == 0:
# 第一条消息显示发送者名
result_lines.append(f"{sender_name}{content}")
else:
# 后续消息缩进
result_lines.append(f" {content}")
return "\n".join(result_lines)