Skip to content

Case study · Master’s thesis

Which chamber? Classifying 37,000+ Arabic court rulings

My Master’s thesis, done at Juridata: a model that reads a ruling of the Moroccan Court of Cassation in Arabic and predicts which of the court’s 7 chambers it belongs to, so the legal assistant can search the right chamber first.

Degree
Master’s, Mathematical Modeling & Data Science
School
FST Tangier
Host
Juridata (internship)
Year
2026

Results

91.7%
Test accuracy (XGBoost)
0.855
Macro-F1 across the 7 chambers
360×
Faster training with Logistic Regression, at 89.8% accuracy

01

The problem

Juridata’s RAG assistant searched every chamber for every question, so retrieval also pulled back rulings from the wrong area of law.

Knowing the chamber first (civil, criminal, commercial, administrative, social, real estate, or personal status and inheritance) narrows the search and makes the sources more relevant. The thesis compares three classical models for this routing step and asks which one is worth running in production.

02

The data

38,600 rulings from 1976 to 2025, each an OCR’d Arabic text.

  • 778 exact duplicates and 48 missing files removed, then 393 length outliers (under 20 words or over 2,143): 37,381 rulings kept.
  • Strong imbalance: the civil chamber holds 30.4% of the rulings, the real-estate chamber 1.5%, about 21 to 1.
  • 676 rulings listed under several chambers keep the first one.
  • Chronological split, stratified by chamber: the oldest 80% for training (30,070), the newest 20% for testing (7,311), so the model is judged on later rulings.

03

Cleaning legal Arabic

The main contribution of the thesis is the preprocessing, not the model.

  1. 01

    Cut boilerplate. Headers and footers are removed using markers such as «⁧باسم جلالة الملك⁩» and «⁧رئيس الغرفة⁩».

  2. 02

    Normalize. PyArabic removes diacritics and tatweel and unifies the forms of hamza and alef maqsura.

  3. 03

    Keep Arabic only. Latin letters and digits are dropped.

  4. 04

    Remove stopwords. The NLTK Arabic list plus custom lists, 967 terms in all. No stemming, by design.

  5. 05

    Vectorize. TF-IDF on word 1–2-grams with 20,000 features, fitted on the training set only. Character n-grams were tested and scored lower.

04

Leakage and imbalance

A model can score well for the wrong reason: if the chamber is named in the ruling, it learns to read the label.

Masking the first and last 50 tokens dropped the criminal chamber’s F1 by about 0.08, a sign the model leaned on headers. I removed chamber names (including OCR-garbled spellings), headers and footers, judge and clerk names, place names and OCR noise. Macro-F1 on the selection probe barely moved (0.891 → 0.884): the score did not rest on the leak.

For the imbalance, all three models use balanced class weights. Random oversampling tied on the probe; SMOTE was set aside.

05

Results

XGBoost wins on accuracy; Logistic Regression wins on cost.

  • XGBoost: 91.7% accuracy, macro-F1 0.855, about 56 minutes to train.
  • Logistic Regression: 89.8% accuracy, macro-F1 0.829, 9.4 seconds to train, and interpretable: the recommended trade-off.
  • Random Forest: 83.9% accuracy, macro-F1 0.736, about 71 minutes: not recommended.
  • The gaps are significant: bootstrap 95% confidence intervals do not overlap, and McNemar tests give p < 0.001 for every pair.
  • Easiest chambers: administrative and criminal (F1 about 0.96). Hardest: real estate, the smallest class (F1 0.505 with XGBoost).

06

Limits and next steps

TF-IDF only sees words, not meaning, and the headline score comes from a single chronological split.

A 5-fold time-series cross-validation gives a lower and more variable macro-F1 (0.653, std 0.153), the real-estate chamber has few examples, and some OCR noise and near-duplicates remain.

  • Arabic transformer models (AraBERT) in place of TF-IDF
  • Cost-sensitive decision thresholds for the small chambers
  • SHAP or LIME explanations for each prediction
  • The classifier as a router in Juridata’s RAG, then extended to lower courts

07

What I did

The full study, from raw files to conclusions.

  1. 01

    Built the cleaning pipeline for OCR’d Arabic legal text (PyArabic, NLTK, regular expressions)

  2. 02

    Designed a chronological, stratified evaluation

  3. 03

    Found and removed label leakage with ablation tests

  4. 04

    Trained and compared Logistic Regression, Random Forest and XGBoost with class weights

  5. 05

    Tested significance with bootstrap intervals and McNemar tests

08

Stack

  • Python
  • scikit-learn
  • XGBoost
  • TF-IDF
  • NLTK
  • PyArabic
  • Pandas
  • NumPy
  • Matplotlib / Seaborn
  • Tableau

Have a similar problem, or a role where this fits?