forked from knadh/ml2en
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathml2en.php
139 lines (110 loc) · 5.03 KB
/
ml2en.php
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
<?php
/*
(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
*/
class ml2en {
private static $_vowels = array(
"അ" => "a", "ആ" => "aa", "ഇ" => "i", "ഈ" => "ee", "ഉ" => "u", "ഊ" => "oo", "ഋ" => "ru",
"എ" => "e", "ഏ" => "e", "ഐ" => "ai", "ഒ" => "o", "ഓ" => "o", "ഔ" => "au"
);
private static $_compounds = array(
"ക്ക" => "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"
);
private static $_consonants = array(
"ക" => "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"
);
private static $_chil = array(
"ൽ" => "l", "ൾ" => "l", "ൺ" => "n",
"ൻ" => "n", "ർ" => "r", "ൿ" => "k"
);
private static $_modifiers = array(
"ു്" => "u", "ാ" => "aa", "ി" => "i", "ീ" => "ee",
"ു" => "u", "ൂ" => "oo", "ൃ" => "ru",
"െ" => "e", "േ" => "e", "ൈ" => "y",
"ൊ" => "o", "ോ" => "o","ൌ" => "ou", "ൗ" => "au",
"ഃ" => "a"
);
// ______ transliterate a malayalam string to english phonetically
public static function transliterate($input) {
// replace zero width non joiners
$input = preg_replace("/\xE2\x80\x8C/u", '', $input);
// replace modified compounds first
$input = self::_replaceModifiedGlyphs(self::$_compounds, $input);
// replace modified non-compounds
$input = self::_replaceModifiedGlyphs(array_merge(self::$_vowels, self::$_consonants), $input);
// replace unmodified compounds
foreach(self::$_compounds as $k=>$v) {
$input = preg_replace("/".$k."്([\w])/u", $v.'$1', $input); // compounds ending in chandrakkala but not at the end of the word
$input = preg_replace("/".$k."്/u", $v.'u', $input); // compounds ending in chandrakkala have +'u' pronunciation
$input = preg_replace("/".$k."/u", $v.'a', $input); // compounds not ending in chandrakkala have +'a' pronunciation
}
// glyphs not ending in chandrakkala have +'a' pronunciation
foreach(self::$_consonants as $k=>$v) {
$input = preg_replace("/".$k."(?!്)/u", $v.'a$1', $input);
}
// glyphs ending in chandrakkala not at the end of a word
foreach(self::$_consonants as $k=>$v) {
$input = preg_replace("/".$k."്(?![\s\)\(\[\]\.;,\"'\/\\\%\!]|$)/iu", $v.'$1', $input);
}
// remaining glyphs ending in chandrakkala will be at end of words and have a +'u' pronunciation
foreach(self::$_consonants as $k=>$v) {
$input = preg_replace("/".$k."്/iu", $v.'u', $input);
}
// remaining self::$_consonants
foreach(self::$_consonants as $k=>$v) {
$input = preg_replace("/".$k."/u", $v, $input);
}
// vowels
foreach(self::$_vowels as $k=>$v) {
$input = preg_replace("/".$k."/u", $v, $input);
}
// chillu glyphs
foreach(self::$_chil as $k=>$v) {
$input = preg_replace("/".$k."/u", $v, $input);
}
// anusvaram 'am' at the end
$input = preg_replace("/ം/u", 'm', $input);
// replace any stray modifiers that may have been left out
foreach(self::$_modifiers as $k=>$v) {
$input = preg_replace("/".$k."/u", $v, $input);
}
// capitalize first letter of sentences for better aeshetics
$input = preg_replace('/([.!?])\s*(\w)/e', "strtoupper('\\1 \\2')", ucfirst(strtolower($input)));
return $input;
}
// ______ replace modified glyphs
private static function _replaceModifiedGlyphs($glyphs, $input) {
// see if a given set of glyphs have modifiers trailing them
preg_match_all("/(".implode("|", array_keys($glyphs)).")(". implode('|', array_keys(self::$_modifiers) ).")/u", $input, $match);
// if yes, replace the glpyh with its roman equivalent, and the modifier with its
if(isset($match[0])) {
for($n=0; $n<count($match[0]); $n++) {
$input = preg_replace("/".$match[0][$n]."/u", $glyphs[ $match[1][$n] ] . self::$_modifiers[ $match[2][$n] ], $input);
}
}
return $input;
}
}
?>