"""Phase 14 — 3개 개선 방법 통합표 (사용자 이미지 포맷)
가로: 하이브리드 / AI-Meta (TAG) / 키워드 전처리
세로: 4개 유닛, 각 셀 Top-3 썸네일
"""
import sys
import json
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent))
from common import load_figma_texts
from extract_units import extract_units
from methods import (
method_kiwi_bm25, method_cross_encoder,
method_distinctive_kiwi, method_ai_meta_for_uid,
)
from phase10 import extract_titles_only_mdx, extract_titles_only_figma, TARGET_UNITS
from phase11 import method_ensemble
ROOT = Path(r"d:\ad-hoc\kei\design_agent")
PREVIEW_DIR = ROOT / "data" / "figma_previews"
def short_id(fid, frame_to_short):
return frame_to_short.get(str(fid), "?")
def main():
units_full = extract_units()
figma_full = load_figma_texts()
figma_titles = extract_titles_only_figma()
mdx_titles = {}
for uid, _, _, fname, mid, sub in TARGET_UNITS:
mdx_titles[uid] = extract_titles_only_mdx(fname, mid, sub)
with open(PREVIEW_DIR / "index.json", encoding="utf-8") as f:
idx_data = json.load(f)
frame_to_short = {info["frame_id"]: sid for sid, info in idx_data.items()}
# 3개 방법 정의
def m_hybrid(uid):
return method_ensemble(units_full[uid], mdx_titles[uid],
figma_full, figma_titles, alpha=0.7)
def m_ai_meta(uid):
return method_ai_meta_for_uid(uid, figma_full)
def m_keyword_prep(uid):
return method_distinctive_kiwi(units_full[uid], figma_full)
methods = [
("하이브리드 (α=0.7: 0.7 BM25 본문×전체 + 0.3 Cross 제목×제목)", m_hybrid),
("AI-Meta (사전 개념 TAG Jaccard+IDF)", m_ai_meta),
("키워드 전처리 (Distinctive-Kiwi: Kiwi+공통어 제거+IDF)", m_keyword_prep),
]
# 실행
results = {} # {method_name: {uid: top3_list}}
scores_hit = {mname: [] for mname, _ in methods}
for mname, fn in methods:
results[mname] = {}
for uid, _, correct_id, *_ in TARGET_UNITS:
ranked = fn(uid)
top3 = ranked[:3]
results[mname][uid] = top3
top1_sid = frame_to_short.get(str(top3[0][0]), "?") if top3 else "?"
scores_hit[mname].append(top1_sid == correct_id)
# ═══ MD 리포트 ═══
png_rel = "../../data/figma_previews/"
lines = []
lines.append("# Phase 14 — 3개 개선 방법 통합표")
lines.append("")
lines.append("| 방법 | MDX1 팝업(18) | MDX2 2.2(14) | MDX03-1(13) | MDX03-2(29) | 합계 |")
lines.append("|------|---|---|---|---|------|")
for mname, _ in methods:
hits = scores_hit[mname]
marks = ["✓" if h else "✗" for h in hits]
score = sum(hits)
lines.append(f"| {mname} | {marks[0]} | {marks[1]} | {marks[2]} | {marks[3]} | **{score}/4** |")
lines.append("")
# 콘텐츠 × 방법 매트릭스 (사용자 이미지 포맷)
lines.append("## 통합 매트릭스 — 각 셀에 Top-3 썸네일")
lines.append("")
header_cells = ["콘텐츠"] + [m[0] for m in methods]
lines.append("| " + " | ".join(header_cells) + " |")
lines.append("|" + "|".join(["------"] * len(header_cells)) + "|")
for uid, display, correct_id, *_ in TARGET_UNITS:
row = [f"**{display}**
(정답 Frame **{correct_id}**)"]
for mname, _ in methods:
top3 = results[mname][uid]
cells_imgs = []
for fid, score in top3:
sid = frame_to_short.get(str(fid), "?")
info = idx_data.get(sid, {})
png = info.get("png", "")
mark = "⭐ " if sid == correct_id else ""
cells_imgs.append(
f"{mark}
**{sid}** ({score * 100:.0f}%)"
)
row.append("
".join(cells_imgs))
lines.append("| " + " | ".join(row) + " |")
lines.append("")
# 유닛별 상세
lines.append("---")
lines.append("")
lines.append("## 유닛별 전략별 Top-3 상세")
lines.append("")
for uid, display, correct_id, *_ in TARGET_UNITS:
lines.append(f"### {display} — 정답 Frame **{correct_id}**")
lines.append("")
lines.append("| 방법 | 1순위 | 2순위 | 3순위 |")
lines.append("|------|-------|-------|-------|")
for mname, _ in methods:
top3 = results[mname][uid]
row = [mname]
for fid, score in top3:
sid = frame_to_short.get(str(fid), "?")
info = idx_data.get(sid, {})
png = info.get("png", "")
title = info.get("title_text", "").strip().replace("\n", " ") or ""
if len(title) > 15:
title = title[:15] + "…"
mark = "⭐ " if sid == correct_id else ""
row.append(
f"{mark}
**{sid}** ({score * 100:.0f}%)
{title}"
)
while len(row) < 4:
row.append("-")
lines.append("| " + " | ".join(row) + " |")
lines.append("")
out_path = Path(__file__).parent / "MATRIX_PHASE14.md"
out_path.write_text("\n".join(lines), encoding="utf-8")
print(f"완료: {out_path}")
# 콘솔 요약
print()
for mname, _ in methods:
hits = scores_hit[mname]
marks = ["✓" if h else "✗" for h in hits]
print(f" {mname[:55]:55s} {' '.join(marks)} = {sum(hits)}/4")
if __name__ == "__main__":
main()