forked from knadh/ml2en
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ml2en.py
134 lines (103 loc) · 4.47 KB
/
ml2en.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Python 3 script
'''
(Phonemic) Romanization of Malayalam script
http://nadh.in/code/ml2en
This algorithm transliterates Malayalam script to Roman characters ('Manglish')
Some heuristics try to retain a certain level phonemic fairness
This work is licensed under GPL v2
___________________
Kailash Nadh, 2012
http://nadh.in
'''
import re
class ml2en:
__vowels = {
"അ": "a", "ആ": "aa", "ഇ": "i", "ഈ": "ee", "ഉ": "u", "ഊ": "oo", "ഋ": "ru",
"എ": "e", "ഏ": "e", "ഐ": "ai", "ഒ": "o", "ഓ": "o", "ഔ": "au"
}
__compounds = {
"ക്ക": "kk", "ഗ്ഗ": "gg", "ങ്ങ": "ng",
"ക്ക": "kk", "ച്ച": "cch", "ജ്ജ": "jj", "ഞ്ഞ": "nj",
"ട്ട": "tt", "ണ്ണ": "nn",
"ത്ത": "tth", "ദ്ദ": "ddh", "ദ്ധ": "ddh", "ന്ന": "nn",
"ന്ത": "nth", "ങ്ക": "nk", "ണ്ട": "nd", "ബ്ബ": "bb",
"പ്പ": "pp", "മ്മ": "mm",
"യ്യ": "yy", "ല്ല": "ll", "വ്വ": "vv", "ശ്ശ": "sh", "സ്സ": "s",
"ക്സ": "ks", "ഞ്ച": "nch", "ക്ഷ": "ksh", "മ്പ": "mp", "റ്റ": "tt", "ന്റ": "nt", "ന്ത": "nth",
"ന്ത്യ": "nthy"
}
__consonants = {
"ക": "k", "ഖ": "kh", "ഗ": "g", "ഘ": "gh", "ങ": "ng",
"ച": "ch", "ഛ": "chh", "ജ": "j", "ഝ": "jh", "ഞ": "nj",
"ട": "t", "ഠ": "dt", "ഡ": "d", "ഢ": "dd", "ണ": "n",
"ത": "th", "ഥ": "th", "ദ": "d", "ധ": "dh", "ന": "n",
"പ": "p", "ഫ": "ph", "ബ": "b", "ഭ": "bh", "മ": "m",
"യ": "y", "ര": "r", "ല": "l", "വ": "v",
"ശ": "sh", "ഷ": "sh", "സ": "s","ഹ": "h",
"ള": "l", "ഴ": "zh", "റ": "r"
}
__chil = {
"ൽ": "l", "ൾ": "l", "ൺ": "n",
"ൻ": "n", "ർ": "r", "ൿ": "k"
}
__modifiers = {
"ു്": "u", "ാ": "aa", "ി": "i", "ീ": "ee",
"ു": "u", "ൂ": "oo", "ൃ": "ru",
"െ": "e", "േ": "e", "ൈ": "y",
"ൊ": "o", "ോ": "o","ൌ": "ou", "ൗ": "au",
"ഃ": "a"
}
# ______ transliterate a malayalam string to english phonetically
def transliterate(self, input):
# replace zero width non joiners
input = re.sub(r'\xE2\x80\x8C', '', input)
# replace modified compounds first
input = self._replaceModifiedGlyphs(self.__compounds, input)
# replace modified non-compounds
input = self._replaceModifiedGlyphs(self.__vowels, input)
input = self._replaceModifiedGlyphs(self.__consonants, input)
v = ''
# replace unmodified compounds
for k, v in self.__compounds.items():
input = re.sub( k + '്([\\w])', v + '\1', input ) # compounds ending in chandrakkala but not at the end of the word
input = input.replace( k + '്', v + 'u' ) # compounds ending in chandrakkala have +'u' pronunciation
input = input.replace( k, v + 'a' ) # compounds not ending in chandrakkala have +'a' pronunciation
# glyphs not ending in chandrakkala have +'a' pronunciation
for k, v in self.__consonants.items():
input = re.sub( k + '(?!്)', v + 'a', input )
# glyphs ending in chandrakkala not at the end of a word
for k, v in self.__consonants.items():
input = re.sub( k + "്(?![\\s\)\.;,\"'\/\\\%\!])", v, input )
# remaining glyphs ending in chandrakkala will be at end of words and have a +'u' pronunciation
for k, v in self.__consonants.items():
input = input.replace( k + "്", v + 'u' )
# remaining consonants
for k, v in self.__consonants.items():
input = input.replace( k, v )
# vowels
for k, v in self.__vowels.items():
input = input.replace( k, v )
# chillu glyphs
for k, v in self.__chil.items():
input = input.replace( k, v )
# anusvaram 'am' at the end
input = input.replace( 'ം', 'm')
# replace any stray modifiers that may have been left out
for k, v in self.__modifiers.items():
input = input.replace( k, v )
# capitalize first letter of sentences for better aeshetics
chunks = re.split('([.!?] *)', input)
input = ''.join([w.capitalize() for w in chunks])
return input
# ______ replace modified glyphs
def _replaceModifiedGlyphs(self, glyphs, input):
# see if a given set of glyphs have modifiers trailing them
exp = re.compile( '((' + '|'.join(glyphs.keys()) + ')(' + '|'.join(self.__modifiers.keys()) + '))' )
matches = exp.findall(input)
# if yes, replace the glpyh with its roman equivalent, and the modifier with its
if matches != None:
for match in matches:
input = input.replace( match[0], glyphs[match[1]] + self.__modifiers[ match[2] ]);
return input