-
Notifications
You must be signed in to change notification settings - Fork 21
/
ml2en.js
178 lines (139 loc) · 5.53 KB
/
ml2en.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
(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
*/
var ml2en = function(input) {
var _vowels = {
"അ": "a", "ആ": "aa", "ഇ": "i", "ഈ": "ee", "ഉ": "u", "ഊ": "oo", "ഋ": "ru",
"എ": "e", "ഏ": "e", "ഐ": "ai", "ഒ": "o", "ഓ": "o", "ഔ": "au"
};
var _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"
};
var _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"
};
var _chil = {
"ൽ": "l", "ൾ": "l", "ൺ": "n",
"ൻ": "n", "ർ": "r", "ൿ": "k"
};
var _modifiers = {
"ു്": "u", "ാ": "aa", "ി": "i", "ീ": "ee",
"ു": "u", "ൂ": "oo", "ൃ": "ru",
"െ": "e", "േ": "e", "ൈ": "y",
"ൊ": "o", "ോ": "o","ൌ": "ou", "ൗ": "au",
"ഃ": "a"
};
// ______ transliterate a malayalam string to english phonetically
function transliterate(input) {
// replace zero width non joiners
input = input.replace(/[\u200B-\u200D\uFEFF]/g, '');
// replace modified compounds first
input = _replaceModifiedGlyphs(_compounds, input);
// replace modified non-compounds
input = _replaceModifiedGlyphs(_vowels, input);
input = _replaceModifiedGlyphs(_consonants, input);
var v = '';
// replace unmodified compounds
for(var k in _compounds) {
if(!_compounds.hasOwnProperty(k)) continue;
v = _compounds[k];
input = input.replace( new RegExp(k + "്([\\w])", 'g'), v + '$1' ); // compounds ending in chandrakkala but not at the end of the word
input = input.replace( new RegExp(k + "്", 'g'), v + 'u' ); // compounds ending in chandrakkala have +'u' pronunciation
input = input.replace( new RegExp(k, 'g'), v + 'a' ); // compounds not ending in chandrakkala have +'a' pronunciation
}
// glyphs not ending in chandrakkala have +'a' pronunciation
for(var k in _consonants) {
if(!_consonants.hasOwnProperty(k)) continue;
v = _consonants[k];
input = input.replace( new RegExp(k + "(?!്)", 'g'), v + 'a' );
}
// glyphs ending in chandrakkala not at the end of a word
for(var k in _consonants) {
if(!_consonants.hasOwnProperty(k)) continue;
v = _consonants[k];
input = input.replace( new RegExp( k + "്(?![\\s\)\.;,\"'\/\\\%\!])", 'ig'), v );
}
// remaining glyphs ending in chandrakkala will be at end of words and have a +'u' pronunciation
for(var k in _consonants) {
if(!_consonants.hasOwnProperty(k)) continue;
v = _consonants[k];
input = input.replace( new RegExp(k + "്", 'g'), v + 'u' );
}
// remaining consonants
for(var k in _consonants) {
if(!_consonants.hasOwnProperty(k)) continue;
v = _consonants[k];
input = input.replace( new RegExp(k, 'g'), v );
}
// vowels
for(var k in _vowels) {
if(!_vowels.hasOwnProperty(k)) continue;
v = _vowels[k];
input = input.replace( new RegExp(k, 'g'), v );
}
// chillu glyphs
for(var k in _chil) {
if(!_chil.hasOwnProperty(k)) continue;
v = _chil[k];
input = input.replace( new RegExp(k, 'g'), v );
}
// anusvaram 'am' at the end
input = input.replace( /ം/g, 'm');
// replace any stray modifiers that may have been left out
for(var k in _modifiers) {
if(!_modifiers.hasOwnProperty(k)) continue;
v = _modifiers[k];
input = input.replace( new RegExp(k, 'g'), v );
}
// capitalize first letter of sentences for better aeshetics
input = input.replace(/(^\s*\w|[\.\!\?]\s*\w)/g, function(c) { return c.toUpperCase(); });
return input;
}
// ______ replace modified glyphs
function _replaceModifiedGlyphs(glyphs, input) {
// see if a given set of glyphs have modifiers trailing them
var match = 0,
re = new RegExp("(" + _getKeys(glyphs).join('|') + ")(" + _getKeys(_modifiers).join('|') + ")", 'g');
// if yes, replace the glpyh with its roman equivalent, and the modifier with its
while(match != null) {
match = re.exec(input);
if(match)
input = input.replace( new RegExp(match[0], 'g'), glyphs[ match[1] ] + _modifiers[ match[2] ]);
}
return input;
}
// ______ get the keys of an object literal
function _getKeys(o) {
var keys = [];
for(var k in o) {
if(o.hasOwnProperty(k)) {
keys.push(k);
}
}
return keys;
}
// _____ construct
return transliterate(input);
};