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