Sentiment_Analysis / modules /visualization.py
Pro-Coder's picture
Create visualization.py
a4a72fe verified
Raw
History Blame Contribute Delete
1.26 kB
# 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