File size: 1,259 Bytes
a4a72fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# modules/visualization.py

import matplotlib.pyplot as plt
from wordcloud import WordCloud
from config.settings import WC_WIDTH, WC_HEIGHT, WC_BG_COLOR

def generate_wordcloud(texts: list, save_path: str = "wordcloud.png"):
    """
    Generate a word cloud from a list of texts and save as an image.
    """
    combined_text = " ".join(texts)
    wc = WordCloud(
        width=WC_WIDTH,
        height=WC_HEIGHT,
        background_color=WC_BG_COLOR
    ).generate(combined_text)

    plt.figure(figsize=(10, 5))
    plt.imshow(wc, interpolation="bilinear")
    plt.axis("off")
    plt.tight_layout(pad=0)
    plt.savefig(save_path)
    plt.close()

    return save_path


def plot_sentiment_distribution(sentiment_results: list, save_path: str = "sentiment_distribution.png"):
    """
    Plot a simple bar chart of sentiment distribution.
    """
    labels = [res["label"] for res in sentiment_results if "label" in res]
    counts = {label: labels.count(label) for label in set(labels)}

    plt.figure(figsize=(6, 4))
    plt.bar(counts.keys(), counts.values(), color=["green", "red", "blue"])
    plt.title("Sentiment Distribution")
    plt.xlabel("Sentiment")
    plt.ylabel("Count")
    plt.savefig(save_path)
    plt.close()

    return save_path