vydik-04/Emotion-Detection
GitHub: vydik-04/Emotion-Detection
一个结合集成机器学习与规则引擎的文本情感检测系统,能识别27种情感类别并处理否定语义。
Stars: 0 | Forks: 0
# 🧠 基于传统机器学习的文本情感检测(27 种情感)
一个强大的**文本情感检测**系统,结合了**机器学习 + 基于规则的 NLP**,可将文本准确分类为 **27 种不同的情感**。
本项目通过实施**混合架构**,超越了传统模型,提高了准确性,处理了否定情况,并针对实际使用优化了性能。
## 🚀 特性
- 🔥 检测 **27 种不同的情感**(基于 GoEmotions)
- ⚡ 混合系统(ML + 基于规则)
- 🧠 高级 NLP 预处理(否定处理、词形还原)
- 🤖 集成机器学习模型
- 📊 置信度分数和最高情感预测
- 💾 针对低资源系统的内存优化模型
- 🧪 广泛的测试和评估支持
## 🏗️ 项目架构
用户输入文本
↓
文本预处理
↓
特征提取 (TF-IDF)
↓
├── ML 模型(集成)
└── 基于规则的引擎
↓
混合决策引擎
↓
最终情感预测
## 📂 项目结构
├── data/
│ ├── emotion_dataset_1350k.csv
│ ├── massive_emotion_dataset.csv
│ └── emotion_dataset_augmented.csv
├── models/
│ ├── ultra_high_accuracy_emotion_model.joblib
│ ├── memory_optimized_emotion_model_*.joblib
│ ├── vectorizer.joblib
│ └── label_encoder.joblib
├── src/
│ ├── emotion_detector.py
│ ├── ultimate_emotion_detector.py
│ ├── memory_optimized_emotion_detector.py
├── training/
│ ├── train_ultra_high_accuracy.py
│ ├── improve_model_accuracy.py
│ └── evaluate_27_emotions.py
├── rule_based/
│ ├── emotion_keywords.json
│ └── emotion_keywords_complete.json
├── debugging/
│ ├── debug_patterns.py
│ ├── debug_strength.py
│ └── debug_classes.py
├── tests/
│ ├── test_negation.py
│ ├── test_sadness.py
│ └── quick_test.py
├── app.py
├── requirements.txt
└── README.md
## 🧠 工作原理
### 1. 数据收集
- 使用大规模数据集,例如:
- GoEmotions(27 种情感)
- 自定义增强数据集
- 约 135 万个样本(各情感类别均衡)
### 2. 文本预处理
- 转换为小写
- 移除 URL 和噪声
- 分词
- 词形还原
- **否定处理(重要)**
## 示例:
"not happy" → sadness (而非 joy)
### 3. 特征工程
- TF-IDF 向量化
- N-grams (1–3)
- 将文本转换为数值向量
### 4. 机器学习模型
- 逻辑回归
- 支持向量机
- 随机森林
- 梯度提升
- 朴素贝叶斯
## 👉 组合方式:
**Voting Classifier (Soft Voting Ensemble)**
### 5. 基于规则的系统
- 关键词匹配
- 正则表达式模式
- 特定情感规则
## 示例:
"I hate this" → anger
"I am not happy" → sadness
### 6. 混合决策引擎
结合:
- ML 预测(概率)
- 基于规则的分数
逻辑:
- 强规则 → 覆盖 ML
- 否则 → 使用 ML 预测
### 7. 输出:
- 最终预测情感
- 置信度分数
- Top-N 情感(可选)
## 📊 模型性能:
| 模型版本 | 准确率 |
|--------------------------|---------|
| 初始模型 | ~50% |
| 改进模型 (10万数据) | ~74% |
| 优化模型 | ~98–99% (小数据集) |
| 内存优化模型 | ~97–98% |
## ⚡ 内存优化:
项目包含一个具有 3 种模式的**内存优化模型**:
- `aggressive` → 低内存,速度更快
- `balanced` → 最佳权衡
- `conservative` → 更高准确率
所使用的技术:
- 减少 TF-IDF 特征
- 批处理
- 稀疏矩阵
- 有限数据集采样
## 🧪 评估指标:
- 准确率
- 精确率
- 召回率
- F1-score
- 混淆矩阵
## ▶️ 运行方法:
### 1. 安装依赖
```
pip install -r requirements.txt
2. Run the app
python app.py
3. Test prediction
python quick_test.py
📌 Example:
from emotion_detector import EmotionDetector
detector = EmotionDetector()
text = "I am feeling very happy today!"
result = detector.predict(text)
print(result)
Output:
{
"emotion": "joy",
"confidence": 0.92
}
🔍 Key Highlights:
✔ Hybrid ML + Rule-Based system
✔ Handles negation (major NLP challenge)
✔ Works on 27 emotion classes
✔ Scalable & optimized
✔ Production-ready design
⚠️ Known Issues:
Dataset imbalance affects rare emotions
Some classes (e.g., awe, relief) have low performance
Augmentation pipeline needs refinement
🚀 Future Improvements
Use BERT / Transformers for better accuracy
Improve rare class prediction
Deploy as API (Flask/FastAPI)
Add real-time UI dashboard
⭐ If you like this project:
Give it a ⭐ on GitHub and share it!
```
标签:Apex, GoEmotions, NLP, Python, TF-IDF, 云计算, 人工智能, 低资源计算, 否定处理, 多分类, 情感分析, 情感计算, 情绪检测, 情绪识别, 数据增强, 文本分类, 文本挖掘, 无后门, 机器学习, 模型优化, 混合模型, 特征提取, 用户模式Hook绕过, 监督学习, 规则引擎, 逆向工具, 集成学习