Spaces:
Runtime error
Runtime error
Create visualization.py
Browse files- modules/visualization.py +43 -0
modules/visualization.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# modules/visualization.py
|
| 2 |
+
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
from wordcloud import WordCloud
|
| 5 |
+
from config.settings import WC_WIDTH, WC_HEIGHT, WC_BG_COLOR
|
| 6 |
+
|
| 7 |
+
def generate_wordcloud(texts: list, save_path: str = "wordcloud.png"):
|
| 8 |
+
"""
|
| 9 |
+
Generate a word cloud from a list of texts and save as an image.
|
| 10 |
+
"""
|
| 11 |
+
combined_text = " ".join(texts)
|
| 12 |
+
wc = WordCloud(
|
| 13 |
+
width=WC_WIDTH,
|
| 14 |
+
height=WC_HEIGHT,
|
| 15 |
+
background_color=WC_BG_COLOR
|
| 16 |
+
).generate(combined_text)
|
| 17 |
+
|
| 18 |
+
plt.figure(figsize=(10, 5))
|
| 19 |
+
plt.imshow(wc, interpolation="bilinear")
|
| 20 |
+
plt.axis("off")
|
| 21 |
+
plt.tight_layout(pad=0)
|
| 22 |
+
plt.savefig(save_path)
|
| 23 |
+
plt.close()
|
| 24 |
+
|
| 25 |
+
return save_path
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def plot_sentiment_distribution(sentiment_results: list, save_path: str = "sentiment_distribution.png"):
|
| 29 |
+
"""
|
| 30 |
+
Plot a simple bar chart of sentiment distribution.
|
| 31 |
+
"""
|
| 32 |
+
labels = [res["label"] for res in sentiment_results if "label" in res]
|
| 33 |
+
counts = {label: labels.count(label) for label in set(labels)}
|
| 34 |
+
|
| 35 |
+
plt.figure(figsize=(6, 4))
|
| 36 |
+
plt.bar(counts.keys(), counts.values(), color=["green", "red", "blue"])
|
| 37 |
+
plt.title("Sentiment Distribution")
|
| 38 |
+
plt.xlabel("Sentiment")
|
| 39 |
+
plt.ylabel("Count")
|
| 40 |
+
plt.savefig(save_path)
|
| 41 |
+
plt.close()
|
| 42 |
+
|
| 43 |
+
return save_path
|