-
Notifications
You must be signed in to change notification settings - Fork 8
/
transliterator.py
61 lines (54 loc) · 2.32 KB
/
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
from utils.alphabet import alphabet, letters, utfalpha, punctuations
from utils.gujarati_hindi import gu_hi, hi_gu
import re
class Transliterator():
def __init__(self, verbose=False):
self.verbose=verbose
def letter_transliterate_gujarati_to_english(self, letter):
"""Transliterates the letter given in Gujarati and prints out the english pronounciation"""
return list(letters.keys())[list(letters.values()).index(letter)]
def gujarati_to_english(self, word):
"""Transliterates the word given in Gujarati and prints out the pronounciation in English"""
word = re.sub(r'્', r'', word)
word = re.sub(r'\u200b',r'',word)
return_list = []
for i in range(len(word)):
try:
if i<len(word)-1:
if word[i] == 'ં' and word[i+1] in punctuations.keys():
continue
if i==len(word)-1 and word[i]=='ં':
continue
a = list(alphabet.keys())[list(alphabet.values()).index(word[i])]
return_list.append(a)
except ValueError:
x = word[i].encode('utf-8')
a = list(utfalpha.keys())[list(utfalpha.values()).index(x)]
return_list.append(a)
translation = ''.join(letter for letter in return_list)
return translation
def hindi_to_gujarati(self, sentence):
"""Transliterates the sentence given in Hindi and prints out the pronounciation in Gujarati"""
l = list(sentence)
for i in range(len(l)):
try:
l[i] = hi_gu[l[i]]
except KeyError:
if self.verbose:
print("Warning: {} does not exist in the dictionary".format(l[i]))
pass
l = ''.join(l)
l = re.sub(r'\u200b', "", l)
l = re.sub(r'\u200d', "", l)
return l
def gujarati_to_hindi(self, sentence):
"""Transliterates the word given in Gujarati and prints out the pronounciation in Hindi"""
l = list(sentence)
for i in range(len(l)):
try:
l[i] = gu_hi[l[i]]
except:
if self.verbose:
print("Warning: {} does not exist in the dictionary".format(l[i]))
pass
return ''.join(l)