# Darija N-gram Language Model π²π¦
A character-level n-gram language model trained on Moroccan Darija (Arabic dialect) text data. Built from scratch using pure Python with no external ML libraries.
---
## Project Structure
```
darija-ngram-lm/
βββ prepare_data.py # Cleans and combines raw text sources into corpus
βββ train.py # Trains the n-gram model (memory-safe streaming)
βββ evaluate.py # Evaluates model quality using perplexity
βββ requirements.txt # No external dependencies needed
βββ src/
β βββ preprocessing.py # Text cleaning utilities
β βββ ngram_model.py # N-gram model core logic
β βββ evaluation.py # Evaluation helpers
βββ data/ # (not included β see Data Sources below)
```
## How It Works
This project builds a trigram character-level language model over Moroccan Darija text:
### 1. prepare_data.py β Data Preparation
- Walks through all raw .txt source files in named subfolders
- Cleans text: keeps only Arabic script characters (U+0600βU+06FF)
- Removes duplicates using MD5 hashing
- Writes a unified data/corpus.txt
### 2. train.py β Model Training
- Reads the corpus in 4KB chunks (memory-safe streaming)
- Counts character trigrams (sequences of 3 characters)
- Periodically prunes low-frequency n-grams to prevent memory overflow
- Saves the trained model as models/model.json
### 3. evaluate.py β Model Evaluation
- Loads the trained model
- Computes perplexity on a Darija test string
- Uses Laplace smoothing for unseen character sequences
---
## Setup
No installation needed. Just make sure you have Python 3.8+:
python --version
All libraries used are part of Python standard library: json, math, os, re, collections, hashlib.
---
## How to Run
Step 1 β Prepare the corpus
python darija-ngram-lm/prepare_data.py
Step 2 β Train the model
python darija-ngram-lm/train.py
Step 3 β Evaluate the model
python darija-ngram-lm/evaluate.py
---
## Results
| Metric β¦