-
Notifications
You must be signed in to change notification settings - Fork 0
/
malayalam_transliterator.py
190 lines (164 loc) · 4.57 KB
/
malayalam_transliterator.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
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
import unicodedata
# Define the transliteration mappings
vowels_exclude = ['അ', 'ആ', 'ഇ', 'ഈ', 'ഉ', 'ഊ', 'ഋ', 'എ', 'ഏ', 'ഐ', 'ഒ', 'ഓ', 'ഔ', 'ം']
chillu = {
'ൺ': 'ṇ',
'ൻ': 'n',
'ർ': 'ṟ',
'ൽ': 'l',
'ൾ': 'ḷ',
'ൿ': 'k',
}
two_part_vowel_signs = { # aka circumgraphs
('െ', 'ാ'): 'ൊ',
('ാ', 'െ'): 'ൊ',
('േ', 'ാ'): 'ോ',
('ാ', 'േ'): 'ോ',
('െ', 'ൗ'): 'ൌ',
('ൗ', 'െ'): 'ൌ',
('െ', 'െ'): 'ൈ',
}
transliteration_mappings = {
'ാ': 'ā',
'ി': 'i',
'ീ': 'ī',
'ു': 'u',
'ൂ': 'ū',
'ൃ': 'r̥',
'ൄ': 'r̥̄',
'ൢ': 'l̥',
'ൣ': 'l̥̄',
'െ': 'e',
'േ': 'ē',
'ൈ': 'ai',
'ൊ': 'o',
'ോ': 'ō',
'ൌ': 'au', # 0D4C (archaic)
'ൗ': 'au', # 0D57
'അ': 'a',
'ആ': 'ā',
'ഇ': 'i',
'ഈ': 'ī',
'ഉ': 'u',
'ഊ': 'ū',
'ഋ': 'r̥',
'ൠ': 'r̥̄',
'ഌ': 'l̥',
'ൡ': 'l̥̄',
'എ': 'e',
'ഏ': 'ē',
'ഐ': 'ai',
'ഒ': 'o',
'ഓ': 'ō',
'ഔ': 'au',
'ക': 'k',
'ഖ': 'kh',
'ഗ': 'g',
'ഘ': 'gh',
'ങ': 'ṅ',
'ച': 'c',
'ഛ': 'ch',
'ജ': 'j',
'ഝ': 'jh',
'ഞ': 'ñ',
'ട': 'ṭ',
'ഠ': 'ṭh',
'ഡ': 'ḍ',
'ഢ': 'ḍh',
'ണ': 'ṇ',
'ത': 't',
'ഥ': 'th',
'ദ': 'd',
'ധ': 'dh',
'ന': 'n',
'പ': 'p',
'ഫ': 'ph',
'ബ': 'b',
'ഭ': 'bh',
'മ': 'm',
'റ': 'ṟ',
'റ്റ': 'ṯ',
'ഩ': 'ṉ',
'ഴ': 'ḻ',
'യ': 'y',
'ര': 'r',
'ല': 'l',
'ള': 'ḷ',
'വ': 'v',
'ശ': 'ś',
'ഷ': 'ṣ',
'സ': 's',
'ഹ': 'h',
'ം': 'ṁ',
'൦': '0',
'൧': '1',
'൨': '2',
'൩': '3',
'൪': '4',
'൫': '5',
'൬': '6',
'൭': '7',
'൮': '8',
'൯': '9',
}
# Set of characters that mark the end of a word
punctuation_set = set(r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~""")
whitespace_set = set(' \t\n\r\v\f')
word_end_markers = punctuation_set.union(whitespace_set)
# Zero-width non-joiner
zwnj = chr(8204)
word_end_markers.add(zwnj)
def is_diacritic(char):
if char == '\u0D02':
return False
category = unicodedata.category(char)
return category.startswith('M')
def transliterate(text):
transliterated_text = ''
i = 0
while i < len(text):
char = text[i]
# Check for the ligatures "ന്റ" and "റ്റ"
if (char == 'ന' or char == 'റ') and i + 2 < len(text) and text[i + 1:i + 3] == '്റ':
if char == 'ന':
transliterated_text += 'nṯ'
else:
transliterated_text += 'ṯṯ'
next_char = text[i + 3] if i + 3 < len(text) else None
if next_char and not is_diacritic(next_char) and next_char != '\u0D4D':
transliterated_text += 'a'
i += 3
# Check for two-part dependent vowel signs
elif i + 1 < len(text) and (char, text[i + 1]) in two_part_vowel_signs:
transliterated_text += transliteration_mappings[two_part_vowel_signs[(char, text[i + 1])]]
i += 2
elif char in transliteration_mappings:
transliterated_text += transliteration_mappings[char]
# 'a' follows most letters, unless its one of the following
# todo: put a comment here which explains whats going on here, because I can't even remember now
if not is_diacritic(char) and char not in vowels_exclude and (
i + 1 == len(text) or not is_diacritic(text[i + 1]) and text[i + 1] != '\u0D4D'):
transliterated_text += 'a'
i += 1
# Check for chillus characters
elif char in chillu:
transliterated_text += chillu[char]
i += 1
# Check for chandrakkala (virama)
elif char == '\u0D4D': # ്
# saṁvr̥tōkāram
if i == len(text) - 1 \
or text[i + 1] in word_end_markers \
or unicodedata.category(text[i + 1]).startswith('Z'): # All unicode separators
transliterated_text += 'ŭ'
i += 1
else: # If we weren't able to map it to something, we just copy it
transliterated_text += char
i += 1
return transliterated_text
if __name__ == '__main__':
while True:
text = input("Enter malayalam text to transliterate:\n")
if text.strip() == '':
break
print(transliterate(text))