-
Notifications
You must be signed in to change notification settings - Fork 18
/
ml2en.java
272 lines (230 loc) · 9.82 KB
/
ml2en.java
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Java port of ml2en.js
* ml2en.js Copyright Kailash Nadh [GPL-2.0]
* https://github.com/knadh/ml2en
*
* ml2en.java Copyright Subin Siby [GPL-3.0]
* https://github.com/subins2000/ml2en
*/
public class ml2en {
Map<String, String> _vowels = new HashMap<String, String>();
Map<String, String> _compounds = new HashMap<String, String>();
Map<String, String> _consonants = new HashMap<String, String>();
Map<String, String> _chil = new HashMap<String, String>();
Map<String, String> _modifiers = new HashMap<String, String>();
public void initVars() {
_vowels.put("അ", "a");
_vowels.put("ആ", "aa");
_vowels.put("ഇ", "i");
_vowels.put("ഈ", "ee");
_vowels.put("ഉ", "u");
_vowels.put("ഊ", "oo");
_vowels.put("ഋ", "ru");
_vowels.put("എ", "e");
_vowels.put("ഏ", "e");
_vowels.put("ഐ", "ai");
_vowels.put("ഒ", "o");
_vowels.put("ഓ", "o");
_vowels.put("ഔ", "au");
_compounds.put("ക്ക", "kk");
_compounds.put("ഗ്ഗ", "gg");
_compounds.put("ങ്ങ", "ng");
_compounds.put("ക്ക", "kk");
_compounds.put("ച്ച", "cch");
_compounds.put("ജ്ജ", "jj");
_compounds.put("ഞ്ഞ", "nj");
_compounds.put("ട്ട", "tt");
_compounds.put("ണ്ണ", "nn");
_compounds.put("ത്ത", "tth");
_compounds.put("ദ്ദ", "ddh");
_compounds.put("ദ്ധ", "ddh");
_compounds.put("ന്ന", "nn");
_compounds.put("ന്ത", "nth");
_compounds.put("ങ്ക", "nk");
_compounds.put("ണ്ട", "nd");
_compounds.put("ബ്ബ", "bb");
_compounds.put("പ്പ", "pp");
_compounds.put("മ്മ", "mm");
_compounds.put("യ്യ", "yy");
_compounds.put("ല്ല", "ll");
_compounds.put("വ്വ", "vv");
_compounds.put("ശ്ശ", "sh");
_compounds.put("സ്സ", "s");
_compounds.put("ക്സ", "ks");
_compounds.put("ഞ്ച", "nch");
_compounds.put("ക്ഷ", "ksh");
_compounds.put("മ്പ", "mp");
_compounds.put("റ്റ", "tt");
_compounds.put("ന്റ", "nt");
_compounds.put("ന്ത", "nth");
_compounds.put("ന്ത്യ", "nthy");
_consonants.put("ക", "k");
_consonants.put("ഖ", "kh");
_consonants.put("ഗ", "g");
_consonants.put("ഘ", "gh");
_consonants.put("ങ", "ng");
_consonants.put("ച", "ch");
_consonants.put("ഛ", "chh");
_consonants.put("ജ", "j");
_consonants.put("ഝ", "jh");
_consonants.put("ഞ", "nj");
_consonants.put("ട", "t");
_consonants.put("ഠ", "dt");
_consonants.put("ഡ", "d");
_consonants.put("ഢ", "dd");
_consonants.put("ണ", "n");
_consonants.put("ത", "th");
_consonants.put("ഥ", "th");
_consonants.put("ദ", "d");
_consonants.put("ധ", "dh");
_consonants.put("ന", "n");
_consonants.put("പ", "p");
_consonants.put("ഫ", "ph");
_consonants.put("ബ", "b");
_consonants.put("ഭ", "bh");
_consonants.put("മ", "m");
_consonants.put("യ", "y");
_consonants.put("ര", "r");
_consonants.put("ല", "l");
_consonants.put("വ", "v");
_consonants.put("ശ", "sh");
_consonants.put("ഷ", "sh");
_consonants.put("സ", "s");
_consonants.put("ഹ", "h");
_consonants.put("ള", "l");
_consonants.put("ഴ", "zh");
_consonants.put("റ", "r");
_chil.put("ൽ", "l");
_chil.put("ൾ", "l");
_chil.put("ൺ", "n");
_chil.put("ൻ", "n");
_chil.put("ർ", "r");
_chil.put("ൿ", "k");
_modifiers.put("ു്", "u");
_modifiers.put("ാ", "aa");
_modifiers.put("ി", "i");
_modifiers.put("ീ", "ee");
_modifiers.put("ു", "u");
_modifiers.put("ൂ", "oo");
_modifiers.put("ൃ", "ru");
_modifiers.put("െ", "e");
_modifiers.put("േ", "e");
_modifiers.put("ൈ", "y");
_modifiers.put("ൊ", "o");
_modifiers.put("ോ", "o");
_modifiers.put("ൌ", "ou");
_modifiers.put("ൗ", "au");
_modifiers.put("ഃ", "a");
}
// ______ transliterate a malayalam string to english phonetically
String transliterate(String 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);
String k = "", v = "";
// replace unmodified compounds
for (Map.Entry<String, String> pair : _compounds.entrySet()) {
k = pair.getKey();
v = pair.getValue();
input = Pattern.compile(k + "്([\\w])", Pattern.UNICODE_CASE).matcher(input).replaceAll(v + "$1" ); // compounds ending in chandrakkala but not at the end of the word
input = Pattern.compile(k + "്", Pattern.UNICODE_CASE).matcher(input).replaceAll(v + "u" ); // compounds ending in chandrakkala have +'u' pronunciation
input = Pattern.compile(k, Pattern.UNICODE_CASE).matcher(input).replaceAll(v + "a" ); // compounds not ending in chandrakkala have +'a' pronunciation
}
// glyphs not ending in chandrakkala have +'a' pronunciation
for (Map.Entry<String, String> pair : _consonants.entrySet()) {
k = pair.getKey();
v = pair.getValue();
input = Pattern.compile(k + "(?!്)", Pattern.UNICODE_CASE).matcher(input).replaceAll(v + "a" );
}
// glyphs ending in chandrakkala not at the end of a word
for (Map.Entry<String, String> pair : _consonants.entrySet()) {
k = pair.getKey();
v = pair.getValue();
input = Pattern.compile(k + "്(?![\\s).;,\"'\\/])", Pattern.UNICODE_CASE | Pattern. CASE_INSENSITIVE).matcher(input).replaceAll(v);
}
// remaining glyphs ending in chandrakkala will be at end of words and have a +'u' pronunciation
for (Map.Entry<String, String> pair : _consonants.entrySet()) {
k = pair.getKey();
v = pair.getValue();
input = Pattern.compile(k + "്", Pattern.UNICODE_CASE | Pattern. CASE_INSENSITIVE).matcher(input).replaceAll(v + "u" );
}
// remaining consonants
for (Map.Entry<String, String> pair : _consonants.entrySet()) {
k = pair.getKey();
v = pair.getValue();
input = Pattern.compile(k, Pattern.UNICODE_CASE).matcher(input).replaceAll(v);
}
// vowels
for (Map.Entry<String, String> pair : _vowels.entrySet()) {
k = pair.getKey();
v = pair.getValue();
input = Pattern.compile(k, Pattern.UNICODE_CASE).matcher(input).replaceAll(v);
}
// chillu glyphs
for (Map.Entry<String, String> pair : _chil.entrySet()) {
k = pair.getKey();
v = pair.getValue();
input = Pattern.compile(k, Pattern.UNICODE_CASE).matcher(input).replaceAll(v);
}
// anusvaram 'am' at the end
input = input.replaceAll("ം", "m");
// replace any stray modifiers that may have been left out
for (Map.Entry<String, String> pair : _modifiers.entrySet()) {
k = pair.getKey();
v = pair.getValue();
input = Pattern.compile(k, Pattern.UNICODE_CASE).matcher(input).replaceAll(v);
}
// capitalize first letter of sentences for better aeshetics
input = capitalizeFirstLetterInEverySentence(input);
return input;
}
// ______ replace modified glyphs
String _replaceModifiedGlyphs(Map glyphs, String input) {
// see if a given set of glyphs have modifiers trailing them
Matcher match;
Pattern re = Pattern.compile("(" + String.join("|", _getKeys(glyphs)) + ")(" + String.join("|", _getKeys(_modifiers)) + ")", Pattern.UNICODE_CASE);
// if yes, replace the glpyh with its roman equivalent, and the modifier with its
match = re.matcher(input);
while (match.find()) {
input = Pattern.compile(match.group(0), Pattern.UNICODE_CASE).matcher(input).replaceAll(glyphs.get(match.group(1)) + _modifiers.get(match.group(2)));
}
return input;
}
// ______ get the keys of an object literal
String[] _getKeys(Map<String, String> o) {
Set<String> keys = o.keySet();
String[] keysStr = keys.toArray(new String[keys.size()]);
return keysStr;
}
/**
* https://tech.chitgoks.com/2010/09/24/capitalize-first-letter-of-every-sentence-in-java/
* @param content Input
* @return Capitalized output
*/
public static String capitalizeFirstLetterInEverySentence(String content) {
Pattern capitalize = Pattern.compile("([\\?!\\.]\\s*)([a-z])");
Matcher m = capitalize.matcher(content);
while (m.find()) {
content = m.replaceFirst(m.group(1) + m.group(2).toUpperCase());
m = capitalize.matcher(content);
}
// Capitalize the first letter of the string.
content = String.format("%s%s", Character.toUpperCase(content.charAt(0)), content.substring(1));
return content;
}
public ml2en() {
initVars();
}
public String convert(String input) {
return transliterate(input);
}
}