Skip to content

Repository files navigation

Vietnamese Sentiment Classification

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.

Báo cáo Dự Án

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

Mô Tả Dự Án

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

Cấu Trúc Dự Án

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                     

Cài Đặt

  1. Clone hoặc Download dự án:
cd "classification of sentiment"
  1. Cài đặt dependencies:
pip install -r requirements.txt
  1. 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 ..

Hướng Dẫn Sử Dụng

Bước 1: Chuẩn Bị Dữ Liệu

Đặ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

Bước 2: Huấn Luyện Mô Hình

Huấn luyện cơ bản:

python -m src.train \
    --feature-method tfidf \
    --model-type svm \
    --save-models

Huấ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-models

Cá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

Bước 3: Dự Đoán Cảm Xúc

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 \
    --probabilities

Dự đ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 svm

Chế độ interactive (nhập text tương tác):

python -m src.predict \
    --feature-method tfidf \
    --model-type svm \
    --probabilities

Sau đó 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

Bước 4: Chạy Streamlit Web App

Chạy ứng dụng web Streamlit:

streamlit run app.py

Truy 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

Các Bước Xử Lý Dữ Liệu

  1. 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
  2. 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
  3. 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
  4. 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, ...)
  5. 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

Các Mô Hình Được Hỗ Trợ

Naive Bayes (MultinomialNB)

  • Ư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

Support Vector Machine (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

Xử Lý Class Imbalance

  • SMOTE: Tạo mẫu nhân tạo cho class thiểu số
  • Random Undersampling: Giảm mẫu class đa số

Kết Quả Đầu Ra

Sau khi huấn luyện, bạn sẽ nhận được:

Thư Mục outputs/

  • 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

Thư Mục models/

  • Các mô hình đã lưu (pickle format)
  • Các vectorizer đã lưu (pickle format)

Xử Lý Lỗi

Lỗi: "Module not found"

pip install -r requirements.txt

Lỗ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)

Benchmark Hiệu Suất

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

Tài Liệu Tham Khảo

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages