Files
ALL-teach_sys/frontend_大健康/convert_health_qa_data.py

171 lines
5.7 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
from datetime import datetime
# 读取大健康问答内容数据
with open('/Users/apple/Documents/cursor/教务系统/frontend_大健康/网页未导入数据/大健康产业/大健康问答内容.json', 'r', encoding='utf-8') as f:
health_qa_data = json.load(f)
# 导师头像映射
mentor_avatars = {
"李奇": "https://ddcz-1315997005.cos.ap-nanjing.myqcloud.com/static/img/teach_sys_teacher-avatar/recuW7dDIg60Tg.png",
"宋积极(配方师gigi)": "https://ddcz-1315997005.cos.ap-nanjing.myqcloud.com/static/img/teach_sys_teacher-avatar/recuW7dxJ5E4Al.png",
"吴兰": "https://ddcz-1315997005.cos.ap-nanjing.myqcloud.com/static/img/teach_sys_teacher-avatar/recuW7dxJ5t1Ii.jpeg",
# 其他导师可以添加更多映射
}
# 默认机器人头像
robot_avatar = "https://ddcz-1315997005.cos.ap-nanjing.myqcloud.com/static/img/teach_sys_icon/recuWmDuekBTlr.png"
# 转换为专家支持中心格式
conversations = []
for idx, item in enumerate(health_qa_data, 1):
messages = []
# 构建对话消息
# 流程1 - 用户问题
if item.get("问题_流程1"):
messages.append({
"type": "user",
"content": item["问题_流程1"],
"time": item["流程1_时间"]
})
# 流程2 - 助手回答
if item.get("回答_流程2"):
mentor_name = item.get("查询导师名称", "多多畅职机器人")
if item.get("问答类型") == "AI答疑":
mentor_name = "多多畅职机器人"
mentor_avatar = robot_avatar
else:
mentor_avatar = mentor_avatars.get(mentor_name, robot_avatar)
if mentor_name != "多多畅职机器人":
mentor_name += "老师"
messages.append({
"type": "assistant",
"content": item["回答_流程2"],
"mentor": mentor_name,
"time": item["流程2_时间"],
"mentorAvatar": mentor_avatar
})
# 流程3 - 用户问题
if item.get("问题_流程3"):
messages.append({
"type": "user",
"content": item["问题_流程3"],
"time": item["流程3_时间"]
})
# 流程4 - 助手回答
if item.get("回答_流程4"):
mentor_name = item.get("查询导师名称", "多多畅职机器人")
if item.get("问答类型") == "AI答疑":
mentor_name = "多多畅职机器人"
mentor_avatar = robot_avatar
else:
mentor_avatar = mentor_avatars.get(mentor_name, robot_avatar)
if mentor_name != "多多畅职机器人":
mentor_name += "老师"
messages.append({
"type": "assistant",
"content": item["回答_流程4"],
"mentor": mentor_name,
"time": item["流程4_时间"],
"mentorAvatar": mentor_avatar
})
# 流程5 - 用户问题
if item.get("问题_流程5"):
messages.append({
"type": "user",
"content": item["问题_流程5"],
"time": item["流程5_时间"]
})
# 流程6 - 助手回答
if item.get("回答_流程6"):
mentor_name = item.get("查询导师名称", "多多畅职机器人")
if item.get("问答类型") == "AI答疑":
mentor_name = "多多畅职机器人"
mentor_avatar = robot_avatar
else:
mentor_avatar = mentor_avatars.get(mentor_name, robot_avatar)
if mentor_name != "多多畅职机器人":
mentor_name += "老师"
messages.append({
"type": "assistant",
"content": item["回答_流程6"],
"mentor": mentor_name,
"time": item["流程6_时间"],
"mentorAvatar": mentor_avatar
})
# 获取对话日期(从第一条消息的时间提取年月)
if messages:
first_time = messages[0]["time"]
try:
# 解析时间格式 "2023/9/12 9:23"
date_obj = datetime.strptime(first_time, "%Y/%m/%d %H:%M")
conversation_date = f"{date_obj.year}{date_obj.month}"
except:
conversation_date = "2024年"
# 确定问答类型
qa_type = item.get("问答类型", "常规问题")
if qa_type == "导师问答":
qa_type = "专业知识"
elif qa_type == "AI答疑":
qa_type = "AI答疑"
else:
qa_type = "常规问题"
# 构建对话对象
if messages:
conversation = {
"id": idx,
"title": item.get("问题标题AI", f"问题{idx}"),
"status": "finish",
"date": conversation_date,
"type": qa_type,
"messages": messages
}
conversations.append(conversation)
# 按时间排序(从最早的第一条消息时间开始)
def get_first_message_time(conv):
if conv["messages"]:
time_str = conv["messages"][0]["time"]
try:
return datetime.strptime(time_str, "%Y/%m/%d %H:%M")
except:
return datetime.now()
return datetime.now()
# 按时间升序排序(早的在前)
conversations.sort(key=get_first_message_time)
# 构建最终的数据结构
expert_support_data = {
"conversations": conversations
}
# 生成JS文件内容
js_content = f"""// 从大健康问答内容.json转换的专家支持中心数据
const expertSupportData = {json.dumps(expert_support_data, ensure_ascii=False, indent=2)};
export default expertSupportData;"""
# 保存到文件
output_path = 'src/data/expertSupportData.js'
with open(output_path, 'w', encoding='utf-8') as f:
f.write(js_content)
print(f"✅ 成功转换 {len(conversations)} 条对话数据")
print(f"✅ 已保存到 {output_path}")