Ewe is my first language. It is spoken by millions of people in the Volta Region of Ghana and in southern Togo, and yet almost none of the language technology I use every day works well in it. Keyboards don't predict it, speech tools don't understand it, and large language models treat it as an afterthought.
So for the n-gram section of my NLP coursework at Ashesi, I set myself a narrow, honest goal: teach a computer to judge how natural a sentence of Ewe looks, and to write new Ewe-looking text, using only counting and arithmetic. No NLP libraries, no pretrained models. Just pandas, matplotlib and the Python standard library.
This article walks through what I built, what the numbers say, and, more importantly, the four or five times the model looked great and was actually broken.
Why n-grams, in 2026?
It is fair to ask why anyone would build an n-gram model when transformers exist.
The answer is that Ewe is a low-resource language. There is far less clean, digitised text than for English, and neural models are hungry. An n-gram model learns from a small corpus in seconds on a laptop, and every probability it produces can be traced back to a count you can inspect. That makes it the right first baseline: anything fancier has to beat it to justify itself.
The idea is simple. A language model assigns a probability to a sequence of words. An n-gram model approximates that by only looking at the previous n − 1 words:
P(wᵢ | w₁ … wᵢ₋₁) ≈ P(wᵢ | wᵢ₋ₙ₊₁ … wᵢ₋₁)
A bigram model remembers one word of context, a trigram two, and so on. I built all five orders, from unigram (no memory at all) to 5-gram (four words of memory).
The data
I combined two sources: about 28,600 Ewe sentences from an Ewe–English parallel corpus (only the Ewe side is used) and about 19,150 Ewe transcriptions from the Waxal dataset. That gives 47,765 lines and roughly 1.5 million tokens after cleaning.

Two properties of this data shaped every later decision:
- Most words are rare. 21,794 of the 42,260 distinct word types (51.6%) appear exactly once. These hapax legomena are the central problem of the whole project: a model that has seen a word once cannot know much about it.
- The corpus leans religious. About a fifth of lines begin with what looks like a Bible verse number, and names like Yehowa and Yesu Kristo are common. Results may not carry over to everyday conversational Ewe, and I say that up front rather than burying it.

The pipeline
- Clean. Lowercase everything, keep the Ewe letters
ɔ ɛ ŋ ɖ ƒ, keep. , ! ?as tokens, drop digits and other symbols, and wrap each sentence in<s>and</s>markers so the model learns how sentences start and end. - Split. Shuffle with a fixed seed and divide 80 / 10 / 10 into train, dev and test sets: 37,704 / 4,713 / 4,714 sentences.
- Count. Tally every 1- to 5-gram in the training set.
- Smooth. Use add-k smoothing at the unigram level and recursive linear interpolation for the higher orders, so a context the model has never seen falls back gracefully to a shorter one.
- Tune. Choose
kand one interpolation weight per order on the dev set. - Evaluate. Score the test set exactly once, then generate text by sampling.
The order of steps 2, 5 and 6 matters more than it looks. If you tune on the test set, the final number is no longer an honest estimate of how the model behaves on new text.
Results
The standard score for a language model is perplexity: roughly, how many words the model is "choosing between" at each step. Lower is better.

| Model | Context | Test perplexity |
|---|---|---|
| Unigram | none | 595.9 |
| Bigram | 1 word | 167.3 |
| Trigram | 2 words | 139.2 |
| 4-gram | 3 words | 184.6 |
| 5-gram | 4 words | 299.6 |
Giving the model one or two words of memory makes it dramatically less surprised by real Ewe: perplexity drops from about 596 to 139. You can see what it learned by asking which words it expects next:

At face value the table also says that longer context makes things worse. That turned out to be mostly my fault, which brings me to the part of the project I actually learned the most from.
Five times the model lied to me
1. "Adding memory made the model worse"
My first run produced unigram 794, bigram 690 and trigram 4,800. More context should help, not make the model six times more confused.
The cause was add-k smoothing applied at every order. It adds k × vocabulary size (about 0.1 × 40,000 = 4,000) pretend counts to every denominator. A trigram context usually has only a handful of real observations, so the pretend counts drowned them. Switching the higher orders to interpolation, which blends the trigram estimate with the bigram and unigram estimates, took trigram perplexity from 4,800 to 225.
Lesson: when a result contradicts common sense, suspect the code before the theory.
2. The <UNK> trap: gibberish beats real Ewe
A standard trick is to replace every once-only word with an <UNK> token so the model learns what "unknown" looks like. I did that, and perplexity looked wonderful: trigram around 150.
Then I scored a nonsense sentence by hand, zzz bbb qqq, and it scored better than a real Ewe sentence.
Here's why. Half the vocabulary had become <UNK>, and rare words tend to sit next to other rare words, so <UNK> <UNK> <UNK> had become one of the most common patterns in training. Any gibberish maps to exactly that pattern. I removed the replacement, and unseen words now fall back to the smoothed unigram floor.
Lesson: a good metric is not proof of a good model. Always keep a qualitative test with a known right answer.
3. A bug from mixing two conventions
When I extended the code from trigrams to 5-grams, bigram perplexity suddenly jumped to 1,203. Unigram counts were stored under plain strings and every higher order under tuples, and one lookup was silently mismatching and returning zero.
Lesson: when one variant fails and the others work, compare their data structures first.
4. Punctuation made the score look better than the model was
Keeping . , ! ? as tokens dropped per-token perplexity from about 189.5 to 134.9 at the time. Later I realised most of that came from adding lots of easy-to-predict tokens to the average, not from the model knowing more Ewe. Keeping punctuation is still reasonable, but you cannot compare perplexities across different tokenisations.
5. The tuning grid was too narrow
This is the important one. I searched the interpolation weight λ over 0.5 to 0.95. For the trigram, 4-gram and 5-gram, the winning value was 0.5: the very bottom of the range. When a search returns the edge of its range, the range is wrong.
I re-ran tuning on the dev set only with a wider grid (0.01 to 0.95):
| Order | Original grid: dev perplexity | Wider grid: dev perplexity |
|---|---|---|
| Trigram | 140.9 | 134.5 |
| 4-gram | 188.4 | 132.0 |
| 5-gram | 307.2 | 131.9 |
With a properly fitted weight, longer context helps slightly and then plateaus. The dramatic "4- and 5-grams are terrible" story in the first chart is largely an artefact of my search range, not a fact about Ewe. I've kept the original test numbers above because that is what the notebook currently reports, but they need re-tuning before anyone quotes them.
What I would tell someone starting the same project
- Split first, tune on dev, test once. It's the only way the last number means anything.
- Measure noise before crediting a change. Re-splitting the data with five different seeds moved trigram test perplexity by about 1.2%. A 2% "improvement" from one run is not evidence.
- Keep a hand-made sanity test. A nonsense sentence and a real sentence, side by side, caught a bug that the headline metric hid completely.
- Write down the limitations. My tokenizer splits about 1% of words because Python's
redoes not treat combining marks (as in nasalɔ̃) as word characters; capitalƉis often typed asÐ, which lowercases to the wrong letter; and the data's provenance and licence still need documenting.
What's next
The obvious next steps are re-tuning with the wider grid and reporting final test numbers, fixing the tokenizer, seeding text generation, and trying Kneser-Ney smoothing. The step I care most about is having a native speaker rate a sample of generated sentences, because perplexity alone cannot tell you whether the output sounds like Ewe to an Ewe speaker.
This baseline also feeds into longer-term work. I'm interested in Ewe NLP beyond coursework, and any neural model I build for Ewe now has a clear, reproducible number to beat.
The notebook, the full write-up guide and the script that reproduces the tuning-grid finding are all in the repository.



