Một hệ thống phân loại cảm xúc tiếng Việt (Sentiment Classification) hoàn chỉnh sử dụng Machine Learning.
Dự án cung cấp một pipeline hoàn chỉnh để:
- Nạp và xử lý dữ liệu tiếng Việt từ CSV files
- Chuẩn hóa và làm sạch text
- Trích chọn đặc trưng (Bag of Words, TF-IDF, PhoBERT)
- Huấn luyện các mô hình phân loại (Naive Bayes, SVM , MLP )
- Đánh giá và so sánh hiệu suất mô hình
- Dự đoán cảm xúc cho text mới (Positive, Neutral, Negative)
Xem báo cáo chi tiết tại: docs.pdf
Hệ thống này phân loại cảm xúc tiếng Việt thành 3 loại:
- POS (Positive) - Tích cực
- NEU (Neutral) - Trung lập
- NEG (Negative) - Tiêu cực
classification of sentiment/
├── data/ Dữ liệu
│ ├── labeled_data/ CSV files với nhãn
├── src/ Source code chính
│ ├── __init__.py Package initialization
│ ├── config.py Cấu hình toàn bộ dự án
│ ├── data_loader.py Nạp và chuẩn bị dữ liệu
│ ├── preprocessor.py Tiền xử lý text
│ ├── feature_extractor.py Trích chọn đặc trưng
│ ├── model_trainer.py Huấn luyện mô hình
│ ├── evaluator.py Đánh giá và visualization
│ ├── train.py Script huấn luyện chính
│ └── predict.py Script dự đoán
├── models/ Thư mục lưu mô hình
├── outputs/ Kết quả, hình vẽ, logs
├── dictionary/ Từ điển tiếng Việt
├── notebook/ Jupyter notebooks
├── app.py Streamlit web application
├── requirements.txt Python dependencies
└── README.md
- Clone hoặc Download dự án:
cd "classification of sentiment"- Cài đặt dependencies:
pip install -r requirements.txt- Download từ điển tiếng Việt (tùy chọn):
cd dictionary
wget https://raw.githubusercontent.com/undertheseanlp/dictionary/master/dictionaries/tudientv/words.txt
cd ..Đặt các file CSV vào thư mục data/labeled_data/. Mỗi file cần có các cột:
- review: Text bản gốc
- corrected_reviews: Text đã sửa lỗi chính tả
- label: Nhãn sentiment (NEG, NEU, POS)
Ví dụ:
review,corrected_reviews,label
"Sản phẩm rất tốt!","Sản phẩm rất tốt!",POS
"Chất lượng kém","Chất lượng kém",NEG
"Bình thường","Bình thường",NEU
Huấn luyện cơ bản:
python -m src.train \
--feature-method tfidf \
--model-type svm \
--save-modelsHuấn luyện với các tùy chọn:
python -m src.train \
--feature-method tfidf \
--model-type svm \
--handle-imbalance \
--use-dictionary \
--tune-hyperparams \
--save-modelsCác tùy chọn huấn luyện:
- --feature-method: Phương pháp trích đặc trưng (bow, tfidf, phobert)
- --model-type: Loại mô hình (nb, svm)
- --handle-imbalance: Xử lý class imbalance với SMOTE
- --use-dictionary: Dùng từ điển tiếng Việt
- --tune-hyperparams: Tìm kiếm hyperparameters tối ưu
- --save-models: Lưu mô hình đã huấn luyện
Dự đoán từ một text:
python -m src.predict \
"Sản phẩm này rất tuyệt vời!" \
--feature-method tfidf \
--model-type svm \
--probabilitiesDự đoán từ file CSV:
python -m src.predict \
--input-file data/test.csv \
--output-file predictions.csv \
--text-column corrected_reviews \
--feature-method tfidf \
--model-type svmChế độ interactive (nhập text tương tác):
python -m src.predict \
--feature-method tfidf \
--model-type svm \
--probabilitiesSau đó nhập các text để dự đoán:
Enter text: Sản phẩm tốt, giao nhanh
Prediction: POS
Probabilities:
POS: 0.9254
NEU: 0.0632
NEG: 0.0114
Chạy ứng dụng web Streamlit:
streamlit run app.pyTruy cập ứng dụng tại: http://localhost:8501
Streamlit cung cấp:
- Dự đoán từng text đơn lẻ
- Xử lý hàng loạt từ file CSV
- Hiển thị xác suất dự đoán
- Giao diện web thân thiện
- Thống kê và visualization
Tùy chọn trong sidebar:
- Feature Method: Chọn phương pháp trích đặc trưng (TF-IDF, BoW, PhoBERT)
- Model Type: Chọn loại mô hình (SVM, Naive Bayes)
- Use Dictionary: Sử dụng từ điển tiếng Việt
- Show Probabilities: Hiển thị xác suất dự đoán
-
Chuẩn Hóa Text (Normalization)
- Xóa HTML tags
- Xóa URLs
- Xóa ký tự đặc biệt
- Xóa số
- Xóa space thừa
-
Tokenization
- Sử dụng VnCoreNLP để phân tách từ
- Chuyển thành chữ thường
- Xóa ký tự điều khiển
-
Lọc Từ Điển (Tùy Chọn)
- Giữ lại chỉ các từ trong từ điển tiếng Việt
- Loại bỏ từ không quen
-
Xóa Stopwords Và Units
- Xóa các từ thông dụng (là, của, và, ...)
- Xóa các đơn vị (km, kg, đô la, ...)
-
Trích Chọn Đặc Trưng
- Bag of Words (BoW): Đếm tần số từ
- TF-IDF: Trọng số theo tần số-nghịch đảo tài liệu
- PhoBERT: Embedding sâu từ mô hình pre-trained
- Ưu điểm: Nhanh, đơn giản, thích hợp cho BoW/TF-IDF
- Nhược điểm: Hiệu suất thấp hơn SVM
- Ưu điểm: Hiệu suất cao, linh hoạt (nhiều kernel)
- Nhược điểm: Chậm hơn Naive Bayes
- SMOTE: Tạo mẫu nhân tạo cho class thiểu số
- Random Undersampling: Giảm mẫu class đa số
Sau khi huấn luyện, bạn sẽ nhận được:
- confusion_matrix_*.pdf: Ma trận nhầm lẫn
- classification_report_*.pdf: Báo cáo phân loại
- roc_curves_*.pdf: Đường cong ROC
- model_comparison.pdf: So sánh các mô hình
- training.log: Log chi tiết quá trình huấn luyện
- Các mô hình đã lưu (pickle format)
- Các vectorizer đã lưu (pickle format)
Lỗi: "Module not found"
pip install -r requirements.txtLỗi: "VnCoreNLP not found"
python -c "import py_vncorenlp; py_vncorenlp.download_model('./')"Lỗi: "CUDA out of memory" (khi dùng PhoBERT)
extractor.get_phobert_embeddings(texts, batch_size=16)Các kết quả điển hình trên dataset Tiếng Việt:
| Method | Model | Accuracy | F1-Score | Training Time |
|---|---|---|---|---|
| BoW | Naive Bayes | 78% | 0.77 | 2s |
| BoW | SVM | 81% | 0.80 | 5s |
| TF-IDF | Naive Bayes | 80% | 0.79 | 2s |
| TF-IDF | SVM | 84% | 0.83 | 8s |
| PhoBERT | - | 88% | 0.87 | 120s |
Lưu ý: Kết quả có thể khác tùy theo dataset
- py-vncorenlp: https://github.com/undertheseanlp/py-vncorenlp
- VnCoreNLP: https://github.com/undertheseanlp/VnCoreNLP
- PhoBERT: https://github.com/VinAIResearch/PhoBERT
- scikit-learn: https://scikit-learn.org/