The situation
Open Barlaman makes Moroccan parliamentary activity easier to explore. The questions members of parliament ask are public, but they’re in Arabic, spread across government sites and spreadsheets, and often available only as PDFs. None of them say what they’re about. The ministry they’re addressed to is a weak hint at best.
I wanted every question to answer three things: what subject, what problem, and what the member is asking for, plus which places it mentions. That meant reading 72,000 documents.
Step 1: getting clean text
Arabic PDFs are tricky. Some have a text layer that looks fine but breaks words or scrambles the reading order. Others are scans with no text at all. Sending everything to OCR would be slow and expensive. Trusting every text layer would give bad input.
So the pipeline routes each document:
- Extract the text with two tools, PyMuPDF and Poppler.
- If the two results are at least 85% similar and the page counts match, keep the native text.
- Otherwise, render the pages as images and transcribe them with an LLM.
To pick the OCR model, I built a 100-document benchmark and compared Tesseract, DeepSeek, Qwen2.5-VL (running locally with Ollama) and Gemini, reviewing outputs side by side on an HTML page. Tesseract’s quality scores looked good, but reading the output showed it dropped text and added Latin noise. Gemini 2.5 Flash-Lite won.
Cleaner Arabic text from the open-data spreadsheets cut the number of documents that needed OCR at all.
Step 2: a taxonomy that says something
Before choosing a model, I fixed the categories. The taxonomy separates the subject (for example healthcare), the problem (for example a staff shortage) and the request (for example more resources). Fixed codes mean every answer can be checked, and the results can be counted and mapped.
Step 3: picking a model by its failures
I ran about six model families through OpenRouter on the same sample and looked for failures, not a score: truncated answers, invented category codes, missing requests, details that weren’t in the source. One model wrapped its JSON in Markdown fences, which broke the parser.
Step 4: cutting the output 12×
Nested JSON is expensive. Every brace, quote and repeated key costs output tokens, and output tokens cost the most. I replaced it with a compact format of labelled lines:
SUMMARY: <one sentence>
SCOPE: <code>
TOPICS: <code>, <code>
PROBLEMS: <code>
REQUESTS: <code>
POPULATION_CATEGORIES: <code>
PLACES: <name>, <name>
PROGRAMS_ENTITIES: <name>
Output dropped from about 7,500 tokens to about 600 per 10 answers. Python parses the lines and rejects any code that isn’t in the taxonomy.
Step 5: batch and resumable
The classification ran through Google’s Gemini Batch API, in JSONL files of 2,000 questions. The pipeline tracks every file through submission, download and import, so an interrupted run picks up where it stopped.
OCR and classification are separate stages, and each stores its model, input hash and token usage. Changing the taxonomy means re-running classification only, not OCR.
Step 6: putting questions on a map
The places a question mentions only matter if you can show them. No open dataset had Morocco’s current commune boundaries, so I found the vector tiles behind the 2024 census dashboard and merged them into GeoJSON for 12 regions, 75 provinces and 1,540 communes. Place names extracted from the Arabic text are matched to those boundaries, preferring the longest complete name and checking the province of each mention.
The result
All 72,039 questions were classified for about $38, roughly $0.53 per thousand. The results power topic exploration and drill-down maps on Open Barlaman, and every question links back to its source.
If you have a pile of documents
Most of the cost and quality is decided before the model runs: clean input text, a fixed set of categories, and an output format that’s cheap to generate and easy to validate. After that, batch processing makes the cost predictable.