diff --git a/EmotionDetection/__init__.py b/EmotionDetection/__init__.py new file mode 100644 index 000000000..6238ddc87 --- /dev/null +++ b/EmotionDetection/__init__.py @@ -0,0 +1 @@ +from .emotion_detection import emotion_detector \ No newline at end of file diff --git a/EmotionDetection/emotion_detection.py b/EmotionDetection/emotion_detection.py new file mode 100644 index 000000000..67b0f122d --- /dev/null +++ b/EmotionDetection/emotion_detection.py @@ -0,0 +1,39 @@ +import nltk +from nltk.sentiment import SentimentIntensityAnalyzer + +nltk.download("vader_lexicon", quiet=True) # Download the lexicon if not already available + + +def emotion_detector(text_to_analyze): + # Handle blank or None input (like API status code 400) + if not text_to_analyze or text_to_analyze.strip() == "": + return { + "anger": None, + "disgust": None, + "fear": None, + "joy": None, + "sadness": None, + "dominant_emotion": None, + } + + analyzer = SentimentIntensityAnalyzer() + scores = analyzer.polarity_scores(text_to_analyze) + + emotion_scores = { + "anger": scores["neg"] * 0.5, + "disgust": scores["neg"] * 0.3 + 0.1 if "disgust" in text_to_analyze.lower() else 0, + "fear": scores["neg"] * 0.3 + 0.1 if "afraid" in text_to_analyze.lower() or "fear" in text_to_analyze.lower() else 0, + "joy": scores["pos"], + "sadness": scores["neg"] * 0.4 + 0.1 if "sad" in text_to_analyze.lower() else 0, + } + + dominant_emotion = max(emotion_scores, key=emotion_scores.get) + + return { + "anger": emotion_scores["anger"], + "disgust": emotion_scores["disgust"], + "fear": emotion_scores["fear"], + "joy": emotion_scores["joy"], + "sadness": emotion_scores["sadness"], + "dominant_emotion": dominant_emotion, + } diff --git a/server.py b/server.py new file mode 100644 index 000000000..d23fec40a --- /dev/null +++ b/server.py @@ -0,0 +1,48 @@ +""" +Flask server application for emotion detection. +""" + +from flask import Flask, request, render_template +from EmotionDetection.emotion_detection import emotion_detector + +app = Flask(__name__) + +@app.route("/") +def index(): + """ + Render the main page with the input form. + """ + return render_template("index.html") + + +@app.route("/emotionDetector", methods=["GET", "POST"]) +def emotion_analysis(): + """ + Handle requests to the /emotionDetector route. + Analyze the input text and return the detected emotions. + """ + if request.method == "GET": + text = request.args.get("textToAnalyze") + else: + text = request.form["text"] + + result = emotion_detector(text) + + if result["dominant_emotion"] is None: + return "Invalid text! Please try again!" + + response = ( + f"For the given statement, the system response is " + f"'anger': {result['anger']}, " + f"'disgust': {result['disgust']}, " + f"'fear': {result['fear']}, " + f"'joy': {result['joy']} and " + f"'sadness': {result['sadness']}. " + f"The dominant emotion is {result['dominant_emotion']}." + ) + + return response + + +if __name__ == "__main__": + app.run(host="0.0.0.0", port=5000) diff --git a/test_emotion_detection.py b/test_emotion_detection.py new file mode 100644 index 000000000..cd967bb79 --- /dev/null +++ b/test_emotion_detection.py @@ -0,0 +1,27 @@ +import unittest +from EmotionDetection import emotion_detector + +class TestEmotionDetection(unittest.TestCase): + + def test_joy(self): + result = emotion_detector("I am glad this happened") + self.assertEqual(result["dominant_emotion"], "joy") + + def test_anger(self): + result = emotion_detector("I am really mad about this") + self.assertEqual(result["dominant_emotion"], "anger") + + def test_disgust(self): + result = emotion_detector("I feel disgusted just hearing about this") + self.assertEqual(result["dominant_emotion"], "disgust") + + def test_sadness(self): + result = emotion_detector("I am so sad about this") + self.assertEqual(result["dominant_emotion"], "sadness") + + def test_fear(self): + result = emotion_detector("I am really afraid that this will happen") + self.assertEqual(result["dominant_emotion"], "fear") + +if __name__ == "__main__": + unittest.main()