From 68642709b790ff566505345a4043b0da9cbf4f76 Mon Sep 17 00:00:00 2001 From: Dylan De Faoite Date: Sun, 11 Jan 2026 17:31:37 +0000 Subject: [PATCH] add rudimentary sentiment analysis endpoint to calculate average sentiment of posts --- server/app.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/server/app.py b/server/app.py index 1ae2247..fcdcad8 100644 --- a/server/app.py +++ b/server/app.py @@ -1,6 +1,7 @@ from flask import Flask from db.database import Database from connectors.reddit_connector import RedditConnector +from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer app = Flask(__name__) db = Database(db_name='ethnograph', user='ethnograph_user', password='ethnograph_pass') @@ -31,5 +32,33 @@ def fetch_subreddit(subreddit, limit = 10): return {"status": "success", "inserted_posts": len(posts)} +@app.route('/sentiment', methods=['GET']) +def sentiment_analysis(): + posts = db.execute_query( + "SELECT id, title, content FROM ethnograph.posts;" + ) + + analyzer = SentimentIntensityAnalyzer() + + total_sentiment = 0.0 + count = 0 + + for post in posts: + content = post.get("title") + if not content: + continue + + score = analyzer.polarity_scores(content)["compound"] + total_sentiment += score + count += 1 + + average_sentiment = total_sentiment / count if count else 0.0 + + return { + "status": "success", + "average_sentiment": average_sentiment, + "posts_analyzed": count + } + if __name__ == "__main__": app.run(debug=True) \ No newline at end of file