{"id":15103568,"url":"https://github.com/fyt3rp4til/tfidf-emotiondetection","last_synced_at":"2026-02-24T07:39:36.013Z","repository":{"id":255646900,"uuid":"853283511","full_name":"FYT3RP4TIL/TFIDF-EmotionDetection","owner":"FYT3RP4TIL","description":null,"archived":false,"fork":false,"pushed_at":"2024-09-06T13:03:58.000Z","size":231,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-30T15:51:26.472Z","etag":null,"topics":["multinomial-naive-bayes","n-grams","random-forest","spacy","tfidf-vectorizer"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/FYT3RP4TIL.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-09-06T10:55:40.000Z","updated_at":"2024-09-06T13:04:01.000Z","dependencies_parsed_at":"2024-09-06T14:25:33.493Z","dependency_job_id":"cd11f6d9-bb6a-4ccd-b02b-2fdac5eeac87","html_url":"https://github.com/FYT3RP4TIL/TFIDF-EmotionDetection","commit_stats":null,"previous_names":["fyt3rp4til/tfidf-emotiondetection"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FYT3RP4TIL%2FTFIDF-EmotionDetection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FYT3RP4TIL%2FTFIDF-EmotionDetection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FYT3RP4TIL%2FTFIDF-EmotionDetection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FYT3RP4TIL%2FTFIDF-EmotionDetection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FYT3RP4TIL","download_url":"https://codeload.github.com/FYT3RP4TIL/TFIDF-EmotionDetection/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237754151,"owners_count":19360654,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["multinomial-naive-bayes","n-grams","random-forest","spacy","tfidf-vectorizer"],"created_at":"2024-09-25T19:40:32.126Z","updated_at":"2025-10-23T00:30:27.142Z","avatar_url":"https://github.com/FYT3RP4TIL.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e\r\n  🎭 Emotion Detection in Text:\u003cbr\u003e\r\n  A Deep Dive into Sentiment Analysis\u003cbr\u003e\r\n  Via TFIDF and N-Grams\r\n\u003c/h1\u003e\r\n\r\n## 📊 Project Overview\r\n\r\nWelcome to our advanced Emotion Detection project! This comprehensive study delves into the intricate world of sentiment analysis, employing cutting-edge Natural Language Processing (NLP) techniques and machine learning models to decode the subtle nuances of human emotions expressed in text.\r\n\r\n### 🎯 Project Objectives\r\n\r\n1. Develop robust models for accurately classifying text into three primary emotions:\r\n   - 😨 Fear: Anticipation of threat or danger\r\n   - 😡 Anger: Strong feeling of annoyance, displeasure, or hostility\r\n   - 😂 Joy: Feeling of great pleasure and happiness\r\n2. Compare and contrast various NLP techniques and machine learning algorithms\r\n3. Explore the impact of text preprocessing on model performance\r\n4. Create a foundation for more advanced emotion detection systems\r\n\r\n## 📚 Dataset: The Foundation of Our Analysis\r\n\r\n### Dataset Source and Description\r\n\r\nOur project utilizes the [Emotions Dataset for NLP](https://www.kaggle.com/datasets/praveengovi/emotions-dataset-for-nlp), a carefully curated collection of text samples paired with corresponding emotions.\r\n\r\n#### Key Features:\r\n\r\n1. 💬 **Comment**: \r\n   - Real-world statements and messages related to various events and situations\r\n   - Diverse in length, complexity, and subject matter\r\n   - Represents natural language usage across different contexts\r\n\r\n2. 🔮 **Emotion**: \r\n   - The labeled emotion associated with each comment\r\n   - Limited to three primary categories: fear, anger, and joy\r\n   - Provides a balanced representation of each emotion\r\n\r\n### Dataset Statistics\r\n\r\n#### Class Distribution\r\n```\r\n😡 Anger | ████████████████████ | 2000 samples | 33.73%\r\n😂 Joy   | ████████████████████ | 2000 samples | 33.73%\r\n😨 Fear  | ███████████████████▌ | 1937 samples | 32.54%\r\n```\r\n\r\nTotal samples: 5,937\r\n\r\n#### Data Quality\r\n- Balanced distribution ensures unbiased model training\r\n- Large sample size provides robust training and evaluation capabilities\r\n- Real-world text data captures the complexity of natural language expression\r\n\r\n## 🚀 Model Development and Evaluation\r\n\r\nWe implemented and evaluated several models, each with unique characteristics and performance profiles. Here's an in-depth look at our model lineup:\r\n\r\n### 1. 🌳 Random Forest with 3-Grams\r\n\r\n#### Configuration:\r\n- Vectorization: CountVectorizer with ngram_range=(3, 3)\r\n- Model: RandomForestClassifier with default parameters\r\n\r\n#### Performance:\r\n```\r\n              precision    recall  f1-score   support\r\n           0       0.58      0.26      0.36       400\r\n           1       0.37      0.80      0.51       388\r\n           2       0.53      0.22      0.31       400\r\n    accuracy                           0.42      1188\r\n   macro avg       0.49      0.43      0.39      1188\r\nweighted avg       0.50      0.42      0.39      1188\r\n```\r\n\r\n#### Analysis:\r\n- Lower overall performance compared to other models\r\n- High recall for class 1 (0.80) but poor precision (0.37)\r\n- Struggles with classes 0 and 2, indicating potential overfitting to class 1\r\n\r\n### 2. 🧮 Multinomial Naive Bayes with 1-2 Grams\r\n\r\n#### Configuration:\r\n- Vectorization: CountVectorizer with ngram_range=(1, 2)\r\n- Model: MultinomialNB with default parameters\r\n\r\n#### Performance:\r\n```\r\n              precision    recall  f1-score   support\r\n           0       0.87      0.86      0.87       400\r\n           1       0.87      0.83      0.85       388\r\n           2       0.83      0.88      0.85       400\r\n    accuracy                           0.86      1188\r\n   macro avg       0.86      0.86      0.86      1188\r\nweighted avg       0.86      0.86      0.86      1188\r\n```\r\n\r\n#### Analysis:\r\n- Significant improvement over the 3-gram Random Forest model\r\n- Balanced performance across all classes\r\n- Good overall accuracy of 86%\r\n\r\n### 3. 🌲 Random Forest with 1-2 Grams\r\n\r\n#### Configuration:\r\n- Vectorization: CountVectorizer with ngram_range=(1, 2)\r\n- Model: RandomForestClassifier with default parameters\r\n\r\n#### Performance:\r\n```\r\n              precision    recall  f1-score   support\r\n           0       0.83      0.96      0.89       400\r\n           1       0.95      0.87      0.91       388\r\n           2       0.93      0.87      0.90       400\r\n    accuracy                           0.90      1188\r\n   macro avg       0.90      0.90      0.90      1188\r\nweighted avg       0.90      0.90      0.90      1188\r\n```\r\n\r\n#### Analysis:\r\n- Further improvement in overall performance\r\n- High precision and recall across all classes\r\n- Particularly strong in identifying class 1 (0.95 precision)\r\n\r\n### 4. 📊 Random Forest with TF-IDF\r\n\r\n#### Configuration:\r\n- Vectorization: TfidfVectorizer with default parameters\r\n- Model: RandomForestClassifier with default parameters\r\n\r\n#### Performance:\r\n```\r\n              precision    recall  f1-score   support\r\n           0       0.89      0.95      0.92       400\r\n           1       0.92      0.91      0.92       388\r\n           2       0.94      0.88      0.91       400\r\n    accuracy                           0.92      1188\r\n   macro avg       0.92      0.92      0.92      1188\r\nweighted avg       0.92      0.92      0.92      1188\r\n```\r\n\r\n#### Analysis:\r\n- Best performing model before preprocessing\r\n- Excellent balance of precision and recall across all classes\r\n- TF-IDF vectorization appears to capture important features effectively\r\n\r\n## 🧹 Text Preprocessing: Enhancing Model Input\r\n\r\nTo further improve our models' performance, we implemented a comprehensive text preprocessing pipeline. This crucial step helps to normalize the input data, reduce noise, and focus on the most meaningful aspects of the text.\r\n\r\n### Preprocessing Steps:\r\n\r\n1. **Removing Stop Words**\r\n   - Eliminates common words (e.g., \"the\", \"is\", \"at\") that typically don't carry significant emotional content\r\n   - Helps models focus on more meaningful words\r\n\r\n2. **Removing Punctuation**\r\n   - Strips away punctuation marks to standardize text input\r\n   - Reduces noise and potential inconsistencies in punctuation usage\r\n\r\n3. **Applying Lemmatization**\r\n   - Reduces words to their base or dictionary form\r\n   - Helps consolidate different forms of a word (e.g., \"running\", \"ran\", \"runs\" → \"run\")\r\n   - Maintains the core meaning of words better than simple stemming\r\n\r\n### Implementation:\r\n\r\nWe utilized the powerful spaCy library for our preprocessing pipeline:\r\n\r\n```python\r\nimport spacy\r\n\r\nnlp = spacy.load(\"en_core_web_sm\")\r\n\r\ndef preprocess(text):\r\n    doc = nlp(text)\r\n    filtered_tokens = []\r\n    for token in doc:\r\n        if token.is_stop or token.is_punct:\r\n            continue\r\n        filtered_tokens.append(token.lemma_)\r\n    \r\n    return \" \".join(filtered_tokens)\r\n```\r\n\r\nThis function processes each input text by:\r\n1. Tokenizing the text using spaCy's linguistic model\r\n2. Filtering out stop words and punctuation\r\n3. Lemmatizing the remaining tokens\r\n4. Joining the processed tokens back into a single string\r\n\r\n## 🏆 Results After Preprocessing\r\n\r\nThe application of our preprocessing pipeline led to significant improvements in model performance:\r\n\r\n### 5. 🌳 Random Forest (1-2 Grams) with Preprocessing\r\n\r\n#### Configuration:\r\n- Preprocessing: Custom pipeline (stop words removal, punctuation removal, lemmatization)\r\n- Vectorization: CountVectorizer with ngram_range=(1, 2)\r\n- Model: RandomForestClassifier with default parameters\r\n\r\n#### Performance:\r\n```\r\n              precision    recall  f1-score   support\r\n           0       0.94      0.95      0.95       400\r\n           1       0.94      0.91      0.93       388\r\n           2       0.93      0.93      0.93       400\r\n    accuracy                           0.93      1188\r\n   macro avg       0.93      0.93      0.93      1188\r\nweighted avg       0.93      0.93      0.93      1188\r\n```\r\n\r\n#### Analysis:\r\n- Substantial improvement over the non-preprocessed version\r\n- High and balanced precision and recall across all classes\r\n- Preprocessing appears to have helped in distinguishing between emotions more effectively\r\n\r\n### 6. 📊 Random Forest (TF-IDF) with Preprocessing\r\n\r\n#### Configuration:\r\n- Preprocessing: Custom pipeline (stop words removal, punctuation removal, lemmatization)\r\n- Vectorization: TfidfVectorizer with default parameters\r\n- Model: RandomForestClassifier with default parameters\r\n\r\n#### Performance:\r\n```\r\n              precision    recall  f1-score   support\r\n           0       0.92      0.96      0.94       400\r\n           1       0.92      0.92      0.92       388\r\n           2       0.94      0.90      0.92       400\r\n    accuracy                           0.93      1188\r\n   macro avg       0.93      0.93      0.93      1188\r\nweighted avg       0.93      0.93      0.93      1188\r\n```\r\n\r\n#### Analysis:\r\n- Matches the performance of the 1-2 Grams model with preprocessing\r\n- Slight improvements in certain class-specific metrics\r\n- Demonstrates the robust performance of Random Forest with TF-IDF, even with preprocessing\r\n\r\n## 🎉 Conclusion and Key Findings\r\n\r\nAfter extensive experimentation and analysis, we can draw several important conclusions:\r\n\r\n1. **Preprocessing Impact**: The application of our custom preprocessing pipeline consistently improved model performance, highlighting the importance of text normalization in emotion detection tasks.\r\n\r\n2. **Best Performing Model**: The Random Forest model, whether using 1-2 Grams or TF-IDF vectorization, combined with our preprocessing pipeline, achieved the best overall performance with 93% accuracy.\r\n\r\n3. **Feature Representation**: Both Count Vectorization (with 1-2 Grams) and TF-IDF Vectorization proved effective in capturing relevant features for emotion detection.\r\n\r\n4. **Balanced Performance**: Our top models demonstrated balanced precision and recall across all three emotion classes, indicating robust and reliable classification capabilities.\r\n\r\n5. **Model Complexity**: The Random Forest algorithm consistently outperformed simpler models like Multinomial Naive Bayes, suggesting that the complexity of emotion detection benefits from ensemble methods.\r\n\r\n## 🔮 Future Directions and Potential Enhancements\r\n\r\nWhile our current models have achieved impressive results, there are several exciting avenues for further research and improvement:\r\n\r\n1. **Deep Learning Exploration**: \r\n   - Implement and evaluate deep learning models such as LSTM (Long Short-Term Memory) or BERT (Bidirectional Encoder Representations from Transformers)\r\n   - Explore the potential of transfer learning using pre-trained language models\r\n\r\n2. **Expanded Emotion Categories**: \r\n   - Extend the model to classify a broader range of emotions (e.g., surprise, disgust, sadness)\r\n   - Investigate multi-label classification for texts expressing multiple emotions\r\n\r\n3. **Advanced Cross-Validation**: \r\n   - Implement k-fold cross-validation for more robust model evaluation\r\n   - Explore stratified sampling techniques to ensure balanced representation of emotions in all folds\r\n\r\n4. **Real-Time Application Development**: \r\n   - Develop a web application or API for real-time emotion detection in text\r\n   - Integrate the emotion detection system with chatbots or social media analysis tools\r\n\r\n5. **Feature Importance Analysis**: \r\n   - Conduct in-depth analysis of feature importance to understand key indicators of different emotions\r\n   - Use this insight to further refine preprocessing and feature selection techniques\r\n\r\n6. **Error Analysis**: \r\n   - Perform detailed error analysis to identify common misclassifications\r\n   - Use these insights to develop targeted improvements in preprocessing or model architecture\r\n\r\n7. **Multilingual Expansion**: \r\n   - Extend the emotion detection capabilities to multiple languages\r\n   - Investigate cross-lingual emotion detection techniques\r\n\r\nBy pursuing these directions, we aim to push the boundaries of emotion detection in text, contributing to the broader field of affective computing and natural language understanding.\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffyt3rp4til%2Ftfidf-emotiondetection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffyt3rp4til%2Ftfidf-emotiondetection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffyt3rp4til%2Ftfidf-emotiondetection/lists"}