Spaces:
Sleeping
Sleeping
Update charts.py
Browse files
charts.py
CHANGED
|
@@ -76,9 +76,9 @@ def misinfo_gauge(score: float, label: str) -> go.Figure:
|
|
| 76 |
# Sentiment Donut β
|
| 77 |
|
| 78 |
def sentiment_donut(summary: Dict) -> go.Figure:
|
| 79 |
-
"""Donut chart:
|
| 80 |
-
labels = ["
|
| 81 |
-
values = [summary["
|
| 82 |
colors = [GREEN, TEXT_DIM, RED]
|
| 83 |
|
| 84 |
fig = go.Figure(go.Pie(
|
|
@@ -94,7 +94,7 @@ def sentiment_donut(summary: Dict) -> go.Figure:
|
|
| 94 |
|
| 95 |
# Centre annotation
|
| 96 |
avg = summary.get("avg_compound", 0)
|
| 97 |
-
overall = "π
|
| 98 |
fig.add_annotation(
|
| 99 |
text=f"<b>{overall}</b><br><span style='font-size:11px;color:{TEXT_DIM}'>{summary['total']} comments</span>",
|
| 100 |
x=0.5, y=0.5,
|
|
@@ -453,11 +453,11 @@ def sentiment_timeline(comments_df: pd.DataFrame, sentiments: List[Dict]) -> go.
|
|
| 453 |
df = comments_df.copy()
|
| 454 |
df["compound"] = [s.get("compound", 0) for s in sentiments]
|
| 455 |
df["label"] = [s.get("label", "NEUTRAL") for s in sentiments]
|
| 456 |
-
df["color"] = df["label"].map({"
|
| 457 |
df["text_short"] = df["text"].str[:80] + "β¦"
|
| 458 |
|
| 459 |
fig = go.Figure()
|
| 460 |
-
for lbl, clr in [("
|
| 461 |
sub = df[df["label"] == lbl]
|
| 462 |
if sub.empty:
|
| 463 |
continue
|
|
@@ -488,13 +488,13 @@ def sentiment_timeline(comments_df: pd.DataFrame, sentiments: List[Dict]) -> go.
|
|
| 488 |
return fig
|
| 489 |
|
| 490 |
|
| 491 |
-
#
|
| 492 |
|
| 493 |
def keyword_comparison(
|
| 494 |
pos_kw: List[Tuple[str, float]],
|
| 495 |
neg_kw: List[Tuple[str, float]],
|
| 496 |
) -> go.Figure:
|
| 497 |
-
"""Diverging bar chart:
|
| 498 |
if not pos_kw and not neg_kw:
|
| 499 |
return _empty_fig("Sentiment Keywords")
|
| 500 |
|
|
@@ -508,7 +508,7 @@ def keyword_comparison(
|
|
| 508 |
pw, pv = zip(*pos_kw)
|
| 509 |
max_p = max(pv) or 1
|
| 510 |
fig.add_trace(go.Bar(
|
| 511 |
-
name="
|
| 512 |
y=list(pw),
|
| 513 |
x=[v/max_p*100 for v in pv],
|
| 514 |
orientation="h",
|
|
@@ -520,7 +520,7 @@ def keyword_comparison(
|
|
| 520 |
nw, nv = zip(*neg_kw)
|
| 521 |
max_n = max(nv) or 1
|
| 522 |
fig.add_trace(go.Bar(
|
| 523 |
-
name="
|
| 524 |
y=list(nw),
|
| 525 |
x=[-v/max_n*100 for v in nv],
|
| 526 |
orientation="h",
|
|
@@ -533,7 +533,7 @@ def keyword_comparison(
|
|
| 533 |
title=dict(text="Sentiment-Weighted Keywords", font=dict(size=13, color=TEXT_DIM), x=0),
|
| 534 |
height=360,
|
| 535 |
barmode="overlay",
|
| 536 |
-
xaxis=dict(title="β
|
| 537 |
zerolinecolor=BORDER, zerolinewidth=2),
|
| 538 |
yaxis=dict(tickfont=dict(size=10)),
|
| 539 |
legend=dict(orientation="h", y=1.1),
|
|
|
|
| 76 |
# Sentiment Donut β
|
| 77 |
|
| 78 |
def sentiment_donut(summary: Dict) -> go.Figure:
|
| 79 |
+
"""Donut chart: Positively Engagement / Negatively Engagement / Neutral breakdown."""
|
| 80 |
+
labels = ["Positively Engagement", "Neutral", "Negatively Engagement"]
|
| 81 |
+
values = [summary["POSITIVELY ENGAGEMENT"], summary["NEUTRAL"], summary["NEGATIVELY ENGAGEMENT"]]
|
| 82 |
colors = [GREEN, TEXT_DIM, RED]
|
| 83 |
|
| 84 |
fig = go.Figure(go.Pie(
|
|
|
|
| 94 |
|
| 95 |
# Centre annotation
|
| 96 |
avg = summary.get("avg_compound", 0)
|
| 97 |
+
overall = "π Positively Engagement" if avg > 0.05 else ("π Negatively Engagement" if avg < -0.05 else "π Mixed")
|
| 98 |
fig.add_annotation(
|
| 99 |
text=f"<b>{overall}</b><br><span style='font-size:11px;color:{TEXT_DIM}'>{summary['total']} comments</span>",
|
| 100 |
x=0.5, y=0.5,
|
|
|
|
| 453 |
df = comments_df.copy()
|
| 454 |
df["compound"] = [s.get("compound", 0) for s in sentiments]
|
| 455 |
df["label"] = [s.get("label", "NEUTRAL") for s in sentiments]
|
| 456 |
+
df["color"] = df["label"].map({"POSITIVELY ENGAGEMENT": GREEN, "NEGATIVELY ENGAGEMENT": RED, "NEUTRAL": AMBER})
|
| 457 |
df["text_short"] = df["text"].str[:80] + "β¦"
|
| 458 |
|
| 459 |
fig = go.Figure()
|
| 460 |
+
for lbl, clr in [("POSITIVELY ENGAGEMENT", GREEN), ("NEGATIVELY ENGAGEMENT", RED), ("NEUTRAL", AMBER)]:
|
| 461 |
sub = df[df["label"] == lbl]
|
| 462 |
if sub.empty:
|
| 463 |
continue
|
|
|
|
| 488 |
return fig
|
| 489 |
|
| 490 |
|
| 491 |
+
# Positively Engagement vs Negatively Engagement Keyword Comparison β
|
| 492 |
|
| 493 |
def keyword_comparison(
|
| 494 |
pos_kw: List[Tuple[str, float]],
|
| 495 |
neg_kw: List[Tuple[str, float]],
|
| 496 |
) -> go.Figure:
|
| 497 |
+
"""Diverging bar chart: Positive keywords right, negative left."""
|
| 498 |
if not pos_kw and not neg_kw:
|
| 499 |
return _empty_fig("Sentiment Keywords")
|
| 500 |
|
|
|
|
| 508 |
pw, pv = zip(*pos_kw)
|
| 509 |
max_p = max(pv) or 1
|
| 510 |
fig.add_trace(go.Bar(
|
| 511 |
+
name="Positively Engagement",
|
| 512 |
y=list(pw),
|
| 513 |
x=[v/max_p*100 for v in pv],
|
| 514 |
orientation="h",
|
|
|
|
| 520 |
nw, nv = zip(*neg_kw)
|
| 521 |
max_n = max(nv) or 1
|
| 522 |
fig.add_trace(go.Bar(
|
| 523 |
+
name="Negatively Engagement",
|
| 524 |
y=list(nw),
|
| 525 |
x=[-v/max_n*100 for v in nv],
|
| 526 |
orientation="h",
|
|
|
|
| 533 |
title=dict(text="Sentiment-Weighted Keywords", font=dict(size=13, color=TEXT_DIM), x=0),
|
| 534 |
height=360,
|
| 535 |
barmode="overlay",
|
| 536 |
+
xaxis=dict(title="β Negatively Engagement | Positively Engagement β", gridcolor=BORDER, zeroline=True,
|
| 537 |
zerolinecolor=BORDER, zerolinewidth=2),
|
| 538 |
yaxis=dict(tickfont=dict(size=10)),
|
| 539 |
legend=dict(orientation="h", y=1.1),
|