sinhalanorm
Normalize Sinhala text, and convert legacy-font Sinhala (FM-Abhaya, Damindu) to Unicode.
Two Sinhala strings can look identical on screen and still be different bytes.
Text typed with different keyboards, copied from PDFs, or scraped from the web
mixes zero-width joiners in the wrong place, vowel signs typed in the order
they are drawn instead of the order Unicode stores them, and lookalike
pairs such as අ + ා standing in for ආ. Search misses matches,
deduplication fails, and tokenizers split the same word several different
ways.
Older documents are worse: they were typed in legacy fonts like FM-Abhaya or Damindu,
so copying them gives you Y%S ,xld instead of ශ්රී ලංකා.
sinhalanorm fixes both, with zero dependencies.
Installation
pip install sinhalanorm
Quick start
import sinhalanorm
# Kombuva typed before the consonant (visual order) → stored order
sinhalanorm.normalize("ෙකාළඹ") # 'කොළඹ'
# Lookalike pair → the real independent vowel
sinhalanorm.normalize("අාලය") # 'ආලය'
# Legacy FM-Abhaya text → Unicode
sinhalanorm.legacy_to_unicode("Y%S ,xld") # 'ශ්රී ලංකා'
sinhalanorm.legacy_to_unicode("úoHdj") # 'විද්යාව'
sinhalanorm.legacy_to_unicode("rdcH ks,OdÍkag") # 'රාජ්ය නිලධාරීන්ට'
sinhalanorm.legacy_to_unicode("mÑmd,k", font="damindu") # 'පරිපාලන'
sinhalanorm.is_normalized("කොළඹ") # True
Find problems without changing anything
for issue in sinhalanorm.check("ෙකාළඹ අාලය"):
print(issue.start, issue.code, repr(issue.text), issue.fixable)
# 0 kombuva_order 'ෙක' True
# 6 vowel_composition 'අා' True
check() reports positions in your string, so you can highlight them in an
editor or a data-quality report. Some problems (such as a vowel sign with no
consonant before it) are reported as fixable=False, because the right fix is
ambiguous.
Command line
sinhalanorm normalize corpus.txt > clean.txt
sinhalanorm legacy old_document.txt > unicode.txt
sinhalanorm legacy --font damindu circular.txt > unicode.txt
sinhalanorm check article.txt # exits 1 if any problems are found
All three commands read from stdin when no file is given.
What normalize() fixes
Rules run in this order. Disable any of them with
normalize(text, skip=["rule_code"]).
| Code | Fixes | Example |
|---|---|---|
invisible |
BOMs and soft hyphens; zero-width spaces / non-joiners touching Sinhala | සිංහල → සිංහල |
zwj_order |
ZWJ placed before al-lakuna in yansaya, rakaransaya, repaya | ක්ය → ක්ය |
zwj_repeat |
Repeated ZWJs | ක්ය → ක්ය |
zwj_stray |
ZWJs next to Sinhala that don't join al-lakuna to a consonant | කම → කම |
double_kombuva |
Two kombuvas instead of kombu deka | ෙෙ → ෛ |
subjoined_order |
Vowel sign typed before a yansaya / rakaransaya | ති්ර → ත්රි |
kombuva_order |
Kombuva typed before its consonant | ෙකාළඹ → කොළඹ |
sign_order |
Anusvara / visarga typed before a vowel sign | කංා → කාං |
repeated_sign |
A vowel sign or al-lakuna typed twice | කාා → කා |
| (NFC) | Two-part vowels stored as separate code points | ක ෙ ා → කො |
vowel_composition |
Lookalike pairs instead of independent vowels | අා → ආ, එ් → ඒ, ඔ් → ඕ |
The output is always NFC. Non-Sinhala text passes through unchanged, including ZWJs in emoji sequences.
Legacy fonts
legacy_to_unicode() supports two legacy encodings:
font= |
Fonts | Typical sources |
|---|---|---|
"fm-abhaya" (default) |
FMAbhaya, FMAbhaya x, FMAbabld (bold) | Gazettes, NIE and school textbooks, 2000s-2010s |
"damindu" (experimental) |
Damindu (Wijayananda Jayaweera) | Government circulars, 1990s to early 2000s |
Both fonts use the Wijesekara typewriter layout for ASCII, so l is ක in
either. Their precomposed glyphs are different, though. The same bytes
mÑmd,k read පරිපාලන in Damindu but පචිපාලන in FM-Abhaya, so pick the
font the document was set in. In a PDF, the font name is on each text span
(PyMuPDF: page.get_text("dict")). Embedded fonts often have generated names
like MSTT31c40b; if words with accented letters come out wrong under one
encoding, try the other.
The converter maps each glyph to Unicode, moves each kombuva from its visual
position to its stored position, and then runs normalize(). It reproduces
what the font draws, including the author's typos. Damindu glyphs with too
little evidence to identify are left unchanged rather than guessed, so they
stay visible.
Accuracy on real documents (share of converted words found in Sinhala Wikipedia; see the evaluation):
| Encoding | Words tested | In vocabulary | Malformed output |
|---|---|---|---|
| FM-Abhaya | 150,580 (6 PDFs) | 98.2% | 0.02% |
| Damindu | 1,774 (5 circulars) | 95.5% | 0% |
A manual review found nearly all of the remaining words to be correct words that Wikipedia lacks, or typos in the source. For comparison, an existing FM-Abhaya converter produces malformed Unicode in 5% of tokens. The Damindu figure comes from far less text, all of which was also used to build the table, so Damindu support is experimental. If you have Damindu documents, please share them in an issue.
Pass it only the text that was set in the legacy font. Every Latin letter is treated as a Sinhala glyph, so English words get converted too. If a word converts wrongly, please open an issue with the input, the font and the expected output.
API reference
| Function / object | Description |
|---|---|
normalize(text, *, skip=()) -> str |
Return normalized NFC text |
is_normalized(text) -> bool |
normalize(text) == text |
check(text) -> list[Issue] |
Report problems with positions; does not modify |
legacy_to_unicode(text, font="fm-abhaya") -> str |
Convert legacy-font text ("fm-abhaya" or "damindu") |
Issue |
Frozen dataclass: code, start, end, text, message, fixable |
ALL_RULES, RULE_CODES |
The rule table and the valid skip codes |
FONTS |
Supported legacy font encodings |
The package ships type hints (py.typed).
Development
git clone https://github.com/RavinduPabasara/sinhalanorm
cd sinhalanorm
make install-dev
make test
License
MIT. See LICENSE.
Author
Ravindu Pabasara Karunarathna, also the author of sinhaladate, slnic and tokscope.
Metadata
Release files for sinhalanorm 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sinhalanorm-0.1.0.tar.gz | 22.4 kB | Without provenance | Details for sinhalanorm-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sinhalanorm-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for sinhalanorm-0.1.0-py3-none-any.whl |
Total release size: 39.6 kB
Release files / sinhalanorm-0.1.0.tar.gz
| Download URL | sinhalanorm-0.1.0.tar.gz |
|---|---|
| Size | 22.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d0eca358f4cd36a1cdb4a283f3723ef3d5bdcbcd16c41f879263ac3d641d0ad0
|
|
BLAKE2b-256 checksum How to use checksums |
cb47a1e8512c0c254183fee2940af72139e4b676b6a1870874d15d7fba12edf3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.2
|
Release files / sinhalanorm-0.1.0-py3-none-any.whl
| Download URL | sinhalanorm-0.1.0-py3-none-any.whl |
|---|---|
| Size | 17.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2ee1003778c90ec3930b05990e76e3baaf8d3f2f4fac8737ff57a5070affa01b
|
|
BLAKE2b-256 checksum How to use checksums |
d58b6b11462b1f99f951b50bf7463613cfefcb6824b03609b1fcd57d723fb5db
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.2
|
Security for sinhalanorm 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.