# Swahili Sentiment Analysis using Transformer Models
> **COS 760 – Natural Language Processing | University of Pretoria**
A comparative study of classical machine learning and transformer-based approaches for sentiment analysis of Swahili tweets, using the AfriSenti dataset.
---
## Table of Contents
- Project Overview
- Research Questions
- Results Summary
- Repository Structure
- Getting Started
- Prerequisites
- Installation
- Data Setup
- Running the Pipeline
- Step 1: Preprocessing
- Step 2: Baseline Models
- Step 3: Transformer Models
- Step 4: Data Augmentation (optional)
- Step 5: Evaluation & Comparison
- Notebooks
- Testing
- Key Findings
- Responsible NLP
- References
---
## Project Overview
Sentiment analysis for African languages remains severely underexplored in NLP research. This project targets **Swahili** — spoken by ~200 million people across East Africa — and evaluates:
| Model | Type |
|---|---|
| Naive Bayes (TF-IDF) | Classical baseline |
| Logistic Regression (TF-IDF) | Classical baseline |
| Linear SVM (TF-IDF) | Classical baseline |
| XLM-RoBERTa (fine-tuned) | Transformer |
| AfriBERTa (fine-tuned) | Transformer |
We also examine the effect of SentencePiece subword tokenisation and back-translation data augmentation.
---
## Research Questions
1. **RQ1** — How do transformer-based models compare to classical baselines for Swahili sentiment analysis?
2. **RQ2** — What is the impact of subword tokenisation on model performance?
3. **RQ3** — Can back-translation augmentation improve classification results?
4. **RQ4** — What systematic error patterns exist, and how do they relate to code-switching?
---
## Results Summary
| Model | Accuracy | F1 (Weighted) | Precision | Recall |
|---|---|---|---|---|
| Naive Bayes | 0.5604 | 0.5014 | 0.4705 | 0.5604 |
| Logistic Regression | 0.6154 | 0.5159 | 0.5608 | 0.6154 |
| Linear SVM | 0.5714 | 0.5142 | 0.4802 | 0.5714 |
| XLM-RoBERTa | 0.5934 | 0.4420 | 0.3521 | 0.5934 |
| AfriBERTa | 0.6 …