File size: 3,877 Bytes
208266a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import pandas as pd
import plotly.express as px


def build_bias_distribution_chart(summary: dict):
    rows = []

    for source, stats in summary.items():
        biased = stats.get("Biased", 0)
        not_biased = stats.get("Not Biased", stats.get("Not_Biased", 0))
        total = stats.get("total", biased + not_biased)

        rows.append(
            {
                "Source": source,
                "Biased": biased,
                "Not biased": not_biased,
                "Total": total,
            }
        )

    df = pd.DataFrame(rows)

    if df.empty:
        return None

    df = df.sort_values("Total", ascending=False)
    df_melted = df.melt(
        id_vars=["Source", "Total"],
        value_vars=["Biased", "Not biased"],
        var_name="Classification",
        value_name="Articles",
    )

    fig = px.bar(
        df_melted,
        x="Source",
        y="Articles",
        color="Classification",
        barmode="group",
        text="Articles",
        color_discrete_map={
            "Biased": "#c24138",
            "Not biased": "#247857",
        },
    )

    fig.update_traces(
        textposition="outside",
        marker_line_width=0,
        cliponaxis=False,
    )
    fig.update_layout(
        height=430,
        margin=dict(l=12, r=12, t=24, b=12),
        paper_bgcolor="rgba(0,0,0,0)",
        plot_bgcolor="rgba(0,0,0,0)",
        bargap=0.26,
        legend=dict(
            orientation="h",
            yanchor="bottom",
            y=1.02,
            xanchor="right",
            x=1,
            title=None,
        ),
        xaxis=dict(
            title=None,
            tickangle=-20,
            showgrid=False,
            linecolor="#d8dee9",
        ),
        yaxis=dict(
            title="Articles",
            gridcolor="#e8edf4",
            zeroline=False,
        ),
        font=dict(color="#15202b", family="Arial, sans-serif"),
    )

    return fig

def build_lean_bias_chart(results: list) -> object:
    from collections import defaultdict

    lean_counts = defaultdict(lambda: {"Biased": 0, "Not biased": 0})

    for article in results:
        lean = article.get("source_bias", "Unknown")
        label = article.get("text_label", "Unknown")
        if label == "Biased":
            lean_counts[lean]["Biased"] += 1
        elif label == "Not Biased":
            lean_counts[lean]["Not biased"] += 1

    rows = []
    for lean, counts in lean_counts.items():
        rows.append({
            "Lean": lean,
            "Biased": counts["Biased"],
            "Not biased": counts["Not biased"],
        })

    df = pd.DataFrame(rows)
    if df.empty:
        return None

    lean_order = ["Left", "Center-Left", "Center", "Center-Right", "Right", "Unknown"]
    df["Lean"] = pd.Categorical(df["Lean"], categories=lean_order, ordered=True)
    df = df.sort_values("Lean")

    df_melted = df.melt(
        id_vars="Lean",
        value_vars=["Biased", "Not biased"],
        var_name="Classification",
        value_name="Articles",
    )

    fig = px.bar(
        df_melted,
        x="Lean",
        y="Articles",
        color="Classification",
        barmode="group",
        text="Articles",
        color_discrete_map={"Biased": "#c24138", "Not biased": "#247857"},
    )

    fig.update_traces(textposition="outside", marker_line_width=0, cliponaxis=False)
    fig.update_layout(
        height=380,
        margin=dict(l=12, r=12, t=24, b=12),
        paper_bgcolor="rgba(0,0,0,0)",
        plot_bgcolor="rgba(0,0,0,0)",
        bargap=0.3,
        legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1, title=None),
        xaxis=dict(title=None, showgrid=False, linecolor="#d8dee9"),
        yaxis=dict(title="Articles", gridcolor="#e8edf4", zeroline=False),
        font=dict(color="#15202b", family="Arial, sans-serif"),
    )

    return fig