DEV Community

Yogeshwaran Tech Tools
Yogeshwaran Tech Tools

Posted on

Convert SunTommy (Bamini-style) Tamil text to Unicode with Python

Many older Tamil Word files and print documents use legacy fonts like SunTommy or Bamini. The text looks like jkpo; until you apply the font, and it cannot be searched, copied into Google Docs or shown on the web. Converting it to Unicode fixes that. Below is a small Python function that does the conversion for SunTommy key codes, plus how I tested it.

The idea

Legacy Tamil fonts map Latin keys to glyphs. A few rules matter:

  • Vowel signs ெ ே ை are typed before the consonant (nf is கெ), but in Unicode they come after (க + ெ).
  • ொ ோ ௌ combine a left sign and a right sign (nfh is கொ).
  • கு, சு, டு and similar have special glyphs (F, R, L), and ூ forms have their own codes.

The code

# sun_to_unicode.py
REV_CONS = dict(f='க',q='ங',r='ச',Q='ஞ',l='ட',z='ண',j='த',e='ந',g='ப',k='ம',a='ய',u='ர',y='ல',t='வ',o='ழ',s='ள',w='ற',d='ன')
REV_CONS.update({'[':'ஜ','\\':'ஷ',']':'ஸ','`':'ஹ'})
REV_VOW = {'m':'அ','M':'ஆ',',':'இ','<':'ஈ','c':'உ','C':'ஊ','v':'எ','V':'ஏ','I':'ஐ','x':'ஒ','X':'ஓ'}
REV_U = dict(F='க',R='ச',L='ட',Z='ண',J='த',E='ந',G='ப',K='ம',A='ய',U='ர',Y='ல',T='வ',O='ழ',S='ள',W='ற',D='ன')
REV_UU = {'$':'க','#':'ச','%':'ம','&':'ர','*':'ழ','@':'ள','^':'ட'}
SUFFIX = {';':'்','h':'ா','p':'ி','P':'ீ','{':'ு','+':'ூ'}
KEEP = set('0123456789 .:/-()\n\r\t')
def sun_to_unicode(s):
    out, i = [], 0
    while i < len(s):
        c, n = s[i], s[i+1] if i+1 < len(s) else ''
        if c in 'nNi' and n in REV_CONS:
            base, n2 = REV_CONS[n], s[i+2] if i+2 < len(s) else ''
            if c == 'i': out.append(base + 'ை'); i += 2
            elif n2 == 'h': out.append(base + ('ொ' if c == 'n' else 'ோ')); i += 3
            elif c == 'n' and n2 == 's': out.append(base + 'ௌ'); i += 3
            else: out.append(base + ('ெ' if c == 'n' else 'ே')); i += 2
            continue
        if c == 'b': out.append('டி')
        elif c == 'B': out.append('டீ')
        elif c == 'H': out.append('ர்')
        elif c == '=': out.append('ஸ்ரீ')
        elif c == '~': out.append('க்ஷ')
        elif c in REV_UU: out.append(REV_UU[c] + 'ூ')
        elif c in REV_U:
            if n == '+': out.append(REV_U[c] + 'ூ'); i += 1
            else: out.append(REV_U[c] + 'ு')
        elif c in REV_CONS and n in SUFFIX: out.append(REV_CONS[c] + SUFFIX[n]); i += 1
        elif c in REV_CONS: out.append(REV_CONS[c])
        elif c in REV_VOW: out.append(REV_VOW[c])
        elif c == '>': out.append(',')
        else: out.append(c)
        i += 1
    return ''.join(out)
Enter fullscreen mode Exit fullscreen mode

Use it on a file

# convert_file.py
import sys
from sun_to_unicode import sun_to_unicode

src, dst = sys.argv[1], sys.argv[2]
text = open(src, encoding="utf-8").read()
open(dst, "w", encoding="utf-8").write(sun_to_unicode(text))
Enter fullscreen mode Exit fullscreen mode
python convert_file.py old.txt new.txt
Enter fullscreen mode Exit fullscreen mode

Quick check

print(sun_to_unicode("Njdp khtl;lk;> Nghbehaf;fD+H tl;lk;"))
# தேனி மாவட்டம், போடிநாயக்கனூர் வட்டம்
Enter fullscreen mode Exit fullscreen mode

How I tested it

I generated every consonant with every vowel sign (about 280 combinations), converted Unicode to SunTommy with a forward converter, then converted back with this function and compared. All matched.

Limits you should know

  • This is written for SunTommy key codes. Bamini is similar but not identical, so check a few words before trusting a whole document.
  • Paste only the Tamil part of a document. English typed in Arial is already readable and would be changed wrongly.
  • The letters ஃ and ஔ are not mapped yet.
  • Always proofread names and rare letters after converting.

If you prefer a browser tool

I made a free browser version that runs offline in your browser (nothing is uploaded): https://luxury-malabi-8a7447.netlify.app/to-unicode.html and the opposite direction, Unicode to SunTommy: https://luxury-malabi-8a7447.netlify.app/sun-tommy.html

Written by Yogeshwaran Tech Tools. Corrections welcome.

Top comments (0)