#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 将智能制造问答内容转换为专家支持中心数据格式 """ import json from datetime import datetime # 导师头像映射 mentor_avatars = { "顾华": "https://ddcz-1315997005.cos.ap-nanjing.myqcloud.com/static/img/teach_sys_teacher-avatar/recuW7cCvScko4.png", "王军": "https://ddcz-1315997005.cos.ap-nanjing.myqcloud.com/static/img/teach_sys_teacher-avatar/recuW7c44HkjqO.png", "杨文琦": "https://ddcz-1315997005.cos.ap-nanjing.myqcloud.com/static/img/teach_sys_teacher-avatar/recuW7dxJ5Av9E.png", "李奇": "https://ddcz-1315997005.cos.ap-nanjing.myqcloud.com/static/img/teach_sys_teacher-avatar/recuW7dDIg60Tg.png" } # 读取原始数据 with open('网页未导入数据/智能制造产业/智能制造问答内容.json', 'r', encoding='utf-8') as f: data = json.load(f) conversations = [] for idx, item in enumerate(data, 1): # 解析日期获取月份 first_time = item.get('流程1_时间', '') if first_time: try: date_obj = datetime.strptime(first_time.split(' ')[0], '%Y/%m/%d') date_str = f"{date_obj.year}年{date_obj.month}月" except: date_str = "2024年10月" # 默认日期 else: date_str = "2024年10月" # 构建对话消息 messages = [] # 流程1和2 if item.get('问题_流程1'): messages.append({ "type": "user", "content": item['问题_流程1'], "time": item.get('流程1_时间', '') }) if item.get('回答_流程2'): mentor_name = item.get('查询导师名称', '') if item['问答类型'] == '智能客服': mentor = "多多畅职机器人" avatar = "https://ddcz-1315997005.cos.ap-nanjing.myqcloud.com/static/img/teach_sys_icon/recuWmDuekBTlr.png" else: mentor = f"{mentor_name}老师" if mentor_name else "顾华老师" avatar = mentor_avatars.get(mentor_name, mentor_avatars['顾华']) messages.append({ "type": "assistant", "content": item['回答_流程2'], "mentor": mentor, "time": item.get('流程2_时间', ''), "mentorAvatar": avatar }) # 流程3和4 if item.get('问题_流程3'): messages.append({ "type": "user", "content": item['问题_流程3'], "time": item.get('流程3_时间', '') }) if item.get('回答_流程4'): messages.append({ "type": "assistant", "content": item['回答_流程4'], "mentor": mentor, "time": item.get('流程4_时间', ''), "mentorAvatar": avatar }) # 流程5和6 if item.get('问题_流程5'): messages.append({ "type": "user", "content": item['问题_流程5'], "time": item.get('流程5_时间', '') }) if item.get('回答_流程6'): messages.append({ "type": "assistant", "content": item['回答_流程6'], "mentor": mentor, "time": item.get('流程6_时间', ''), "mentorAvatar": avatar }) # 创建对话对象 conversation = { "id": idx, "title": item['问题标题'], "status": "finish", "date": date_str, "type": item['问题类型'], "messages": messages } conversations.append(conversation) # 生成最终的JavaScript文件 js_content = """// 从智能制造问答内容.json转换的专家支持中心数据 const expertSupportData = { "conversations": %s }; export default expertSupportData; """ % json.dumps(conversations, ensure_ascii=False, indent=2) # 写入文件 with open('src/data/expertSupportData.js', 'w', encoding='utf-8') as f: f.write(js_content) print(f"成功转换 {len(conversations)} 个对话")