-
Notifications
You must be signed in to change notification settings - Fork 21
/
ml2en.rb
140 lines (112 loc) · 4.65 KB
/
ml2en.rb
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
#! /usr/bin/env ruby
# encoding: utf-8
<<INFO
(Phonemic) Romanization of Malayalam script
Original script by Kailash Nadh, 2012, 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
INFO
class Ml2En
class << self
def __vowels
{
"അ"=> "a", "ആ"=> "aa", "ഇ"=> "i", "ഈ"=> "ee", "ഉ"=> "u", "ഊ"=> "oo", "ഋ"=> "ru",
"എ"=> "e", "ഏ"=> "e", "ഐ"=> "ai", "ഒ"=> "o", "ഓ"=> "o", "ഔ"=> "au" }
end
def __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"}
end
def __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"}
end
def __chil
{"ൽ"=> "l", "ൾ"=> "l", "ൺ"=> "n",
"ൻ"=> "n", "ർ"=> "r", "ൿ"=> "k"}
end
def __modifiers
{
"ു്"=> "u", "ാ"=> "aa", "ി"=> "i", "ീ"=> "ee",
"ു"=> "u", "ൂ"=> "oo", "ൃ"=> "ru",
"െ"=> "e", "േ"=> "e", "ൈ"=> "y",
"ൊ"=> "o", "ോ"=> "o","ൌ"=> "ou", "ൗ"=> "au",
"ഃ"=> "a"}
end
# ______ transliterate a malayalam string to english phonetically
def transliterate(input)
# replace zero width non joiners
input.gsub!("/\xE2\x80\x8C/u", '')
# replace modified compounds first
input = self._replaceModifiedGlyphs(self.__compounds, input)
# replace modified non-compounds
input = self._replaceModifiedGlyphs(self.__vowels.merge(self.__consonants), input)
v = ''
# replace unmodified compounds
self.__compounds.each do |k,v|
input.gsub!( k + '്([\\w])', v + '\1') # compounds ending in chandrakkala but not at the end of the word
input.gsub!( k + '്', v + 'u' ) # compounds ending in chandrakkala have +'u' pronunciation
input.gsub!( k, v + 'a' ) # compounds not ending in chandrakkala have +'a' pronunciation
end
# glyphs not ending in chandrakkala have +'a' pronunciation
self.__consonants.each do |k,v|
input.gsub!(Regexp.new( k+ '(?!്)'), v + 'a')
end
# glyphs ending in chandrakkala not at the end of a word
self.__consonants.each do |k,v|
input.gsub!( Regexp.new(k + "്(?![\\s\)\.;,\"'\/\\\%\!])"), v)
end
# remaining glyphs ending in chandrakkala will be at end of words and have a +'u' pronunciation
self.__consonants.each do |k,v|
input.gsub!(Regexp.new( k + "്"), v + 'u' )
end
# remaining consonants
self.__consonants.each do |k,v|
input.gsub!(Regexp.new( k), v )
end
# vowels
self.__vowels.each do |k,v|
input.gsub!( Regexp.new(k), v )
end
# chillu glyphs
self.__chil.each do |k,v|
input.gsub!( Regexp.new(k), v )
end
# anusvaram 'am' at the end
input.gsub!( 'ം', 'm')
# replace any stray modifiers that may have been left out
self.__modifiers.each do |k,v|
input.gsub!( k, v )
end
# capitalize first letter of sentences for better aeshetics
return input
end
# ______ replace modified glyphs
def _replaceModifiedGlyphs(glyphs, input)
# see if a given set of glyphs have modifiers trailing them
exp = Regexp.new("((#{glyphs.keys.join("|")})(#{self.__modifiers.keys.join('|')}))")
matches = input.scan(exp)
# if yes, replace the glpyh with its roman equivalent, and the modifier with its
unless matches.empty? # != None=>
matches.each do |match|
input.gsub!( match[0], glyphs[match[1]] + self.__modifiers[ match[2] ])
end
end
return input
end
end
end