|
import os |
|
from wordcloud import WordCloud |
|
import matplotlib.pyplot as plt |
|
from transformers import pipeline |
|
|
|
|
|
sentiment_analyzer = pipeline("sentiment-analysis") |
|
|
|
def analyze_sentiment(text): |
|
"""Analyzes sentiment and returns sentiment label with confidence.""" |
|
result = sentiment_analyzer(text)[0] |
|
return result['label'], result['score'] |
|
|
|
def generate_sentiment_graph(sentiments): |
|
"""Creates a bar chart for sentiment analysis results.""" |
|
|
|
assets_dir = "assets" |
|
|
|
|
|
if os.path.exists(assets_dir) and not os.path.isdir(assets_dir): |
|
os.remove(assets_dir) |
|
os.makedirs(assets_dir) |
|
elif not os.path.exists(assets_dir): |
|
os.makedirs(assets_dir) |
|
|
|
|
|
labels = [s[0] for s in sentiments] |
|
scores = [s[1] for s in sentiments] |
|
|
|
|
|
plt.figure(figsize=(6, 3)) |
|
plt.bar(labels, scores, color=["green" if lbl == "POSITIVE" else "red" for lbl in labels]) |
|
plt.xlabel("Sentiment") |
|
plt.ylabel("Confidence Score") |
|
plt.title("Sentiment Analysis Results") |
|
|
|
|
|
graph_path = os.path.join(assets_dir, "sentiment_graph.png") |
|
plt.savefig(graph_path) |
|
plt.close() |
|
|
|
return graph_path |
|
|
|
def generate_wordcloud(text): |
|
"""Generates a word cloud and ensures 'assets/' is always a folder.""" |
|
|
|
assets_dir = "assets" |
|
|
|
|
|
if os.path.exists(assets_dir) and not os.path.isdir(assets_dir): |
|
os.remove(assets_dir) |
|
os.makedirs(assets_dir) |
|
elif not os.path.exists(assets_dir): |
|
os.makedirs(assets_dir) |
|
|
|
|
|
wordcloud = WordCloud(width=800, height=400, background_color="white").generate(text) |
|
wordcloud_path = os.path.join(assets_dir, "wordcloud.png") |
|
wordcloud.to_file(wordcloud_path) |
|
|
|
return wordcloud_path |
|
|