"""브랜드 x 키워드 교차 분석 + LLM 이유 태그."""
import html
import pandas as pd
import plotly.graph_objects as go
import streamlit as st
from core.api_client import ChainShiftClient
def render_brand_keyword_cross(data: dict, competitor: bool = False):
"""브랜드 x 키워드 교차 분석 + LLM 이유 태그."""
brand_label = "경쟁사" if competitor else "자사"
st.markdown(f"**{brand_label} 브랜드 x 키워드 교차 분석**")
st.caption(f"키워드별로 {brand_label} 브랜드가 어떤 맥락에서 언급되는지 분석합니다")
try:
client = ChainShiftClient(api_key=data.get("api_key"), access_token=data.get("access_token"))
response = client.get_keyword_brand_analysis(data["campaign_id"], competitor=competitor)
resp_data = response.get("data", {})
items = resp_data.get("items", [])
except Exception as e:
st.error(f"브랜드x키워드 분석 로드 실패: {e}")
return
if not items:
st.info("브랜드x키워드 교차 데이터가 없습니다")
return
# Cross table
rows = []
for item in items:
keyword = item.get("keyword", "")
brand = item.get("brand", "")
total = item.get("total", 0)
sent = item.get("sentiment", {})
pos = sent.get("positive", 0)
neu = sent.get("neutral", 0)
neg = sent.get("negative", 0)
reason_tags = item.get("reason_tags", [])
top_tags = ", ".join(rt.get("tag", "") for rt in reason_tags[:3])
rows.append({
"키워드": keyword,
"브랜드": brand,
"총 건수": total,
"긍정": pos,
"중립": neu,
"부정": neg,
"주요 이유 태그": top_tags,
})
if rows:
df = pd.DataFrame(rows).sort_values("부정", ascending=False)
st.dataframe(df, use_container_width=True, hide_index=True)
# LLM Reason Tag Analysis
st.markdown("---")
st.markdown("**🏷️ LLM 이유 태그 분석**")
st.caption("LLM이 자동 생성한 이유 태그 빈도")
# Aggregate all reason tags
tag_counts: dict[str, int] = {}
tag_examples: dict[str, str] = {}
for item in items:
for rt in item.get("reason_tags", []):
tag = rt.get("tag", "")
count = rt.get("count", 0)
if tag:
tag_counts[tag] = tag_counts.get(tag, 0) + count
if tag not in tag_examples and rt.get("example_sentence"):
tag_examples[tag] = rt["example_sentence"]
if tag_counts:
# Bar chart for top tags
sorted_tags = sorted(tag_counts.items(), key=lambda x: -x[1])[:15]
tag_names = [t[0] for t in sorted_tags]
tag_vals = [t[1] for t in sorted_tags]
fig = go.Figure(go.Bar(
x=tag_vals,
y=tag_names,
orientation="h",
marker_color="#059669",
))
fig.update_layout(
height=max(250, len(sorted_tags) * 30),
margin=dict(l=20, r=20, t=10, b=10),
yaxis=dict(autorange="reversed"),
)
st.plotly_chart(fig, use_container_width=True, config={"displayModeBar": False})
# Tag detail table
tag_rows = []
for tag, count in sorted_tags:
example = tag_examples.get(tag, "")
tag_rows.append({
"태그": tag,
"빈도": count,
"예시 문장": example[:100] if example else "",
})
st.dataframe(pd.DataFrame(tag_rows), use_container_width=True, hide_index=True)
else:
st.info("LLM 이유 태그 데이터가 없습니다")