Military_Topical_Sentiment_Analysis / sentiment_analyzer.py
Sami2000's picture
Update sentiment_analyzer.py
2334393 verified
raw
history blame contribute delete
799 Bytes
from transformers import pipeline
sentiment_model = pipeline(
"sentiment-analysis",
model="cardiffnlp/twitter-roberta-base-sentiment-latest",
device=-1
)
def analyze_sentiment(reddit_data):
sentiments = []
if not reddit_data:
return sentiments # handle empty case
comments_iter = []
if isinstance(reddit_data, dict):
for comments in reddit_data.values():
comments_iter.extend(comments)
elif isinstance(reddit_data, list):
comments_iter = reddit_data
for comment in comments_iter:
body = comment.get("body", "")
if body:
result = sentiment_model(body[:512])[0]
label = result["label"].lower()
sentiments.append({"body": body, "sentiment": label})
return sentiments