Last active
May 16, 2020 07:51
-
-
Save codito/cb31ba37b0a4e5a77dc03c84a3ebc50d to your computer and use it in GitHub Desktop.
Utility to convert DV1-TTSurekh font to devanagari unicode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
#!/usr/bin/env python2 | |
# usage: | |
# mkdir /tmp/trial | |
# git clone https://github.com/sushant354/indic2unicode | |
# sudo pacman -S python2-ply | |
# # copy the below code in /tmp/trial and save as convert.py | |
# cd /tmp/trial | |
# python2 convert.py input.txt output.txt | |
import sys | |
from indic2unicode.fontconv import FontConv | |
from codecs import open | |
# FIXME encoding map below isn't working great. If below works, we can choose | |
# to not depend on indic2unicode | |
# surekh = {"$": "ॐ", "&": "ः", "*": "।", "£Ã": "फ़्र", "+Éä": "ओ", "+Éè": "औ", "+É": "आ", "+": "अ", "<È": "ईं", "<Ç": "ई", "<": "इ", ">": "ऊ", "=": "उ", "@": "ऋ", "A": "ॠ", "Bå": "ऐं", "Bä": "ऐ", "‹ä": "ऐ", "B": "ए", "‹": "ए", "आå": "ओं", "आé": "औं", "C": "क्", "D": "क़्", "E": "क", "F": "क़", "G": "क्र", "H": "क्त", "I": "क्ष्", "J": "ख्", "K": "ख़्", "L": "ख्र्", "M": "ग्", "N": "ग़्", "O": "ग्र्", "P": "घ्", "Q": "घ्र्", "R": "ङ", "S": "च्", "T": "च्र्", "U": "छ", "V": "ज्", "W": "ज़्", "X": "ज्र्", "Y": "ज्ञ्", "Z": "झ्", "[": "झ्र्", "\\": "ञ्", "]": "ट", "^": "ट्ट", "_": "ट्ठ", "`": "ठ", "a": "ठ्ठ", "b": "ड", "c": "ड़", "d": "ड्ड", "e": "ड्ढ", "f": "ढ", "g": "ढ़", "h": "ण्", "i": "त्", "j": "त्र्", "k": "त्त्", "l": "थ्", "m": "थ्र्", "n": "द", "o": "दृ", "p": "द्र", "q": "द्द", "r": "द्ध", "s": "द्म", "t": "द्य", "u": "द्व", "v": "ध्", "w": "ध्र्", "x": "न्", "y": "न्र्", "z": "न्न्", "{": "प्", "|": "प्र्", "}": "फ्", "~": "फ़्", "¡": "फ", "¢": "फ़", "£": "फ्र", "¤": "ब्", "¥": "ब्र्", "¦": "भ्", "§": "भ्र्", "¨": "म्", "©": "म्र्", "ª": "य्", "«": "य्र्", "®": "र", "¯": "रु", "¬": "्य", "°": "रू", "±": "ल्", "²": "ळ्", "³": "ळ", "´": "व्", "µ": "व्र्", "¶": "श्", "•": "श्व्", "·": "श्व्", "¸": "श्र्", "¹": "ष्", "º": "स्", "»": "स्र्", "À": "ह्म", "Á": "ह्य", "¾": "हृ", "¿": "ह्र", "¼": "ह्", "½": "ह", "Â": "्", "Ã": "़", "Ä": "ँ", "Æ": "ं", "Å": "्र", "ä": "े", "è": "ै", "Éä": "ो", "Éè": "ौ", "É": "ा", "Ò": "ी", "Ö": "ु", "×": "ु", "Ù": "ु", "Ú": "ू", "Ý": "ू", "Þ": "ृ", "ß": "ॄ", "्ा": "", "å": "ें", "é": "ैं", "ì": "ॅ", "ð": "", "ñ": "", "ò": "", "ó": "", "ô": "", "õ": "", "ö": "", "÷": "", "ø": "", "ù": "", "ú": "", "û": "", "ü": "", "ý": "", "þ": "", "ाे": "ो", "ाै": "ौ", "आॅ": "ऑ", "ाॅ": "ॉ", "\'": "\"", "रू ": " डिग्री "} | |
# surekh = {"$": "ॐ", "&": "ः", "*": "।", "£Ã": "फ़्र", "+Éä": "ओ", "+Éè": "औ", "+É": "आ", "+": "अ", "<È": "ईं", "<Ç": "ई", "<": "इ", ">": "ऊ", "=": "उ", "@": "ऋ", "A": "ॠ", "Bå": "ऐं", "Bä": "ऐ", "‹ä": "ऐ", "B": "ए", "‹": "ए", "आå": "ओं", "आé": "औं", "C": "क्", "D": "क़्", "E": "क", "F": "क़", "G": "क्र", "H": "क्त", "I": "क्ष्", "J": "ख्", "K": "ख़्", "L": "ख्र्", "M": "ग्", "N": "ग़्", "O": "ग्र्", "P": "घ्", "Q": "घ्र्", "R": "ङ", "S": "च्", "T": "च्र्", "U": "छ", "V": "ज्", "W": "ज़्", "X": "ज्र्", "Y": "ज्ञ्", "Z": "झ्", "[": "झ्र्", "\\": "ञ्", "]": "ट", "^": "ट्ट", "_": "ट्ठ", "`": "ठ", "a": "ठ्ठ", "b": "ड", "c": "ड़", "d": "ड्ड", "e": "ड्ढ", "f": "ढ", "g": "ढ़", "h": "ण्", "i": "त्", "j": "त्र्", "k": "त्त्", "l": "थ्", "m": "थ्र्", "n": "द", "o": "दृ", "p": "द्र", "q": "द्द", "r": "द्ध", "s": "द्म", "t": "द्य", "u": "द्व", "v": "ध्", "w": "ध्र्", "x": "न्", "y": "न्र्", "z": "न्न्", "{": "प्", "|": "प्र्", "}": "फ्", "~": "फ़्", "¡": "फ", "¢": "फ़", "£": "फ्र", "¤": "ब्", "¥": "ब्र्", "¦": "भ्", "§": "भ्र्", "¨": "म्", "©": "म्र्", "ª": "य्", "«": "य्र्", "®": "र", "¯": "रु", "¬": "्य", "°": "रू", "±": "ल्", "²": "ळ्", "³": "ळ", "´": "व्", "µ": "व्र्", "¶": "श्", "•": "श्व्", "·": "श्व्", "¸": "श्र्", "¹": "ष्", "º": "स्", "»": "स्र्", "À": "ह्म", "Á": "ह्य", "¾": "हृ", "¿": "ह्र", "¼": "ह्", "½": "ह", "Â": "्", "Ã": "़", "Ä": "ँ", "Æ": "ं", "Å": "्र", "ä": "े", "è": "ै", "Éä": "ो", "Éè": "ौ", "É": "ा", "Ò": "ी", "Ö": "ु", "×": "ु", "Ù": "ु", "Ú": "ू", "Ý": "ू", "Þ": "ृ", "ß": "ॄ", "्ा": "", "å": "ें", "é": "ैं", "ì": "ॅ", "ð": "", "ñ": "", "ò": "", "ó": "", "ô": "", "õ": "", "ö": "", "÷": "", "ø": "", "ù": "", "ú": "", "û": "", "ü": "", "ý": "", "þ": "", "ाे": "ो", "ाै": "ौ", "आॅ": "ऑ", "ाॅ": "ॉ", "\'": "\"", "रू ": " डिग्री "} | |
def convert(input_file, out_file, char_map=None): | |
fc = FontConv() | |
def _is_ascii(word): | |
# we've a mix of english and devanagari characters, decide which word to | |
# convert based on the weight of english characters in the word :) | |
# default threshold 0.7 means we consider a word to be english of 70% chars | |
# are in ascii range 0-128. | |
count_ascii = sum([1 if ord(c) < 128 else 0 for c in word]) | |
return count_ascii > 0.7 * len(word) | |
def _replace_word(word): | |
return fc.to_unicode('surekh', word) | |
def _replace_line(line): | |
words = line.split(' ') | |
return ' '.join(_replace_word(w) if not _is_ascii(w) else w for w in words) | |
with open(input_file, 'r', encoding='utf-8') as f: | |
lines = f.readlines() | |
with open(out_file, 'w', encoding='utf-8') as of: | |
of.writelines(map(_replace_line, lines)) | |
if __name__ == "__main__": | |
convert(sys.argv[1], sys.argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment