Sinhala is an abugida. A consonant letter carries an inherent vowel, vowels written after a consonant are signs that attach to it, and a special mark kills the inherent vowel. Typing it from a Latin keyboard ("Singlish") means turning ammaa into අම්මා. I wrote a converter for this as part of Singlish to Sinhala, and the interesting part is how little machinery it needs: one map, a longest-match scan, and a couple of flags of state.

This post walks through that design. Nothing here depends on the site; the same approach works for other abugidas.

The token table

There are two tables: 16 vowels and 44 consonant keys. Each vowel has two forms, because Sinhala writes a vowel differently at the start of a word than after a consonant:

{ latin: "aa", independent: "ආ", sign: "ා", note: "Long a." }, { latin: "ae", independent: "ඇ", sign: "ැ", note: "As in the English word cat." }, { latin: "Ru", independent: "ඍ", sign: "ෘ", note: "Vocalic r, in words from Sanskrit. A plain ru is රු." }, 

Short a is a special case: its sign is the empty string. After a consonant, the inherent vowel is already there, so "ka" is just ක.

All keys go into one Map, and the longest key is computed from the map itself, so adding a key can never leave the scanner out of date:

const MAX_KEY = Math.max(...Array.from(TOKENS.keys()).map((k) => k.length)); 

Today that is three letters (aae, chh, Ruu).

Longest match first

Keys overlap. a is a prefix of aa, which is a prefix of aae. k is a prefix of kh, n of nG. A scan that takes the first match would split aae into a + a + e. So at each position the code tries the longest candidate and shrinks:

for (let len = Math.min(MAX_KEY, chars.length - i); len >= 1; len--) { const key = chars.slice(i, i + len).join(""); const token = TOKENS.get(folded ? key.toLowerCase() : key); if (token) return { token, length: len }; } 

Case matters, which is how the scheme gets the retroflex letters without extra punctuation: t is dental ත, capital T is retroflex ට; n is න, N is ණ. The prenasalised letters use a capital inside the key: nG is ඟ, mB is ඹ. If a capital letter starts no key, a second pass reads it in lowercase, so a sentence-initial Amma still begins with a plain a:

"Amma" => අම්ම 

Vowels after consonants, and al-lakuna

The converter's main piece of state is bare, meaning "the last thing written was a consonant that has no vowel yet". It decides everything else:

  • A vowel arrives while bare is set: write its sign. Otherwise write the independent letter.
  • A consonant arrives while bare is set: first write al-lakuna (U+0DCA), which cancels the previous consonant's inherent vowel.
  • Anything else arrives (space, digit, end of text): close the bare consonant with al-lakuna, then pass the character through.

The consequence is that consonants are written optimistically as the bare letter and the vowel is resolved later, one token behind. Real outputs:

"kka" => ක්ක d9a dca d9a "nda" => න්ද db1 dca daf "k" => ක් "amma" => අම්ම 

Joiners: yansaya, rakaransaya, repaya

Three Sinhala conjuncts need a zero width joiner (U+200D) after al-lakuna so the renderer draws a combined shape instead of a visible al-lakuna. The code covers them in one line, because all three are "al-lakuna, then ZWJ, then the next letter":

const joined = t.ch === "ර" || t.ch === "ය" || bareLetter === "ර"; out.push(joined ? HAL + ZWJ : HAL); 

If the incoming consonant is ර (rakaransaya) or ය (yansaya), or the pending one was ර (repaya, as in "dharma"), the joiner is added. The code needs bareLetter only for that last case. The tests check these against Unicode 13.2, Table 13-3, and write the expected strings as hex code points, so a mistyped Sinhala letter cannot hide in both the code and the test.

"kra" => ක්‍ර (…dca 200d dbb) "dya" => ද්‍ය "karma" => කර්‍ම "shrii laxkaa" => ශ්‍රී ලංකා 

The ZWJ is invisible, so it is easy to lose when copying text around. That is the reason to test with code points rather than by eye.

The ambiguous spellings

This is where a transliterator earns its keep, because Latin spelling underdetermines Sinhala. The code chooses, and the choices are documented rather than hidden.

Long vowels need two letters. The scheme treats a as the short vowel, so amma is අම්ම and ammaa is අම්මා. The header comment is honest about it: "The tool cannot guess which words end in a long vowel." Likewise ayubowan gives අයුබොවන් (short o), and aayuboowan gives the intended ආයුබෝවන්.

th is not ත. In casual Singlish people write thaththa for "father". In this scheme th is the aspirated ථ, and dental ත is plain t:

"thaththa" => ථථ්ථ "taththa" => තථ්ථ "tatta" => තත්ත 

The first two are wrong for the word; only tatta gives the dental letters. In this scheme the digraph th goes to the aspirate, since Sinhala has both letters and Latin has only one t. A tool that guessed from a dictionary would be a different project.

Prenasalised versus a plain cluster. Sinhala has single letters like ඟ, ඬ, ඳ, ඹ that are a nasal fused with a stop. Spelled as a cluster, the same sounds are n + g with al-lakuna. The scheme gives each its own key, and lets you pick:

"nGa" => ඟ "nga" => න්ග "n\Ga" => න්ග "nda" => න්ද "Nda" => ඳ "mba" => ම්බ "mBa" => ඹ 

Because the longest-match scanner would read nG as one key, there is an explicit separator: a backslash is a token of kind break that writes nothing, so n\G stops the two letters joining.

Sibilants. sh is ශ, S is ෂ, s is ස. puShpa and puSpa both give පුෂ්ප, while a lazy pushpa gives පුශ්ප, which is a different spelling. Again, the table makes the choice; it does not guess.

Context-dependent marks. x is anusvara (ං), so laxkaa is ලංකා, and it keeps the consonant's own a rather than adding al-lakuna. H is visarga (ඃ), but only after a letter: duHkha gives දුඃඛ while a leading Hari gives හරි. That is the one place the scanner consults a second flag, inWord, to turn a match into a non-match.

What it cannot tell you

It is deterministic and has no dictionary. It will not fix a missing long vowel, will not know that a word is Sanskrit-derived and wants ඍ, and does not check spelling. It also only converts one way. The output is Unicode, so how it looks depends on the reader's font and renderer, especially for the ZWJ conjuncts. The tool also caps input at 50,000 characters and ignores the rest.

Testing it

The tests I trust most use expected values worked out by hand and typed as code points, plus the rules table, whose worked examples are run as tests. If you write something similar for another script, start there: the bugs in Indic text are almost always a missing joiner or a wrong sign, and neither is visible in an editor.

If you have written a transliterator for another abugida, did you resolve ambiguity like this with explicit keys, or with a dictionary, and what broke first?

This article was written with the help of AI. The code, numbers and sources come from my own project and were checked against it before publishing.