#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
How解构结果可视化脚本 V2
改进版:
- 使用标签页展示多个帖子
- 参考 visualize_inspiration_points.py 的帖子详情展示
- 分层可折叠的匹配结果
"""
import json
from pathlib import Path
from typing import Dict, List
import sys
import html as html_module
# 添加项目根目录到路径
project_root = Path(__file__).parent.parent.parent
sys.path.insert(0, str(project_root))
def get_relation_color(relation: str) -> str:
"""根据关系类型返回对应的颜色"""
color_map = {
"same": "#10b981", # 绿色 - 同义
"contains": "#3b82f6", # 蓝色 - 包含
"contained_by": "#8b5cf6", # 紫色 - 被包含
"coordinate": "#f59e0b", # 橙色 - 同级
"overlap": "#ec4899", # 粉色 - 部分重叠
"related": "#6366f1", # 靛蓝 - 相关
"unrelated": "#9ca3af" # 灰色 - 无关
}
return color_map.get(relation, "#9ca3af")
def get_relation_label(relation: str) -> str:
"""返回关系类型的中文标签"""
label_map = {
"same": "同义",
"contains": "包含",
"contained_by": "被包含",
"coordinate": "同级",
"overlap": "部分重叠",
"related": "相关",
"unrelated": "无关"
}
return label_map.get(relation, relation)
def generate_historical_post_card_html(post_detail: Dict, inspiration_point: Dict) -> str:
"""生成历史帖子的紧凑卡片HTML"""
title = post_detail.get("title", "无标题")
body_text = post_detail.get("body_text", "")
images = post_detail.get("images", [])
like_count = post_detail.get("like_count", 0)
collect_count = post_detail.get("collect_count", 0)
comment_count = post_detail.get("comment_count", 0)
author = post_detail.get("channel_account_name", "")
link = post_detail.get("link", "#")
publish_time = post_detail.get("publish_time", "")
# 获取灵感点信息
point_name = inspiration_point.get("点的名称", "")
point_desc = inspiration_point.get("点的描述", "")
# 准备详情数据(用于模态框)
import json
post_detail_data = {
"title": title,
"body_text": body_text,
"images": images,
"like_count": like_count,
"comment_count": comment_count,
"collect_count": collect_count,
"author": author,
"publish_time": publish_time,
"link": link
}
post_data_json = json.dumps(post_detail_data, ensure_ascii=False)
post_data_json_escaped = html_module.escape(post_data_json)
# 截取正文预览(前80个字符)
body_preview = body_text[:80] + "..." if len(body_text) > 80 else body_text
# 生成缩略图
thumbnail_html = ""
if images:
thumbnail_html = f''
html = f'''
灵感点特征匹配分析